public bool CanMakeChange(int cost)
        {
            int priorAmount = enteredAmount;

            enteredAmount -= cost;
            CashBox tempCashBox = new CashBox(cashbox);
            bool    result      = ReturnChange(tempCashBox, false);

            enteredAmount = priorAmount;
            return(result);
        }
Beispiel #2
0
 public CashBox(CashBox src)
 {
     quarters = new List <Quarter>();
     dimes    = new List <Dime>();
     nickels  = new List <Nickel>();
     pennies  = new List <Penny>();
     AddQuarterToCashBox(src.quarters.Count);
     AddDimeToCashBox(src.dimes.Count);
     AddNickelToCashBox(src.nickels.Count);
     AddPennyToCashBox(src.pennies.Count);
 }
 //constructor
 public SodaMachine()
 {
     inventory = new Inventory();
     inventory.AddGrapeToInventory(20);
     inventory.AddOrangeToInventory(20);
     inventory.AddLemonToInventory(20);
     cashbox = new CashBox();
     cashbox.AddQuarterToCashBox(20);
     cashbox.AddDimeToCashBox(10);
     cashbox.AddNickelToCashBox(20);
     cashbox.AddPennyToCashBox(50);
     enteredAmount = 0;
 }
 public bool ReturnChange(CashBox box, bool printText)
 {
     while (enteredAmount > 0)
     {
         if (enteredAmount >= 25 && cashbox.quarters.Count > 0)
         {
             if (printText)
             {
                 Console.WriteLine("The Machine dispensed one quarter.");
             }
             cashbox.RemoveQuarterFromCashBox(1);
             enteredAmount = (enteredAmount - Quarter.WORTH);
         }
         else if (enteredAmount >= 10 && cashbox.dimes.Count > 0)
         {
             if (printText)
             {
                 Console.WriteLine("The Machine dispensed one dime.");
             }
             cashbox.RemoveDimeFromCashBox(1);
             enteredAmount = (enteredAmount - Dime.WORTH);
         }
         else if (enteredAmount >= 5 && cashbox.nickels.Count > 0)
         {
             if (printText)
             {
                 Console.WriteLine("The Machine dispensed one nickel.");
             }
             cashbox.RemoveNickelFromCashBox(1);
             enteredAmount = (enteredAmount - Nickel.WORTH);
         }
         else if (enteredAmount >= 1 && cashbox.pennies.Count > 0)
         {
             if (printText)
             {
                 Console.WriteLine("The Machine dispensed one penny.");
             }
             cashbox.RemovePennyFromCashBox(1);
             enteredAmount = (enteredAmount - Penny.WORTH);
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }