public void GiveChange(double change) { List <CashMoney> money = new List <CashMoney>(); List <CashMoney> changedMoney = new List <CashMoney>(); try { using (var db = new VendMachineDbContext()) { money = db.Money.ToList <CashMoney>(); changedMoney = CalculateMinimum(money, change); Console.WriteLine("Change: "); for (int i = 0; i < changedMoney.Count; i++) { CashMoney coinFromChange = changedMoney[i]; Console.Write(changedMoney[i] + " "); CashMoney cashMoney = db.Money.Where(x => x.MoneyValue == coinFromChange.MoneyValue).FirstOrDefault(); cashMoney.Quantity -= coinFromChange.Quantity; db.Entry(cashMoney).State = EntityState.Modified; db.SaveChanges(); } log.Info("GIVE Change success"); } } catch (Exception) { log.Error("Fail database connection-GIVE Change"); } }
public bool Refill() { try { IList <Product> productQuantity = GetProductsToRefill(); using (var context = new VendMachineDbContext()) { foreach (Product prod in productQuantity) { prod.Quantity = 10; context.Entry(prod).State = EntityState.Modified; context.SaveChanges(); } } log.Info("REFILL successful"); return(true); } catch (Exception) { log.Error("REFILL failed"); return(false); } }
public void UpdateProduct(Product p) { try { using (var context = new VendMachineDbContext()) { context.Entry(p).State = EntityState.Modified; context.SaveChanges(); } } catch (Exception) { log.Error("Db connection failed-UPDATE"); } }
public void AddProduct(Product p) { try { using (var context = new VendMachineDbContext()) { context.Products.Add(p); context.SaveChanges(); } } catch (Exception) { log.Error("Db connection failed-ADD"); } }
public void AddTransaction(Transaction t) { try { using (var db = new VendMachineDbContext()) { db.Transactions.Add(t); db.SaveChanges(); log.Info("Transaction Added"); } } catch (Exception) { log.Error("Db connection failed-transactions"); } }
public void RemoveProduct(int productId) { try { using (var context = new VendMachineDbContext()) { Product p = context.Products.Where(x => x.ProductId == productId).FirstOrDefault(); context.Products.Remove(p); context.SaveChanges(); } } catch (Exception) { log.Error("Db connection failed-remove prod"); } }
public void UpdateMoney(double value, int quantity) { try { using (var db = new VendMachineDbContext()) { CashMoney cashMoney = db.Money.Where(x => x.MoneyValue == value).FirstOrDefault(); cashMoney.Quantity += quantity; db.Entry(cashMoney).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception) { log.Error("Fail database connection\n"); } }
public void Pay(double cost) { try { using (var db = new VendMachineDbContext()) { Account Account = db.Accounts.Where(x => x.CardNO == cardNo).FirstOrDefault(); Account.Amount -= cost; db.Entry(Account).State = EntityState.Modified; db.SaveChanges(); log.Info("Payment success"); } } catch (Exception) { log.Error("Db connection failed"); } }