//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 } } }
/// <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); } } } }
//adds items to inventoryItems from file public void AddStartingInventory() { try { //Read inventory file from file provided using (StreamReader sr = new StreamReader(path)) { while (!sr.EndOfStream) // read through input file { string item = sr.ReadLine(); string[] fields = item.Split("|"); decimal cost = decimal.Parse(fields[2]); string slot = fields[0]; string name = fields[1]; string type = fields[3]; Products p = null; //Creates a new object of candy, chips, drink or gum if (type == ("Candy")) { p = new Candy(name, cost, slot, type); } else if (type == ("Chip")) { p = new Chips(name, cost, slot, type); } else if (type == ("Drink")) { p = new Drink(name, cost, slot, type); } else if (type == "Gum") { p = new Gum(name, cost, slot, type); } //adds object to inventory items list with key of slot and product with all information inventoryItems.Add(p.ProductSlot, p); } } } catch (Exception) { Console.WriteLine($"ERROR! Invalid {path} path."); } }