public void AddCoin(Coin coin, int ammount) { Contract.Requires(ammount > 0); Contract.Requires(Contract.Exists(Wallet, el => (decimal)el.Element("Type") == coin.ToValue()), "PRE: The coin type must exist in the wallet"); Contract.Ensures(ammount <= (int)Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single().Element("Ammount")); Contract.Ensures(Contract.OldValue((int)Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single().Element("Ammount")) + ammount == (int)Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single().Element("Ammount")); XElement soughtCoin = Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single(); soughtCoin.Element("Ammount").Value = ((int)soughtCoin.Element("Ammount") + ammount).ToString(); Database.Save("CoinDatabase.xml"); }
public TransactionResult InsertCoin(Coin coin) { Contract.Requires(state == State.ProductChosen || state == State.MoneyInserted); Contract.Ensures(state == State.MoneyInserted); InsertedValue += coin.ToValue(); CoinMan.AddCoin(coin, 1); state = State.MoneyInserted; if (SelectedProduct != null && !CoinMan.CheckChange(SelectedProduct.Price, InsertedValue)) return TransactionResult.SuccessButNoChange; return TransactionResult.Success; }
public void EjectCoin(Coin coin, int ammount, LinkedList<Coin> coinCase) { Contract.Requires(coinCase != null); Contract.Requires(ammount >= 0); Contract.Requires(Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Select(c => (int)c.Element("Ammount")).Single() >= ammount, "PRE: There must be enough of coins of given type to eject"); Contract.Ensures(Contract.OldValue(coinCase.Where(c => c == coin).Count()) + ammount == coinCase.Where(c => c == coin).Count(), "POST: The ejected coins must be in the case"); Contract.Ensures(Contract.OldValue((int)Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single().Element("Ammount")) - ammount == (int)Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single().Element("Ammount")); XElement soughtCoin = Wallet.Where(c => (decimal)c.Element("Type") == coin.ToValue()).Single(); soughtCoin.Element("Ammount").Value = ((int)soughtCoin.Element("Ammount") - ammount).ToString(); for (int i = 0; i < ammount; ++i) coinCase.AddLast(coin); Database.Save("CoinDatabase.xml"); }