protected void addCash(RegisterItem cashAmount) { if (cashAmount.NumOfCoins > 0) { // add more of the accepted coins, doesn't test if the coin is accepted as it checked before the method gets called var amountAdded = Cash.First(c => c.AcceptedCoin == cashAmount.AcceptedCoin); var newTotal = amountAdded.NumOfCoins + cashAmount.NumOfCoins; // checks if the new total will exceed the limit , if not add it otherwise show the error message to user if (newTotal <= totalLimitPerAcceptedCoin) { amountAdded.NumOfCoins = amountAdded.NumOfCoins + cashAmount.NumOfCoins; Console.WriteLine("The amount was sucessfully added to the register!"); } else { Console.WriteLine("The amount was not added to the register as the number of total coins exceeded the limit!"); } } else { Console.WriteLine("The number of coins added is invalid. Please try again."); } }
public Register() { RegisterItem pence5 = new RegisterItem(0.05m, 20); RegisterItem pence10 = new RegisterItem(0.10m, 20); RegisterItem pence20 = new RegisterItem(0.20m, 20); RegisterItem pence50 = new RegisterItem(0.50m, 20); RegisterItem pound1 = new RegisterItem(1.00m, 20); RegisterItem pound2 = new RegisterItem(2.00m, 10); Cash = new List <RegisterItem>() { pence5, pence10, pence20, pence50, pound1, pound2 }; }
protected void removeCash(RegisterItem cashAmount) { if (cashAmount.NumOfCoins > 0) { // remove the amount var amountRemoved = Cash.First(c => c.AcceptedCoin == cashAmount.AcceptedCoin); amountRemoved.NumOfCoins = amountRemoved.NumOfCoins - cashAmount.NumOfCoins; Console.WriteLine("The amount was sucessfully removed from the register!"); } else { Console.WriteLine("The number of coins to be removed is invalid. Please try again."); } }