Ejemplo n.º 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");
            }
        }
Ejemplo n.º 2
0
 public void AddMoney(double money)
 {
     if (acceptedDenominations.Contains(money))
     {
         CashMoney cashMoney = new CashMoney(money, 1);
         IntroducedMoney.Add(cashMoney);
         TotalMoney += money;
     }
     else
     {
         throw new Exception("Money not accepted");
     }
 }
Ejemplo n.º 3
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");
     }
 }
Ejemplo n.º 4
0
        private static List <CashMoney> Calculate(List <CashMoney> coins, double change, int start = 0)
        {
            for (int i = start; i < coins.Count; i++)
            {
                CashMoney coin = coins[i];

                if (coin.Quantity > 0 && coin.MoneyValue <= change)
                {
                    double moneyValue = (Double)coin.MoneyValue;
                    double remainder  = change % moneyValue;
                    if (remainder < change)
                    {
                        double s        = (change - remainder) / moneyValue;
                        int    quantity = (Int32)coin.Quantity;
                        int    howMany  = (Int32)Math.Min(quantity, s);

                        List <CashMoney> matches = new List <CashMoney>();
                        matches.Add(new CashMoney(moneyValue, howMany));

                        double amount     = howMany * moneyValue;
                        double changeLeft = change - amount;
                        if (changeLeft == 0)
                        {
                            return(matches);
                        }

                        List <CashMoney> subCalc = Calculate(coins, changeLeft, i + 1);
                        if (subCalc != null)
                        {
                            matches.AddRange(subCalc);
                            return(matches);
                        }
                    }
                }
            }

            return(null);
        }