コード例 #1
0
        public void PurchaseItem(string slotSelected, VendingMachineCustomer myCustomer)
        {
            string message = "";

            foreach (VendingMachineItem vmi in CurrentInventory)
            {
                if (vmi.SlotLocation == slotSelected)
                {
                    if (vmi.Quantity > 0)
                    {
                        if (vmi.Price <= myCustomer.Balance)
                        {
                            myCustomer.Balance -= vmi.Price;    // deduct from customer balance
                            vmi.Quantity--;                     // deduct from inventory qty
                            DailySales += vmi.Price;            // add to machine daily sales
                            ItemsSoldToday.Add(vmi);
                            // logs in audit file when an item has been purchased
                            // logs date, time, item name, item slot, item price, customer balance
                            PrintToAuditFile(DateTime.Now.ToString() + " " + vmi.Name + " " + vmi.SlotLocation + " $" + vmi.Price + " $" + myCustomer.Balance);

                            Console.WriteLine(VendItem(vmi));
                            message = "Item sold.";
                            break;
                        }
                        else
                        {
                            message = "***Insufficient funds.***";
                            break;
                        }
                    }
                    else
                    {
                        message = "***Item sold out.***";
                        break;
                    }
                }
                else
                {
                    message = "***NOT A VALID SELECTION***";
                }
            }
            Console.WriteLine(message);
        }
コード例 #2
0
        public string DispenseChange(VendingMachineCustomer vmi)
        {
            Console.WriteLine("Balance: " + vmi.Balance);
            string giveChange = "Your change is ";   //6.35

            if (vmi.Balance > 0)
            {
                int numQuarters = (int)(vmi.Balance / 0.25M);   //25
                vmi.Balance -= (decimal)(numQuarters * 0.25M);
                int numDimes = (int)(vmi.Balance / 0.10M);
                vmi.Balance -= (decimal)(numDimes * 0.10M);
                int numNickels = (int)(vmi.Balance / 0.05M);
                vmi.Balance -= (decimal)(numNickels * 0.05M);

                if (numQuarters != 0)
                {
                    giveChange += numQuarters + " quarter(s) ";
                }
                if (numDimes != 0)
                {
                    giveChange += numDimes + " dime(s) ";
                }
                if (numNickels != 0)
                {
                    giveChange += numNickels + " nickel(s)";
                }
                // log to audit file when a customer finishes a transaction
                // logs date, time, amount of change, customer balance (should be zero)
                PrintToAuditFile(DateTime.Now + " GIVE CHANGE: $" + ((numQuarters * 0.25M) + (numDimes * 0.10M) + (numNickels * 0.05M)) + " $" + vmi.Balance);
            }
            else
            {
                giveChange += "0";
            }

            return(giveChange);
        }