private IVendingItem GetItemFromItemPart(char firstCharacter) { IVendingItem item = null; switch (firstCharacter) { case 'A': item = new Chips(); break; case 'B': item = new Candy(); break; case 'C': item = new Drink(); break; case 'D': item = new Gum(); break; } return(item); }
// string is going to equal the itemCode // VendingItem will equal all the other values public Dictionary <string, VendingItem> ImportingVendingItems() { Dictionary <string, VendingItem> vendingItems = new Dictionary <string, VendingItem>(); string directory = @"..\..\..\.."; 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[] itemSection = line.Split("|"); string itemName = itemSection[1]; decimal itemPrice = decimal.Parse(itemSection[2]); int itemsRemaining = 5; VendingItem itemType; // call new variable itemType switch (itemSection[3]) // switch out type with all the values { case "Beverages": itemType = new Beverages(itemName, itemPrice, itemsRemaining); break; case "Candy": itemType = new Candy(itemName, itemPrice, itemsRemaining); break; case "Chips": itemType = new Chips(itemName, itemPrice, itemsRemaining); break; case "Gum": itemType = new Gum(itemName, itemPrice, itemsRemaining); break; default: // don't forget this itemType = new Gum(itemName, itemPrice, itemsRemaining); break; } vendingItems.Add(itemSection[0], value: itemType); } } } catch { Console.WriteLine("Can't import CSV."); } return(vendingItems); // updated dictionary }
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 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); } }
/// <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); } }