static MoneyAmount Reserve(MoneyAmount cost) { decimal factor = 1; if (IsHappyHour) { factor = 0.5M; } Console.WriteLine("\nReserving an item that cost: {0}.", cost); return(cost.Scale(factor)); }
static void Main(string[] args) { /*Buy(new MoneyAmount(12, "USD"), * new MoneyAmount(10, "USD")); * * Buy(new MoneyAmount(7, "USD"), * new MoneyAmount(10, "USD")); * * IsHappyHour = true; * Buy(new MoneyAmount(7, "USD"), * new MoneyAmount(10, "USD"));*/ /*int x = 2; * int y = 4; * MoneyAmount x = new MoneyAmount(2, "USD"); * MoneyAmount y = new MoneyAmount(4, "USD"); * * if (x.Equals(y)) * Console.WriteLine("Equal."); * if ((x*2).Equals(y)) * Console.WriteLine("Equal after scaling.");*/ /* * MoneyAmount x = new MoneyAmount(2, "USD"); * MoneyAmount y = new MoneyAmount(2, "USD"); * * HashSet<MoneyAmount> set = new HashSet<MoneyAmount>(); * set.Add(x); * * if (set.Contains(y)) * Console.WriteLine("Cannot add repeated value."); * else * set.Add(y); * * Console.WriteLine("Count = {0}", set.Count); */ MoneyAmount x = new MoneyAmount(2, "USD"); MoneyAmount y = new MoneyAmount(4, "USD"); if (x == y) { Console.WriteLine("Equal."); } if ((x * 2) == y) { Console.WriteLine("Equal after scaling."); } Console.ReadLine(); }
// Reference types are susceptible to aliasing bugs; // By changing contents of an existing object we are risking an aliasing bug to apear; // Try to avoid instantiating objects directly; // Modification to the constructor requires update in all consumers; // Let objects of the class construct subsequent objects; private static MoneyAmount Reserve(MoneyAmount cost) { decimal factor = 1; var newCost = cost; if (IsHappyHour) { // Create new object when content should change; factor = .5M; } Console.WriteLine($"\nReserving an item that costs {cost}"); return(cost.Scale(factor)); }
private static void Buy(MoneyAmount wallet, MoneyAmount cost) { var enoughMoney = wallet.Amount >= cost.Amount; var finalCost = Reserve(cost); var finalEnough = wallet.Amount >= finalCost.Amount; if (enoughMoney && finalEnough) { Console.WriteLine($"You will pay {cost} with your {wallet}."); } else if (finalEnough) { Console.WriteLine($"This time, {wallet} will be enough to pay {finalCost}"); } else { Console.WriteLine($"You cannot pay {cost} with your {wallet}."); } }
static void Buy(MoneyAmount wallet, MoneyAmount cost) { bool enoughMoney = wallet.Amount >= cost.Amount; MoneyAmount finalCost = Reserve(cost); bool finalEnough = wallet.Amount >= finalCost.Amount; if (enoughMoney && finalEnough) { Console.WriteLine("You will pay {0} with your {1}", cost, wallet); } else if (finalEnough) { Console.WriteLine("This time, {0} will be enough to pay {1}", wallet, finalCost); } else { Console.WriteLine("You cannot pay {0} with your {1}", cost, wallet); } }
private static void Main() { //Buy( // new MoneyAmount(100, "USD"), // new MoneyAmount(70, "USD") //); //IsHappyHour = true; //Buy( // new MoneyAmount(100, "USD"), // new MoneyAmount(120, "USD") //); var x = new MoneyAmount(5, "USD"); var y = new MoneyAmount(5, "USD"); if (x == y) { Console.WriteLine("Equal."); } if ((x * 2) == y) { Console.WriteLine("Equal after scaling."); } //var x = 2; //var y = 2; //var set = new HashSet<MoneyAmount>(); //set.Add(x); //if (set.Contains(y)) // Console.WriteLine("Cannot add repeated value."); //else // set.Add(y); //Console.WriteLine($"Count = {set.Count}"); }