public void LoadInventory() { string sourceFilePath = "vendingmachine.csv"; if (!Path.IsPathRooted(sourceFilePath)) { sourceFilePath = Path.Combine(Environment.CurrentDirectory, sourceFilePath); } try { using (StreamReader sr = new StreamReader(sourceFilePath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); char[] splitChar = { '|' }; string[] itemLoc = line.Split(splitChar); if (itemLoc[3] == Item.Candy) { Candy candy = new Candy(itemLoc[1], double.Parse(itemLoc[2])); InventoryItem item = new InventoryItem(candy); _inventory.Add(itemLoc[0], item); } if (itemLoc[3] == Item.Chips) { Chips chip = new Chips(itemLoc[1], double.Parse(itemLoc[2])); InventoryItem item = new InventoryItem(chip); _inventory.Add(itemLoc[0], item); } if (itemLoc[3] == Item.Beverage) { Beverage drink = new Beverage(itemLoc[1], double.Parse(itemLoc[2])); InventoryItem item = new InventoryItem(drink); _inventory.Add(itemLoc[0], item); } if (itemLoc[3] == Item.Gum) { Gum gum = new Gum(itemLoc[1], double.Parse(itemLoc[2])); InventoryItem item = new InventoryItem(gum); _inventory.Add(itemLoc[0], item); } } } } catch (Exception e) { throw new Exception(e.Message); } }
public void ReadFile(Dictionary <string, Product> mainMenu) { string directory = Directory.GetCurrentDirectory(); string filename = "vendingmachine.csv"; string path = Path.Combine(directory, filename); try { using (StreamReader sr = new StreamReader(path)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] productArray = line.Split('|'); slot = productArray[0]; name = productArray[1]; price = decimal.Parse(productArray[2]); type = productArray[3]; if (type == "Chip") { Chip chipName = new Chip(price, name, qty); mainMenu.Add(slot, chipName); } else if (type == "Candy") { Candy candyName = new Candy(price, name, qty); mainMenu.Add(slot, candyName); } else if (type == "Gum") { Gum gumName = new Gum(price, name, qty); mainMenu.Add(slot, gumName); } else if (type == "Drink") { Drink drinkName = new Drink(price, name, qty); mainMenu.Add(slot, drinkName); } } } }catch (Exception e) { } }
/// <summary> /// Stocks or vending machine using the csv file /// </summary> /// <param name="stock"></param> public static void StockVendingMachine(Dictionary <string, Item> stock) { // Directory and file name string directory = Environment.CurrentDirectory; string filename = "vendingmachine.csv"; // Full Path string fullPath = Path.Combine(directory, "..\\..\\..\\..\\etc", filename); // Here we'll read in the file, separate each word with a comma and store // the individual word in a collection. try { using (StreamReader sr = new StreamReader(fullPath)) { while (!sr.EndOfStream) { int maxAmount = (5); string line = sr.ReadLine(); string[] words = line.Split('|'); string snackType = words[3]; if (snackType == "Chip") { stock[words[0]] = new Chip(words[0], words[1], decimal.Parse(words[2]), words[3], maxAmount); } if (snackType == "Candy") { stock[words[0]] = new Candy(words[0], words[1], decimal.Parse(words[2]), words[3], maxAmount); } if (snackType == "Drink") { stock[words[0]] = new Drink(words[0], words[1], decimal.Parse(words[2]), words[3], maxAmount); } if (snackType == "Gum") { stock[words[0]] = new Gum(words[0], words[1], decimal.Parse(words[2]), words[3], maxAmount); } } } } catch (IOException e) { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } }
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; } }
public VendingMachine() { string directory = Environment.CurrentDirectory; string fileName = "vendingmachine.csv"; string fullPath = Path.Combine(directory, fileName); try { using (StreamReader sr = new StreamReader(fullPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] itemArray = line.Split('|'); if (itemArray[0].StartsWith("A")) { vendingDictionary[itemArray[0]] = new Chips(itemArray[1], 5, double.Parse(itemArray[2])); } else if (itemArray[0].StartsWith("B")) { vendingDictionary[itemArray[0]] = new Candy(itemArray[1], 5, double.Parse(itemArray[2])); } else if (itemArray[0].StartsWith("C")) { vendingDictionary[itemArray[0]] = new Soda(itemArray[1], 5, double.Parse(itemArray[2])); } else { vendingDictionary[itemArray[0]] = new Gum(itemArray[1], 5, double.Parse(itemArray[2])); } } } } catch (IOException e) { Console.WriteLine("There's an error!"); Console.WriteLine(e.Message); } }
public void Start() { //StreamReader to update vendingmachine parameters using (StreamReader sr = new StreamReader($"{fullpath}\\VendingMachine.txt")) { while (!sr.EndOfStream) { string line = sr.ReadLine().Replace("|", ", "); string[] product = line.Split(", "); //update string for Display() productData.Add($"{product[0]} {product[1]} {product[2]}"); //write dictionary values if (product[3] == "Gum") { Item foodProduct = new Gum(product[1], decimal.Parse(product[2])); Slot foodSlot = new Slot(foodProduct, 5); Inventory.Add(product[0], foodSlot); } else if (product[3] == "Chip") { Item foodProduct = new Chip(product[1], decimal.Parse(product[2])); Slot foodSlot = new Slot(foodProduct, 5); Inventory.Add(product[0], foodSlot); } else if (product[3] == "Drink") { Item foodProduct = new Drink(product[1], decimal.Parse(product[2])); Slot foodSlot = new Slot(foodProduct, 5); Inventory.Add(product[0], foodSlot); } else if (product[3] == "Candy") { Item foodProduct = new Candy(product[1], decimal.Parse(product[2])); Slot foodSlot = new Slot(foodProduct, 5); Inventory.Add(product[0], foodSlot); } } } }
/// <summary> /// Slot Constructor /// </summary> /// <param name="rawTextLine">String line seperated by pipe (|) characters</param> public Slot(string rawTextLine) { //set quantity to 5 Quantity = 5; //Split raw line text char[] splitPipe = { '|' }; string[] slotValueArray = rawTextLine.Split(splitPipe); //assign arrays to individual variables for ease of use string location = slotValueArray[0]; string name = slotValueArray[1]; double price = double.Parse(slotValueArray[2]); string foodType = slotValueArray[3]; //determine Food type and build corresponding item if (foodType == Food.Chip) { Chips holderChip = new Chips(location, name, price); SlotItem = holderChip; } else if (foodType == Food.Drink) { Beverage holderBeverage = new Beverage(location, name, price); SlotItem = holderBeverage; } else if (foodType == Food.Candy) { Candy holderCandy = new Candy(location, name, price); SlotItem = holderCandy; } else if (foodType == Food.Gum) { Gum holderGum = new Gum(location, name, price); SlotItem = holderGum; } }
//Uploads inventory file into Dictionary where key is the slot location public void GetInventoryMethod(string directoryPath, string fileToRead) { //directoryPath is the new inputFile Directory.SetCurrentDirectory(directoryPath); string wholeDocument = File.ReadAllText(fileToRead).Trim(); string[] eachLineArray = wholeDocument.Split("\r\n"); for (int i = 0; i < eachLineArray.Length; i++) { string currentLine = eachLineArray[i]; string[] currentLineArray = currentLine.Split("|"); Products product = null; if (currentLineArray[3] == "Chip") { product = new Chips(currentLineArray[1], Decimal.Parse(currentLineArray[2]), currentLineArray[3], currentLineArray[0], 5); } else if (currentLineArray[3] == "Candy") { product = new Candy(currentLineArray[1], Decimal.Parse(currentLineArray[2]), currentLineArray[3], currentLineArray[0], 5); } else if (currentLineArray[3] == "Gum") { product = new Gum(currentLineArray[1], Decimal.Parse(currentLineArray[2]), currentLineArray[3], currentLineArray[0], 5); } else if (currentLineArray[3] == "Drink") { product = new Drink(currentLineArray[1], Decimal.Parse(currentLineArray[2]), currentLineArray[3], currentLineArray[0], 5); } inventory.Add(currentLineArray[0].ToString(), product); } }
/// <summary> /// Initializes new instance of VendingMachine. /// </summary> public VendingMachine() { this.Balance = 0.0M; using (StreamReader sr = new StreamReader("vendingmachine.csv")) { while (!sr.EndOfStream) { // string array of all read in values string[] items = sr.ReadLine().Split("|"); // create new item object with read in values (slot location, name, price, type) // Item item = new Item(items[0], items[1], decimal.Parse(items[2]), items[3]); if (items[3] == "Gum") { Item item = new Gum(items[0], items[1], decimal.Parse(items[2]), items[3]); this.Stock[items[0]] = item; } else if (items[3] == "Drink") { Item item = new Drink(items[0], items[1], decimal.Parse(items[2]), items[3]); this.Stock[items[0]] = item; } else if (items[3] == "Chip") { Item item = new Chip(items[0], items[1], decimal.Parse(items[2]), items[3]); this.Stock[items[0]] = item; } else if (items[3] == "Candy") { Item item = new Candy(items[0], items[1], decimal.Parse(items[2]), items[3]); this.Stock[items[0]] = item; } } } }
public VendingMachine(string file) { Inventory = new Dictionary <string, Product> { }; // Sets up File to read in to the inventory from a txt file string directory = Environment.CurrentDirectory; string inventoryFile = file; string inventoryFullPath = Path.Combine(directory, inventoryFile); using (StreamReader sr = new StreamReader(inventoryFullPath)) { while (!sr.EndOfStream) { Product prod = null; string line = sr.ReadLine(); string[] breaks = line.Split("|"); string slot = breaks[0]; string productName = breaks[1]; string productPrice = breaks[2]; string productType = breaks[3]; // Check to see if we get a vaild price of a product if (!double.TryParse(productPrice, out double result)) { //Log.AddTransactiontoLog($"{slot}: Bad Inventory Input: Price needs to be decimal", 0, this.Balance); continue; } // Make sure we did not already have in item in that location if (Inventory.ContainsKey(slot)) { //Log.AddTransactiontoLog($"{slot}: Bad Inventory Input: This Slot is already filled with another product!", 0, this.Balance); continue; } // Checks type and add the correct type to the inventory if (productType.Contains("Chip")) { prod = new Chip(productName, double.Parse(productPrice)); } else if (productType.Contains("Drink")) { prod = new Drink(productName, double.Parse(productPrice)); } else if (productType.Contains("Gum")) { prod = new Gum(productName, double.Parse(productPrice)); } else if (productType.Contains("Candy")) { prod = new Candy(productName, double.Parse(productPrice)); } else { // We did not get a valid input so we report and move on //Log.AddTransactiontoLog($"{slot}:Bad Inventory Input: No Product of this type exists.", 0, this.Balance); continue; } Inventory.Add(slot, prod); } Report.SetupReportWithInventory(Inventory); } }
public Dictionary <string, List <VendingItem> > StockNewVendingMachine(string filePath) { Dictionary <string, List <VendingItem> > initialStock = new Dictionary <string, List <VendingItem> >(); if (!File.Exists(filePath)) { throw new Exception("File path isn't valid."); } try { using (StreamReader sr = new StreamReader(filePath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] vendItemProperties = line.Split(delimiters); if (vendItemProperties.Length != 3) { throw new Exception("A vending item is not formatted correctly: " + line); } // This assumes all input files will be coming in with the format(Location|Product_Name|Price) string productLocation = vendItemProperties[SlotID]; string productName = vendItemProperties[ProductName]; bool validCost = decimal.TryParse(vendItemProperties[Cost], out decimal productCost); if (!validCost) { throw new Exception("The file had an invalid cost: " + line); } switch (productLocation[0]) { case 'A': // chips Chip ch = new Chip(productCost, productName); List <VendingItem> fiveOfChipCH = new List <VendingItem> { ch, ch, ch, ch, ch }; initialStock[productLocation] = fiveOfChipCH; break; case 'B': //candy Candy ca = new Candy(productCost, productName); List <VendingItem> fiveOfCandyCA = new List <VendingItem> { ca, ca, ca, ca, ca }; initialStock[productLocation] = fiveOfCandyCA; break; case 'C': // drinks Drink dr = new Drink(productCost, productName); List <VendingItem> fiveOfDrinkDR = new List <VendingItem> { dr, dr, dr, dr, dr }; initialStock[productLocation] = fiveOfDrinkDR; break; case 'D': // gum Gum g = new Gum(productCost, productName); List <VendingItem> fiveOfGumG = new List <VendingItem> { g, g, g, g, g }; initialStock[productLocation] = fiveOfGumG; break; default: break; } } } } catch (IOException e) { Console.WriteLine("StreamReader encountered a problem: " + e.Message); } return(initialStock); }