private void DisplayInventory() { foreach (var slot in vendingMachine.Slots) { VendingMachineItem item = vendingMachine.GetItemAtSlot(slot); if (item.ItemStock > 0) { Console.WriteLine($"{slot} | {item.ItemName} | {item.Price} | {item.ItemStock}"); } else { Console.WriteLine($"{slot} | {item.ItemName} | {item.Price} | Out of Stock"); } } Console.WriteLine(); }
public VendingMachineItem BuyItem(string code, TransactionSession session) { VendingMachineItem wantedItem = null; foreach (VendingMachineItem item in items) { if (item.ItemCode == code) { decimal price = item.Price; session.Deduct(price); item.NumberInStock--; wantedItem = item; } } return(wantedItem); }
public void Initialize() { try { using (StreamWriter writer = new StreamWriter(logPath, false)) { } } catch { } try { using (StreamReader reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); VendingMachineItem newItem = null; if (line.StartsWith('A')) { newItem = new Item_Chips(line); } else if (line.StartsWith('B')) { newItem = new Item_Candy(line); } else if (line.StartsWith('C')) { newItem = new Item_Beverages(line); } else if (line.StartsWith('D')) { newItem = new Item_Gum(line); } items.Add(newItem); } } } catch (Exception e) // catch Exceptions { Console.WriteLine(e.Message); } }
public void ListUpdate(string slot, decimal money) { foreach (VendingMachineItem item in items) { if (item.SlotLocation == slot) { noMoney = false; currItem = item; if (item.StartingQuantity == "SOLD OUT") { item.isSoldout = true; } if (money > item.Price && !item.isSoldout) { int quantity = Int32.Parse(item.StartingQuantity); quantity -= 1; if (quantity == 0) { soldout = true; item.StartingQuantity = "SOLD OUT"; } else { item.StartingQuantity = quantity.ToString(); } money -= item.Price; totalSales += item.Price; userMoney = money; Munchy(item.SlotLocation); WriteLog($"{DateTime.Now} {item.ProductName} {item.SlotLocation} ${(userMoney + item.Price)} ${userMoney}"); } else if (money > item.Price && soldout) { } else { noMoney = true; } } else { } } }
public double MakeSelection(string product, double newBalance, VendingMachineItem entry, double totalSales) { try { if (newBalance >= entry.Price && entry.Quantity >= 1) { newBalance -= entry.Price; totalSales += entry.Price; entry.Quantity -= 1; totalSale = totalSales; Console.WriteLine(); Console.WriteLine("Vending!"); Console.WriteLine(entry.DisplayMessage()); Console.WriteLine(); } else if (entry.Quantity < 1) { Console.WriteLine(); Console.WriteLine("Sold Out!"); Console.WriteLine("Please make another selection"); Console.WriteLine(); } else { Console.WriteLine(); Console.WriteLine("Please insert more money!"); Console.WriteLine(); } } catch (Exception e) { Console.WriteLine(); Console.WriteLine("Entry not valid, please try again!"); Console.WriteLine(); } balance = newBalance; return(balance); }
public List <VendingMachineItem> items = new List <VendingMachineItem>(); //needs populated when file is uploaded public VendingMachine() { string filePath = @"C:\VendingMachine\vendingmachine.csv"; using (StreamReader sr = new StreamReader(filePath)) { while (!sr.EndOfStream) { VendingMachineItem newItem = new VendingMachineItem(); string line = sr.ReadLine(); string[] props = line.Split('|'); newItem.Slot = props[0]; newItem.ProductName = props[1]; newItem.Price = decimal.Parse(props[2]); items.Add(newItem); } } }
public void ReadTheInventory() { string directory = Environment.CurrentDirectory; string filename = "VendingMachine.txt"; string fullPath = Path.Combine(directory, filename); using (StreamReader sr = new StreamReader(fullPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); //store inventory in memory string[] ar = line.Split("|"); //create a vending machine item for each line VendingMachineItem item = new VendingMachineItem(ar[0], ar[1], double.Parse(ar[2]), ar[3]); vendingDictionary.Add(ar[0], item); } } }
public void WriteTransaction(VendingMachineTransaction transaction, decimal currentBalance) { using (StreamWriter sw = new StreamWriter(LogFile, true)) { switch (transaction.Type) { case TransactionType.MachineStart: sw.WriteLine(); goto default; case TransactionType.PurchaseItem: VendingMachineItem item = transaction.Item; sw.WriteLine($"{transaction.Timestamp} {item.Name} {(char)item.Type}{item.Slot} {transaction.Amount.ToString("C")}\t\t{currentBalance.ToString("C")}"); break; default: sw.WriteLine($"{transaction.Timestamp} {transaction.Type} {transaction.Amount.ToString("C")}\t\t{currentBalance.ToString("C")}"); break; } } }
public int StockMachine() { string filePath = @"C:\VendingMachine\vendingmachine.csv"; int count = 0; using (StreamReader reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] values = line.Split('|'); string slotLocation = values[0]; string productName = values[1]; decimal productPrice = decimal.Parse(values[2]); VendingMachineItem vendingMachineItem = new VendingMachineItem(slotLocation, productName, productPrice); items.Add(vendingMachineItem); count++; } } return(count); }
private static void DisplayInventory(VendingMachine vendoMatic500) { Console.Write("\nSlot_Location", -10); Console.Write("\tName", -20); Console.Write("\t\t Price", -6); Console.Write(" Amount_Left", -6); Console.Write("\t Type\n"); //Gets all the slots you can work with string[] slots = vendoMatic500.Slots; foreach (string slot in slots) { VendingMachineItem item = vendoMatic500.GetItemAtSlot(slot); //Logic to color-code based on type if (item.ProductType == "Chip") { Console.ForegroundColor = ConsoleColor.DarkMagenta; } if (item.ProductType == "Candy") { Console.ForegroundColor = ConsoleColor.DarkYellow; } if (item.ProductType == "Drink") { Console.ForegroundColor = ConsoleColor.DarkCyan; } if (item.ProductType == "Gum") { Console.ForegroundColor = ConsoleColor.Green; } Console.Write($" {item.SlotLocation,-14}"); Console.Write($"{item.ProductName,-20}"); Console.Write($"${item.Price,-10}"); Console.Write($"{item.Quantity.ToString(),-11}"); Console.Write(item.ProductType); Console.WriteLine(); } }
//should be fully testable, no read or write lines //userinterface will not be testable //contrctor to read file and intialize vending machine public bool ReadFile() { bool result = false; string path = Path.Combine(filePath, fileName); try { using (StreamReader sr = new StreamReader(path)) { items.Clear(); while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] snackInfo = line.Split('|'); VendingMachineItem vendingMachineItem = new VendingMachineItem(); vendingMachineItem.SlotLocation = snackInfo[0]; vendingMachineItem.SnackName = snackInfo[1]; try { vendingMachineItem.Price = decimal.Parse(snackInfo[2]); result = true; } catch { //TODO ask john if we will be passed non decimal price vendingMachineItem.Price = 0.00M; } vendingMachineItem.Count = 5; items.Add(vendingMachineItem); } } } catch { result = false; } return(result); }
public void FillInventory() { // read txt file // set object properties from read-in data try { string currentDirectory = Environment.CurrentDirectory; string inventoryFile = "Inventory.txt"; string fullInventoryPath = Path.Combine(currentDirectory, @"..\..\..\..\..\Example Files", inventoryFile); using (StreamReader sr = new StreamReader(fullInventoryPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] lineArray = line.Split("|"); VendingMachineItem vmi = new VendingMachineItem { Category = lineArray[3], Price = Decimal.Parse(lineArray[2]), SlotLocation = lineArray[0], Name = lineArray[1] }; CurrentInventory.Add(vmi); } } } catch (IOException e) { Console.WriteLine("File IO error."); } catch (Exception e) { Console.WriteLine("Application error."); } }
public void RunInterface() { bool done = false; UIAction action = UIAction.DisplayMainMenu; while (!done) { uiManager.CurrentBalance = vendingMachine.CurrentBalance; VendingMachineTransaction transaction; try { switch (action) { case UIAction.DisplayMainMenu: action = uiManager.PrintMainMenu(); break; case UIAction.DisplayPurchasing: action = uiManager.PrintPurchasingMenu(); break; case UIAction.FeedMoney: int amount = uiManager.FeedMoneyRequest(); transaction = vendingMachine.FeedMoney(amount); dataManager.WriteTransaction(transaction, vendingMachine.CurrentBalance); uiManager.CurrentBalance = vendingMachine.CurrentBalance; uiManager.PrintTransaction(transaction); action = UIAction.DisplayPurchasing; break; case UIAction.ReviewItems: action = uiManager.PrintProductSelectionMenu(vendingMachine.GetAllItems(), UIAction.CheckItem); break; case UIAction.DisplayItems: action = uiManager.PrintProductSelectionMenu(vendingMachine.GetAllItems(), UIAction.PurchaseItem); break; case UIAction.CheckItem: VendingMachineItem item = null; item = vendingMachine.CheckItem(uiManager.CurrentType, uiManager.CurrentSlot); uiManager.PrintItemCheck(item); action = UIAction.DisplayMainMenu; break; case UIAction.PurchaseItem: transaction = vendingMachine.PurchaseItem(uiManager.CurrentType, uiManager.CurrentSlot); dataManager.WriteTransaction(transaction, vendingMachine.CurrentBalance); uiManager.CurrentBalance = vendingMachine.CurrentBalance; uiManager.PrintTransaction(transaction); action = UIAction.DisplayPurchasing; break; case UIAction.FinishTransaction: transaction = vendingMachine.FinishTransaction(); dataManager.WriteTransaction(transaction, vendingMachine.CurrentBalance); uiManager.PrintTransaction(transaction); action = UIAction.DisplayMainMenu; break; case UIAction.Exit: done = true; break; case UIAction.SalesReport: action = UIAction.DisplayMainMenu; // I can't decide between this Query syntax or that Lambda syntax.. //var items = from itemArray in vendingMachine.GetAllItems().Values // from item in itemArray // where item != null // select item; IEnumerable <VendingMachineItem> items = vendingMachine.GetAllItems().Values .SelectMany(x => x).Where(x => x != null); dataManager.GenerateSalesReport(items.ToList()); uiManager.PrintTransaction(new VendingMachineTransaction(TransactionType.GenerateSalesReport)); break; default: action = UIAction.DisplayMainMenu; break; } } catch (Exception e) { uiManager.PrintException(e); action = UIAction.DisplayMainMenu; } } Console.ForegroundColor = ConsoleColor.Gray; Console.SetCursorPosition(1, Console.WindowHeight - 1); }
public VendingMachineItem GetItemAtSlot(string slotId) { List <VendingMachineItem> itemWantedList = new List <VendingMachineItem>(); List <VendingMachineItem> ourSlotDevice = null; try { ourSlotDevice = Inventory[slotId]; string price = ourSlotDevice[0].Price.ToString(); string name = ourSlotDevice[0].Name; string[] nameArray = new string[] { name, price }; this.slots = nameArray; itemWantedList = Inventory[slotId]; if (itemWantedList[0] == null) { throw new Exception(); } else if (itemWantedList[0].Price <= curerntBalance && itemWantedList.Count > 0) { VendingMachineItem item = itemWantedList[0]; curerntBalance -= itemWantedList[0].Price; itemWantedList.RemoveAt(0); return(item); } else if (itemWantedList[0].Price > curerntBalance) { throw new Exception(); } else { return(null); } } catch (Exception ex) { if (ourSlotDevice == null) { InvalidSlotIdException x = new InvalidSlotIdException("This slot does not exist", ex); { Console.Beep(); Tools.ColorfulWriteLine("This slot does not exist", ConsoleColor.Red); } } else if (itemWantedList.Count == 0) { Console.Beep(); OutOfStockException x = new OutOfStockException("This item is out of stock", ex); Tools.ColorfulWriteLine("This item is out of stock", ConsoleColor.Red); } else if (itemWantedList[0].Price > curerntBalance) { Console.Beep(); InsuffiecientFundsException x = new InsuffiecientFundsException("You do not have enough money", ex); Tools.ColorfulWriteLine("You do not have enough money", ConsoleColor.Red); } return(null); } }
public VendingMachineItem GetItemAtSlot(string slot) { VendingMachineItem itemSlot = Inventory[slot][0]; return(itemSlot); }
public void Display() { while (true) { Console.WriteLine(); Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine(); string input = Console.ReadLine(); if (input == "1") { try { Console.WriteLine("How many dollars would you like to insert?"); int fedMoney = Int32.Parse(Console.ReadLine()); logger.RecordDeposit(fedMoney, (VM.CurrentBalance + fedMoney)); VM.FeedMoney(fedMoney); } catch (FormatException) { Console.Beep(); Tools.ColorfulWriteLine("Please only enter a whole Dollar Value", ConsoleColor.Red); } } else if (input == "2") { Console.WriteLine("Please select your product."); Console.WriteLine(); string slotId = Console.ReadLine().ToUpper(); VendingMachineItem productSelected = VM.GetItemAtSlot(slotId); if (productSelected != null) { totalProductsSelected.Add(productSelected); logger.RecordPurchase(productSelected.Name, slotId, VM.CurrentBalance, VM.CurrentBalance - productSelected.Price); } } else if (input == "3") { change = VM.returnChange(); Console.WriteLine("Your change is:"); Console.WriteLine(change.Quarters + " in quarters, " + change.Dimes + " in dimes, " + change.Nickels + " in nickels"); Console.WriteLine(); logger.RecordCompleteTransaction(change.TotalChange, VM.CurrentBalance); for (int i = 0; i < totalProductsSelected.Count; i++) { Console.WriteLine(totalProductsSelected[i].Consume()); } break; } else { Console.WriteLine("You did not enter a valid option?"); } Console.WriteLine(VM.CurrentBalance.ToString("C")); } }
public void DecreaseQuantity(string slotID) { VendingMachineItem item = inventory[slotID]; item.QuantityRemaining -= 1; }
// Method called by UserInterface to display inventory list, update selected item, and return an action. public UIAction PrintProductSelectionMenu(Dictionary <ItemType, VendingMachineItem[]> items, UIAction actionToTake) { PrintBalance(); int selectedRow = 0; int selectedColumn = 0; int totalRows = 19; // 20 rows, indexed at 0. int totalColumns = 1; // 2 columns, indexed at 0. int tablePadding = 6; int currentRow = 0; VendingMachineItem selectedItem = null; bool isCurrentlyInMenu = true; ConsoleKeyInfo keyPress; while (isCurrentlyInMenu) { SetCursorPosition(tablePadding * 3, 0); switch (actionToTake) { case UIAction.CheckItem: SetColor(DarkGray); Write("This is where the things are! Check'em out!"); break; case UIAction.PurchaseItem: SetColor(Gray); Write("This is where the food lives! Come get you one!"); break; } SetColor(DarkGreen); SetCursorPosition(tablePadding + 4, 1); Write("A) Chips"); SetCursorPosition(tablePadding + 4, 13); Write("C) Drinks"); SetCursorPosition((WindowWidth - 2 * tablePadding) / 2 + 4, 1); Write("B) Candy"); SetCursorPosition((WindowWidth - 2 * tablePadding) / 2 + 4, 13); Write("D) Gum"); SetColor(); foreach (KeyValuePair <ItemType, VendingMachineItem[]> itemList in items) { bool activeColumn = false; bool activeRow = false; switch (itemList.Key) { case ItemType.Candy: case ItemType.Chip: currentRow = -1; CursorTop = 1; break; case ItemType.Drink: case ItemType.Gum: currentRow = 9; CursorTop = 13; break; } foreach (VendingMachineItem item in itemList.Value) { activeRow = ++currentRow == selectedRow; switch (itemList.Key) { case ItemType.Chip: case ItemType.Drink: CursorLeft = tablePadding; activeColumn = selectedColumn == 0; break; case ItemType.Candy: case ItemType.Gum: CursorLeft = (WindowWidth - 2 * tablePadding) / 2; activeColumn = selectedColumn == 1; break; } CursorTop++; if (activeColumn && activeRow) { selectedItem = item; CurrentType = itemList.Key; CurrentSlot = currentRow; if (CurrentSlot >= 10) { CurrentSlot -= 10; } } if (item != null) { SetColor(activeColumn && activeRow); if (item.Quantity > 0) { Write((item.Slot + 1) % 10 + ") " + item.Quantity + "x " + item.Name + " " + item.Price.ToString("C")); } else { Write((item.Slot + 1) % 10 + ") "); SetColor(DarkRed); Write("SOLD OUT "); SetColor(activeColumn && activeRow); Write(item.Name + " " + item.Price.ToString("C")); } } else { if (activeColumn && activeRow) { SetColor(DarkGreen); } else { SetColor(DarkGray); } Write(" -- no item --"); } SetColor(); } } keyPress = ReadKey(true); switch (keyPress.Key) // HACK: Refactor out keyPress? Only necessary if we need to refer back to modifier keys. { // Navigate menu using arrow keys case ConsoleKey.UpArrow: if (--selectedRow < 0) { selectedRow = totalRows; } break; case ConsoleKey.DownArrow: if (++selectedRow > totalRows) { selectedRow = 0; } break; case ConsoleKey.LeftArrow: if (--selectedColumn < 0) { selectedColumn = totalColumns; } break; case ConsoleKey.RightArrow: if (++selectedColumn > totalColumns) { selectedColumn = 0; } break; // Navigate menu using item category letters: A, B, C, D case ConsoleKey.A: selectedColumn = 0; selectedRow = 0; break; case ConsoleKey.B: selectedColumn = 1; selectedRow = 0; break; case ConsoleKey.C: selectedColumn = 0; selectedRow = 10; break; case ConsoleKey.D: selectedColumn = 1; selectedRow = 10; break; // Navigate to specific row by number key or numpad. case ConsoleKey.NumPad1: case ConsoleKey.D1: selectedRow = selectedRow < 10 ? 0 : 10; break; case ConsoleKey.NumPad2: case ConsoleKey.D2: selectedRow = selectedRow < 10 ? 1 : 11; break; case ConsoleKey.NumPad3: case ConsoleKey.D3: selectedRow = selectedRow < 10 ? 2 : 12; break; case ConsoleKey.NumPad4: case ConsoleKey.D4: selectedRow = selectedRow < 10 ? 3 : 13; break; case ConsoleKey.NumPad5: case ConsoleKey.D5: selectedRow = selectedRow < 10 ? 4 : 14; break; case ConsoleKey.NumPad6: case ConsoleKey.D6: selectedRow = selectedRow < 10 ? 5 : 15; break; case ConsoleKey.NumPad7: case ConsoleKey.D7: selectedRow = selectedRow < 10 ? 6 : 16; break; case ConsoleKey.NumPad8: case ConsoleKey.D8: selectedRow = selectedRow < 10 ? 7 : 17; break; case ConsoleKey.NumPad9: case ConsoleKey.D9: selectedRow = selectedRow < 10 ? 8 : 18; break; case ConsoleKey.NumPad0: case ConsoleKey.D0: selectedRow = selectedRow < 10 ? 9 : 19; break; // Exit app via Q, cancel selection with escape, confirm selection with enter/spacebar case ConsoleKey.Q: LastAction = UIAction.Exit; isCurrentlyInMenu = false; break; case ConsoleKey.Escape: LastAction = UIAction.DisplayMainMenu; isCurrentlyInMenu = false; break; case ConsoleKey.Enter: case ConsoleKey.Spacebar: LastAction = actionToTake; isCurrentlyInMenu = false; break; } } return(LastAction); }
private void DisplayPurchaseMenu() { while (true) { Console.WriteLine("(1) Insert money"); Console.WriteLine("(2) Make a selection"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine("(R) Return to Main Menu"); Console.WriteLine(); Console.WriteLine($"Current balance: {vm.CurrentBalance.ToString("C")}"); Console.Write("Please make a choice: "); string choice = Console.ReadLine().ToLower(); if (choice == Option_InsertMoney) { Console.Write("How much money do you want to enter? ($1, $2, $5, $20): "); int moneyIn = int.Parse(Console.ReadLine()); vm.FeedMoney(moneyIn); } else if (choice == Option_MakeSelection) { Console.Write("Please select a slot id: "); string slot = Console.ReadLine().ToUpper(); Console.WriteLine(); try { VendingMachineItem purchasedItem = vm.Purchase(slot); Console.WriteLine("Here are your " + purchasedItem.ItemName); } catch (VendingMachineException ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine(); Console.WriteLine("Thank you for using Vendo-Matic!"); Console.ReadKey(); } } else if (choice == Option_ReturnChange) { DisplayReturnedChange(); Console.ReadKey(); } else if (choice == Option_ReturnToPreviousMenu) { Console.WriteLine("Returning to previous menu. "); Console.ReadKey(); return; } else { DisplayInvalidOption(); } Console.Clear(); } }
public void TransactionMenu() { TransactionSession session = new TransactionSession(); bool validTransInput = false; while (!validTransInput) { Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine($"Current Money Provided: ${session.Balance}"); Console.WriteLine(); Console.Write("Selection: "); string transInput = Console.ReadLine(); switch (transInput) { case "1": // Feed Money Console.Clear(); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); decimal billValue = 0.0M; string inputFeed = Console.ReadLine(); while (!decimal.TryParse(inputFeed, out billValue)) { Console.Clear(); Console.WriteLine($"\'{inputFeed}\' is not an accepted dollar amount! Try again!"); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); inputFeed = Console.ReadLine(); } while (!session.Deposit(billValue)) { Console.Clear(); Console.WriteLine($"\'{billValue}\' is not an accepted dollar amount! Try again!"); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); billValue = decimal.Parse(Console.ReadLine()); } Console.Clear(); vendingMachine.WriteLineToLog("FEED MONEY:", billValue, session.Balance); // 10/01/2018 12:00:00 PM FEED MONEY: $5.00 $5.00 // Date Time "FEED MONEY:" amount_added balance break; case "2": // Select Product Console.Clear(); Display(); Console.Write("Select an item code: "); string input = Console.ReadLine(); string inputCode = input.ToUpper(); if (!vendingMachine.IsValidCode(inputCode)) { Console.Clear(); Console.WriteLine($"\'{input}\' is not a valid item code"); Console.WriteLine(); } else { if (!vendingMachine.IsInStock(inputCode)) { Console.Clear(); Console.WriteLine($"\'{inputCode}\' is out of stock"); Console.WriteLine(); } else { // buy the item if (vendingMachine.HaveEnoughToBuy(inputCode, session)) { decimal startingBalance = session.Balance; VendingMachineItem theItem = vendingMachine.BuyItem(inputCode, session); Console.Clear(); Console.WriteLine($"User obtained one (1) {theItem.Name}"); Console.WriteLine(); Console.WriteLine(theItem.Text); Console.WriteLine(); vendingMachine.WriteLineToLog($"{theItem.Name} {theItem.ItemCode}", startingBalance, session.Balance); } else { Console.Clear(); Console.WriteLine("Funds not sufficient ;("); Console.WriteLine(); } } } // 10/01/2018 12:00:20 PM Crunchie B4 $10.00 $8.50 // Date Time itemName itemCode balanceBefore balanceAfter break; case "3": // Finish Transaction Console.Clear(); if (session.Balance > 0) { decimal changeBack = session.Balance; string change = session.FinishTransaction(); Console.WriteLine("Change returned:"); Console.WriteLine(); Console.WriteLine(change); Console.WriteLine(); vendingMachine.WriteLineToLog("GIVE CHANGE:", changeBack, session.Balance); } validTransInput = true; // 10/01/2018 12:01:35 PM GIVE CHANGE: $7.50 $0.00 // Date Time "GIVE CHANGE:" balanceBefore balanceAfter break; default: // If input is not 1, 2, or 3 Console.Clear(); Console.WriteLine($"\'{transInput}\' is not a valid selection dammit! Try again!"); Console.WriteLine(); Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine($"Current Money Provided: ${session.Balance}"); transInput = Console.ReadLine(); break; } } }
//constructor public InventoryItem(VendingMachineItem item) { Item = item; }
public void PrintTransaction(VendingMachineTransaction transaction) { PrintBalance(); SetCursorPosition(4, 6); SetColor(Gray); switch (transaction.Type) { case TransactionType.FeedMoney: Write($"Yay Money! You gave me a "); SetColor(); Write($"{transaction.Amount.ToString("C")}"); SetColor(Gray); Write(" bill!"); SetCursorPosition(4, 7); break; case TransactionType.InvalidBill: Write($"That isn't real money. What am I supposed to do with that?"); SetCursorPosition(4, 7); break; case TransactionType.PurchaseItem: VendingMachineItem item = transaction.Item; switch (item.Type) { case ItemType.Candy: Write("Munch Munch, Yum!"); break; case ItemType.Chip: Write("Crunch Crunch, Yum!"); break; case ItemType.Drink: Write("Glug Glug, Yum!"); break; case ItemType.Gum: Write("Chew Chew, Yum!"); break; } SetCursorPosition(4, 7); break; case TransactionType.InvalidPurchase: Write("There's nothing there for you to buy."); SetCursorPosition(4, 7); Write("You can't buy that."); SetCursorPosition(4, 8); Write("Buy something else."); SetCursorPosition(4, 9); break; case TransactionType.NotSufficientFunds: Write("I get that you'd like that,"); SetCursorPosition(6, 7); Write("but sadly.."); SetCursorPosition(8, 8); Write("you can't afford it."); SetCursorPosition(4, 10); Write("Insert additional cash or try something different."); SetCursorPosition(4, 11); break; case TransactionType.ItemOutOfStock: Write("I'm sorry, we're all out of that!"); SetCursorPosition(4, 8); break; case TransactionType.GiveChange: if (transaction.Amount >= 0.05M) { int[] coins = { 0, 0, 0 }; decimal total = transaction.Amount; while (total >= 0.05M) { if (total >= 0.25M) { ++coins[0]; total -= 0.25M; } else if (total >= 0.10M) { ++coins[1]; total -= 0.10M; } else if (total >= 0.05M) { ++coins[2]; total -= 0.05M; } } SetColor(Gray); Write($"You receive "); SetColor(); if (coins[0] > 0) { Write(coins[0]); SetColor(Gray); Write(" quarter"); if (coins[0] > 1) { Write("s"); } if (coins[1] > 0 || coins[2] > 0) { Write(", "); } else { Write("."); } SetColor(); } if (coins[1] > 0) { if (coins[0] > 0 && coins[2] == 0) { SetColor(Gray); Write("and "); SetColor(); } Write(coins[1]); SetColor(Gray); Write(" dime"); if (coins[1] > 1) { Write("s"); } if (coins[2] > 0) { Write(", "); } else { Write("."); } SetColor(); } if (coins[2] > 0) { if (coins[0] > 0 || coins[1] > 0) { SetColor(Gray); Write("and "); SetColor(); } Write(coins[2]); SetColor(Gray); Write(" nickle."); SetColor(); } SetCursorPosition(4, 7); } break; case TransactionType.GenerateSalesReport: Write("Generating Sales report!"); SetCursorPosition(4, 7); break; default: throw new NotImplementedException(transaction.Type.ToString()); } PrintContinueMessage(); }
public VendingMachineTransaction(TransactionType type, VendingMachineItem item) : this(type, item.Price, item) { }