private void SetItemFields(IVendingItem item, string[] itemParts) { item.ItemSlot = itemParts[Col_SlotId]; item.ItemName = itemParts[Col_Name]; item.ItemPrice = Convert.ToDouble(itemParts[Col_Cost]); item.Stock = Default_Quantity; }
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); }
public List <IVendingItem> GetItems() { List <IVendingItem> items = new List <IVendingItem>(); try { using (StreamReader sr = new StreamReader("vendingmachine.csv")) { while (!sr.EndOfStream) { string stringItem = sr.ReadLine(); string[] itemParts = stringItem.Split('|'); IVendingItem item = GetItemFromItemPart(itemParts[Col_SlotId][0]); SetItemFields(item, itemParts); items.Add(item); } } } catch (IOException ex) { Console.WriteLine("Input error: " + ex); } return(items); }
private void PromptForPurchase() { Console.Write("Please enter the product code: "); string productChoice = Console.ReadLine(); IVendingItem item = vendingMachine.Items.Find(p => p.ItemSlot == productChoice); if (item != null) { if (item.Stock > 0) { if (item.ItemPrice <= vendingMachine.CurrentMoneyProvided) { vendingMachine.Purchase(item); items.Add(item); vendingMachine.SalesReport(item); } else { Console.WriteLine("\nPlease provide more money\n"); } } else { Console.WriteLine("\nItem out of stock\n"); //throw new ItemOutOfStockException("Item out of stock."); } } else { Console.WriteLine("Product was not found."); //throw new ProductNotFoundException("Product was not found."); } }
public void SalesReport(IVendingItem item) { if (File.Exists("SalesReport.txt")) { using (StreamReader sr = new StreamReader("SalesReport.txt")) { using (StreamWriter sw = new StreamWriter("TempSalesReport.txt")) { try { while (!sr.EndOfStream) { string stringItem = sr.ReadLine(); if (stringItem.Contains(item.ItemName)) { string[] itemParts = stringItem.Split('|'); int previousSold = Convert.ToInt32(itemParts[1]); int newAmount = (previousSold + 1); string newText = item.ItemName + "|" + newAmount; sw.WriteLine(newText); } else { if (stringItem.Contains("Total Sales")) { string newText = "Total Sales|" + Balance.ToString("C"); sw.WriteLine(newText); } else { sw.WriteLine(stringItem); } } } } catch (IOException ex) { Console.WriteLine("Error writing to file"); } } sr.Close(); } File.Delete("SalesReport.txt"); File.Move("TempSalesReport.txt", "SalesReport.txt"); File.Delete("TempSalesReport.txt"); } else { using (StreamWriter srr = new StreamWriter("SalesReport.txt")) { foreach (IVendingItem vendingItem in Items) { srr.WriteLine(vendingItem.ItemName + "|" + vendingItem.SoldItems); } srr.WriteLine("Total Sales" + "|" + Balance); } } }
public void Purchase(IVendingItem item) { item.Stock--; item.SoldItems++; CurrentMoneyProvided -= item.ItemPrice; Balance += item.ItemPrice; Transaction.PrintTransaction(item.ItemName, item.ItemSlot, CurrentMoneyProvided, CurrentMoneyProvided - item.ItemPrice); Console.WriteLine("Item purchase successful"); }