예제 #1
0
        /// <summary>
        /// The vendingmachine.csv is read line by line and applied to an array seperated by "|".
        /// The resulting strings of the idexes in the array are applied to a corresponding dictionary and/or list.
        /// </summary>
        public void Load()
        {
            string filePath = @"..\..\..\..\vendingmachine.csv";

            using (StreamReader itemString = new StreamReader(filePath))
            {
                while (!itemString.EndOfStream)
                {
                    string   itemLines    = itemString.ReadLine();
                    string[] itemArray    = itemLines.Split("|");
                    string   itemLocation = itemArray[0];
                    string   itemName     = itemArray[1];
                    decimal  itemPrice    = decimal.Parse(itemArray[2]);
                    string   itemType     = itemArray[3];

                    switch (itemType)
                    {
                    case "Chip":
                        Chips chips = new Chips(itemName, itemPrice);
                        vendingStock.Add(itemLocation, chips);
                        Slots chipSlot = new Slots(itemLocation);
                        slotList.Add(chipSlot);
                        break;

                    case "Candy":
                        Candy candy = new Candy(itemName, itemPrice);
                        vendingStock.Add(itemLocation, candy);
                        Slots candySlot = new Slots(itemLocation);
                        slotList.Add(candySlot);
                        break;

                    case "Drink":
                        Drink drink = new Drink(itemName, itemPrice);
                        vendingStock.Add(itemLocation, drink);
                        Slots drinkSlot = new Slots(itemLocation);
                        slotList.Add(drinkSlot);
                        break;

                    case "Gum":
                        Gum gum = new Gum(itemName, itemPrice);
                        vendingStock.Add(itemLocation, gum);
                        Slots gumSlot = new Slots(itemLocation);
                        slotList.Add(gumSlot);
                        break;
                    }
                }
            }
        }
예제 #2
0
        public void Restock()
        {
            //set path to get file from
            string path = @"..\..\..\..\vendingmachine.csv";


            using (StreamReader sr = new StreamReader(path))
            {
                while (!sr.EndOfStream)
                {
                    //read line by line
                    string singleItem = sr.ReadLine();

                    //split by the | delimter to array
                    string[] singleItemContents = singleItem.Split("|");

                    //reference by index and store values in temp variable
                    string  tempName     = singleItemContents[1];
                    string  tempSlot     = singleItemContents[0];
                    string  tempItemType = singleItemContents[3];
                    decimal tempPrice    = decimal.Parse(singleItemContents[2]);

                    //create a new object to assign the values
                    if (tempItemType == "Chip")
                    {
                        Chip tempChip = new Chip(tempName, tempSlot, tempPrice);
                        Inventory.Add(tempSlot, tempChip);
                    }
                    if (tempItemType == "Drink")
                    {
                        Drink tempDrink = new Drink(tempName, tempSlot, tempPrice);
                        Inventory.Add(tempSlot, tempDrink);
                    }
                    if (tempItemType == "Gum")
                    {
                        Gum tempGum = new Gum(tempName, tempSlot, tempPrice);
                        Inventory.Add(tempSlot, tempGum);
                    }
                    if (tempItemType == "Candy")
                    {
                        Candy tempCandy = new Candy(tempName, tempSlot, tempPrice);
                        Inventory.Add(tempSlot, tempCandy);
                    }
                }
            }
        }
        public VendingMachine(string path)
        {
            Products = new List <Product>();
            using (StreamReader sr = new StreamReader(path))
            {
                while (!sr.EndOfStream)
                {
                    string   item           = sr.ReadLine();
                    string[] individualItem = item.Split("|");
                    string   slot           = individualItem[0];
                    string   name           = individualItem[1];
                    decimal  cost           = decimal.Parse(individualItem[2]);

                    switch (individualItem[3])
                    {
                    case "Chip":
                        Chip chip = new Chip(slot, name, cost);
                        Products.Add(chip);
                        break;

                    case "Candy":
                        Candy candy = new Candy(slot, name, cost);
                        Products.Add(candy);
                        break;

                    case "Drink":
                        Drink drink = new Drink(slot, name, cost);
                        Products.Add(drink);
                        break;

                    case "Gum":
                        Gum gum = new Gum(slot, name, cost);
                        Products.Add(gum);
                        break;
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Stock machine from a string array.
        /// </summary>
        /// <param name="inputLines">Pipe-delimited string array.</param>
        /// <returns>Success of completely stocking the machine.</returns>
        public bool Stock(string[] inputLines)
        {
            foreach (string line in inputLines)
            {
                string identifier   = "";
                string nameProduct  = "";
                string priceProduct = "";
                string productClass = "";
                try
                {
                    string[] stockElements = line.Split("|");
                    identifier   = stockElements[0].ToUpper();
                    nameProduct  = stockElements[1];
                    priceProduct = stockElements[2];
                    productClass = stockElements[3];
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"An error occurred stocking the machine: {ex.Message}");
                    Console.ReadKey();
                    return(false);
                }

                bool priceWasParsed = decimal.TryParse(priceProduct, out decimal priceDecimal);

                if (priceWasParsed)
                {
                    // Find if Product already exists
                    Product product = null;
                    if (products.ContainsKey(nameProduct))
                    {
                        product = products[nameProduct];
                    }
                    if (product == null)
                    {
                        // Create new product
                        switch (productClass)
                        {
                        case "Chip":
                            product = new Chip(nameProduct);
                            break;

                        case "Drink":
                            product = new Beverage(nameProduct);
                            break;

                        case "Gum":
                            product = new Gum(nameProduct);
                            break;

                        case "Candy":
                            product = new Candy(nameProduct);
                            break;
                        }
                        products[nameProduct] = product;
                    }

                    // Create slot and put product in it
                    slots[identifier] = new Slot(identifier, product, priceDecimal);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }