예제 #1
0
        public static List <VendingMachineSlot> ReadIn()
        {
            string filePath     = @"C:\VendingMachine\";
            string fileName     = "vendingmachine.csv";
            string fullFilePath = Path.Combine(filePath, fileName);

            List <VendingMachineSlot> slots = new List <VendingMachineSlot>();

            //Possible exceptions while reading!
            using (StreamReader sr = new StreamReader(fullFilePath))
            {
                while (!sr.EndOfStream)
                {
                    //read in each line
                    string lineIn = sr.ReadLine();

                    // parse slot name | item name | item price
                    string[] parsedLine = lineIn.Split('|');

                    //Check for errors.  Will throw exceptions if any errors are found
                    CheckReadInErrors(parsedLine, lineIn);

                    //Create slot name, item name and decimal price from parsed line
                    string slotName = parsedLine[0];
                    string itemName = parsedLine[1];
                    decimal.TryParse(parsedLine[2], out decimal itemPrice);

                    //Create a new slot containing the read in item and place it in the list of slots
                    VendingMachineSlot vms = new VendingMachineSlot(slotName);
                    vms.PlaceItemInSlot(new VendingMachineItem(itemName, itemPrice));
                    slots.Add(vms);
                }
            }
            return(slots);
        }
예제 #2
0
        /// <summary>
        /// Writes out list of products to console
        /// </summary>
        public void DisplayProducts()
        {
            // Display Header categories
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("SlotID" + "Snack".PadLeft(10) + "Price".PadLeft(21) + "Quantity".PadLeft(11));
            Console.WriteLine();

            // FOREACH loop per product in Inventory
            foreach (string key in this.VM.Inventory.Keys)
            {
                VendingMachineSlot vms     = this.VM.Inventory[key];
                Product            product = vms.HeldProduct;

                // IF this product is in stock
                if (vms.Quantity > 0)
                {
                    Console.WriteLine($"{key.ToUpper().PadLeft(4)} - {product.Name.PadRight(22)} - ${product.Price.ToString().PadRight(7):C2} - {vms.Quantity}");
                }

                // ELSE the product is SOLD OUT
                else
                {
                    Console.WriteLine($"{key.ToUpper().PadLeft(4)} - {product.Name.PadRight(22)} - ${product.Price.ToString().PadRight(7):C2}SOLD OUT");
                }
            }

            // Wait for user to continue
            Console.WriteLine();
            Console.WriteLine("Press any key to return to Menu.");
            Console.ReadKey();
        }
예제 #3
0
        /// <summary>
        /// Sets selected product ready for purchase.
        /// </summary>
        /// <param name="slotID">Slot ID of product.</param>
        public void GiveProduct(string slotID)
        {
            VendingMachineSlot vms = this.Inventory[slotID];
            Product product = vms.HeldProduct;

            if (this.CurrentBalance >= product.Price)
            {
                string logAction = product.Name + " " + slotID.ToUpper();

                vms.Quantity--;
                this.CurrentBalance -= product.Price;
                this.RW.LogAction(logAction, this.CurrentBalance + product.Price, this.CurrentBalance);
                this.ProductsPurchased.Add(product);
            }
        }
예제 #4
0
 private void Initialize()
 {
     SelectedSlot = new VendingMachineSlot();
     Slots        = new List <VendingMachineSlot>();
 }
예제 #5
0
        /// <summary>
        /// initiates menu to get product from user
        /// </summary>
        public void GetProduct()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Enter a slotID for the product you would like to purchase?");
                Console.WriteLine("(1) Display Products");
                Console.WriteLine("(Q) Quit to Purchase Menu");
                Console.WriteLine();
                Console.Write("> Slot ID: ");
                string choice = Console.ReadLine();

                // IF the user wants to return to purchase menu
                if (choice.ToLower() == "q")
                {
                    // BREAK out of GetProduct to return
                    break;
                }

                if (choice == "1")
                {
                    this.MM.DisplayProducts();
                    continue;
                }

                // IF the product does not exist in inventory
                if (!this.VM.Inventory.ContainsKey(choice))
                {
                    // THEN prompt user invalid input
                    Console.WriteLine();
                    Console.WriteLine("Invalid slotID. Press any key to return to Purchase Menu.");
                    Console.ReadKey();
                    return;
                }

                // Set VendingMachineSlot and product from user input
                VendingMachineSlot vms     = this.VM.Inventory[choice];
                Product            product = vms.HeldProduct;

                // IF product is SOLD OUT
                if (vms.Quantity < 1)
                {
                    // PROMPT user product SOLD OUT
                    Console.WriteLine();
                    Console.WriteLine("Item is SOLD OUT. Press any key to return to Purchase Menu.");
                    Console.ReadKey();
                    return;
                }

                // IF not enough money to purchase
                if (this.VM.CurrentBalance < product.Price)
                {
                    // PROMPT user not enough money
                    Console.WriteLine();
                    Console.WriteLine("Not enough money to purchase. Press any key to return to Purchase Menu.");
                    Console.ReadKey();
                    return;
                }

                // Purchase product successful
                this.VM.GiveProduct(choice);
                return;
            }
        }