Beispiel #1
0
        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 double GetProductPriceByKey(int id)
 {
     using (var context = new VendMachineDbContext())
     {
         Product p     = context.Products.Where(x => x.ProductId == id).FirstOrDefault();
         double  price = (Double)p.Price;
         return(price);
     }
 }
 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 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 IList <Product> GetProducts()
 {
     try
     {
         using (var context = new VendMachineDbContext())
         {
             return(context.Products.ToList <Product>());;
         }
     }
     catch (Exception)
     {
         log.Error("Db connection-GET Prod");
         return(new List <Product>());
     }
 }
Beispiel #7
0
 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 DecreaseProductQuantity(int productId)
 {
     try
     {
         using (var context = new VendMachineDbContext())
         {
             Product p = context.Products.Where(x => x.ProductId == productId).FirstOrDefault();
             p.Quantity -= 1;
             UpdateProduct(p);
         }
     }
     catch (Exception)
     {
         log.Error("Db connection failed-DecreaseProd");
     }
 }
 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");
     }
 }
Beispiel #10
0
 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");
     }
 }
Beispiel #11
0
 public List <Transaction> GetTransactions()
 {
     try
     {
         List <Transaction> transactions = new List <Transaction>();
         using (var db = new VendMachineDbContext())
         {
             transactions = db.Transactions.ToList <Transaction>();
         }
         return(transactions);
     }
     catch (Exception)
     {
         log.Error("Db connection");
         return(new List <Transaction>());
     }
 }
 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");
     }
 }
 public IList <Product> GetProductsToRefill()
 {
     try
     {
         using (var context = new VendMachineDbContext())
         {
             IList <Product> productQuantity = (from product in context.Products
                                                where (product.Quantity != 10)
                                                select product).ToList();
             return(productQuantity);
         }
     }
     catch (Exception)
     {
         log.Error("FIND refill products failed");
         return(new List <Product>());
     }
 }
 private bool IsValidCard(string cardNumber, string cardPin)
 {
     try
     {
         using (var db = new VendMachineDbContext())
         {
             Account Account = db.Accounts.Where(x => x.CardNO == cardNumber).FirstOrDefault();
             if (Account != null && Account.Pin == cardPin)
             {
                 return(true);
             }
             return(false);
         }
     }
     catch (Exception) {
         log.Error("Db connection failed");
     }
     return(false);
 }
 public bool IsEnough(double cost)
 {
     try
     {
         using (var db = new VendMachineDbContext())
         {
             Account Account = db.Accounts.Where(x => x.CardNO == cardNo).FirstOrDefault();
             if (Account.Amount >= cost)
             {
                 return(true);
             }
         }
     }
     catch (Exception)
     {
         log.Error("Db connection failed");
     }
     return(false);
 }
Beispiel #16
0
 public bool IsValid(IList <CashMoney> money)
 {
     try
     {
         using (var db = new VendMachineDbContext())
         {
             IList <CashMoney> cashMoney = db.Money.ToList <CashMoney>();
             foreach (CashMoney m in money)
             {
                 if (cashMoney.Where(mon => mon.MoneyValue == m.MoneyValue).FirstOrDefault() == null)
                 {
                     return(false);
                 }
             }
             return(true);
         }
     }
     catch (Exception)
     {
         log.Error("Fail database connection-IsValid");
         return(false);
     }
 }