public StockService() { string productFile = @"../../../etc/vendingmachine.csv"; bool productFileExists = File.Exists(productFile); if (productFileExists) { try { List <string> productStreamList = new List <string>(); // Get products from a stream and save them to productStreamList using (StreamReader sR = new StreamReader(productFile)) { while (!sR.EndOfStream) { string productStream = sR.ReadLine(); productStreamList.Add(productStream); } } //Go thru productStreamList and creat products foreach (var productStreamLine in productStreamList) { List <string> productProps = productStreamLine.Split('|').ToList(); Product newProduct = null; switch (productProps[0].Substring(0, 1)) { case "A": newProduct = new Chip(productProps[0], productProps[1], double.Parse(productProps[2]), 5); break; case "B": newProduct = new Candy(productProps[0], productProps[1], double.Parse(productProps[2]), 5); break; case "C": newProduct = new Drinks(productProps[0], productProps[1], double.Parse(productProps[2]), 5); break; case "D": newProduct = new Gum(productProps[0], productProps[1], double.Parse(productProps[2]), 5); break; } Products.Add(newProduct); } } catch (Exception e) { Console.WriteLine("Sorry...Vendo is currently experienceing some technical diffculties:"); Console.WriteLine(e.Message); } } else { Console.WriteLine("Vendo-Matic 500 down for Maintenance"); } }
public VendingMachine() { Balance = 0.00M; ReadError = ""; WriteError = ""; try { using (StreamReader sr = new StreamReader(Path.Combine(filePath, inFile))) // Read input file and stock machine, i.e., populate list(items) with all snacks { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] snackData = line.Split('|'); if (snackData[0][0] == 'A') { Chips chips = new Chips(snackData); items.Add(chips); } else if (snackData[0][0] == 'B') { Candy candy = new Candy(snackData); items.Add(candy); } else if (snackData[0][0] == 'C') { Drinks drink = new Drinks(snackData); items.Add(drink); } else if (snackData[0][0] == 'D') { Gum gum = new Gum(snackData); items.Add(gum); } } } } catch (IOException e) // Catch file read error { ReadError = "Error stocking the vending machine.\n" + e.Message; } }