예제 #1
0
        public void RunPurchase(VendingMachine VendoMatic600, string sLogPath)
        {
            Console.Write("Please Select Your Product by ID: ");
            bool   bIsValidProductNumber = false;
            bool   bIsValidProductLetter = false;
            string sInput = Console.ReadLine();

            bIsValidProductNumber = int.TryParse(sInput.Substring(1, 1), out int nNumber);
            bIsValidProductLetter = sInput.Substring(0, 1).ToUpper() == "A" ||
                                    sInput.Substring(0, 1).ToUpper() == "B" ||
                                    sInput.Substring(0, 1).ToUpper() == "C" ||
                                    sInput.Substring(0, 1).ToUpper() == "D";

            while (!bIsValidProductNumber || !bIsValidProductLetter || nNumber < 1 || nNumber > 4)
            {
                Console.WriteLine("Please enter a valid Product ID...   Format : LetterNumber");
                sInput = Console.ReadLine();
                bIsValidProductNumber = int.TryParse(sInput.Substring(1, 1), out nNumber);
                bIsValidProductLetter = sInput.Substring(0, 1).ToUpper() == "A" ||
                                        sInput.Substring(0, 1).ToUpper() == "B" ||
                                        sInput.Substring(0, 1).ToUpper() == "C" ||
                                        sInput.Substring(0, 1).ToUpper() == "D";
            }
            sInput = sInput.Substring(0, 1).ToUpper() + sInput.Substring(1, 1);

            if (VendoMatic600.GetSnackItems()[sInput].nAmount > 0)
            {
                VendoMatic600.Balance -= VendoMatic600.GetSnackItems()[sInput].dPrice;
                VendoMatic600.DecrementAmountOfSnack(VendoMatic600.GetSnackItems()[sInput], 1);
                Console.WriteLine(VendoMatic600.DispenseItem(VendoMatic600.GetSnackItems()[sInput]));
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("We Don't Have Any More, Go Away.");
            }
            using (StreamWriter newSW = new StreamWriter(sLogPath, true))
            {
                //write Date & Time
                newSW.Write(DateTime.Now.ToString() + " ");
                //write amount entered
                newSW.Write(VendoMatic600.GetSnackItems()[sInput].sName + " ");
                newSW.Write(VendoMatic600.GetSnackItems()[sInput].sLocation + " ");
                newSW.Write("$" + string.Format("{0:0.00}", VendoMatic600.GetSnackItems()[sInput].dPrice) + " ");
                //write balance
                newSW.Write($"${string.Format("{0:0.00}", VendoMatic600.Balance)}\n");
            }
        }