Exemplo n.º 1
0
        //Sets inventory from provided csv file
        public void GetInventory()
        {
            using (StreamReader sr = new StreamReader("C:\\Users\\Student\\workspace\\week-4-pair-exercises-c-team-6\\19_Capstone\\dotnet\\vendingmachine.csv"))
            {
                while (!sr.EndOfStream)                    //read to the end of the csv
                {
                    string   lines     = sr.ReadLine();    //reading lines of the csv
                    string[] temparray = lines.Split('|'); //splitting to an array

                    if (temparray.Contains("Chip"))
                    {
                        Chips chips = new Chips(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], chips);
                    }
                    if (temparray.Contains("Drink"))
                    {
                        Drinks drinks = new Drinks(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], drinks);
                    }
                    if (temparray.Contains("Candy"))
                    {
                        Candy candy = new Candy(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], candy);
                    }
                    if (temparray.Contains("Gum"))
                    {
                        Gum gum = new Gum(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], gum);
                    }

                    //adding the array to our list
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// stocks a machine with new items on creation
        /// </summary>
        public void StockMachine()
        {
            string directory = Environment.CurrentDirectory;
            string fileName  = @"\..\..\..\etc\vendingmachine.csv";
            string fullPath  = directory + fileName;

            using (StreamReader sr = new StreamReader(fullPath))
            {
                while (!sr.EndOfStream)
                {
                    string   fullLine = sr.ReadLine();
                    string   type     = "";
                    string[] lines    = fullLine.Split('|');
                    type = lines[3];
                    if (type == "Chip")
                    {
                        Chips chip = new Chips();
                        chip.Symbol   = lines[0];
                        chip.Name     = lines[1];
                        chip.Price    = decimal.Parse(lines[2]);
                        chip.Quantity = 5;
                        inventory.Add(chip);
                    }
                    else if (type == "Candy")
                    {
                        Candy candy = new Candy();
                        candy.Symbol   = lines[0];
                        candy.Name     = lines[1];
                        candy.Price    = decimal.Parse(lines[2]);
                        candy.Quantity = 5;
                        inventory.Add(candy);
                    }
                    else if (type == "Drink")
                    {
                        Drinks drink = new Drinks();
                        drink.Symbol   = lines[0];
                        drink.Name     = lines[1];
                        drink.Price    = decimal.Parse(lines[2]);
                        drink.Quantity = 5;
                        inventory.Add(drink);
                    }
                    else if (type == "Gum")
                    {
                        Gum gum = new Gum();
                        gum.Symbol   = lines[0];
                        gum.Name     = lines[1];
                        gum.Price    = decimal.Parse(lines[2]);
                        gum.Quantity = 5;
                        inventory.Add(gum);
                    }
                }
            }
        }