public void UserItemChoice_Test_OutOfStock() { // Arrange VendingMachine testVendingMachine = new VendingMachine(); testVendingMachine.RestockFromLines(sampleStockFileLines); testVendingMachine.AddMoney(20M); Exception e = new Exception(""); //Act try { VendingMachineItems resultItem = testVendingMachine.UserItemChoice("A1"); VendingMachineItems resultItem1 = testVendingMachine.UserItemChoice("A1"); VendingMachineItems resultItem2 = testVendingMachine.UserItemChoice("A1"); VendingMachineItems resultItem3 = testVendingMachine.UserItemChoice("A1"); VendingMachineItems resultItem4 = testVendingMachine.UserItemChoice("A1"); VendingMachineItems resultItem5 = testVendingMachine.UserItemChoice("A1"); } catch (Exception f) // catch insufficient stock error { e = f; // effectively makes e message "out of stock" ??? } //Assert Assert.AreEqual(e.Message, VendingMachine.OUT_OF_STOCK_MESSAGE); }
/// <summary> /// -Checks user code input (ex: "A1", "B4") /// -if code matches from the list- dispense item and update balance /// - if code does not match, release error message. /// </summary> /// <param name="userCodeEntered">this is the code that the user entered</param> /// <returns></returns> public VendingMachineItems UserItemChoice(string userCodeEntered) { userCodeEntered = userCodeEntered.ToUpper(); if (TotalInventoryDictionary.ContainsKey(userCodeEntered)) { VendingMachineItems selectedItem = TotalInventoryDictionary[userCodeEntered]; AuditLogPurchaseMethod(selectedItem.Name, selectedItem.SlotId, Balance, Balance - selectedItem.Price); if (selectedItem.StockCount == 0) { throw new Exception(OUT_OF_STOCK_MESSAGE); } else if (Balance < selectedItem.Price) { throw new Exception(INSUFFICIENT_FUNDS_MESSAGE); } selectedItem.StockCount -= 1; Balance -= selectedItem.Price; return(selectedItem); } else { throw new Exception(INVALID_SLOT_MESSAGE); } }
public Machine(VendingMachineItems vendingMachineItems, Currency currency) { _vendingMachineItems = vendingMachineItems; foreach (Tender tender in currency.Coins) { _coinDenominations.Add(tender.Value); } _coinDenominations = _coinDenominations.OrderByDescending(x => x).ToList(); _currencySymbol = currency.Symbol; }
/// <summary> /// get the machine items, and determine which country you are working in to pass to the <see cref="Machine"/> to handle the logic. /// </summary> /// <param name="request"><see cref="PurchaseItemRequest"/></param> /// <returns><see cref="PurchaseItemResponse"/></returns> public PurchaseItemResponse PurchaseItem(PurchaseItemRequest request) { List <Change> denominations = null; List <int> coinsDue = null; try { VendingMachineItems items = GetVendingMachineItems(); Country country = countries.Find(x => x.Alpha2Code.ToLower() == request.Alpha2Code.ToLower()); if (country == null) { throw new ApplicationException($"Unable to find a config set for the country with the Alpha2Code: {request.Alpha2Code}"); } Machine machine = new Machine(items, country.Currency); decimal changeDue = machine.ProcessPurchase(request.Position, request.TenderAmount); if (changeDue != 0.0M) { denominations = machine.GiveCoins(changeDue); var ordered = denominations.OrderByDescending(x => x.Coin).ToList(); coinsDue = new List <int>(); foreach (Change change in ordered) { int count = 0; while (count < change.Total) { count++; coinsDue.Add(change.Coin); } } } return(new PurchaseItemResponse { IsSuccessful = true, IsClientFriendlyMessage = true, Message = $"Successfully purchased: {request.Position.ToString()}.{Environment.NewLine}Change due: {country.Currency.Symbol}{changeDue.ToString()}", TotalChangeDue = changeDue, Denominations = denominations, Coins = coinsDue }); } catch (ApplicationException exception) { return(new PurchaseItemResponse { IsSuccessful = false, IsClientFriendlyMessage = true, Message = exception.Message }); } catch (Exception exception) { return(new PurchaseItemResponse { IsSuccessful = false, IsClientFriendlyMessage = false, Message = exception.ToString() }); } }
public void RestockFromLines(List <string> fileLines) // we take our list of strings and loop through it // make a new machineitems. we split the list and set that to an array. set the indexes of the array to to the properties in the new machine // then add the item to the new dictionary. { foreach (string line in fileLines) { VendingMachineItems newItem = new VendingMachineItems(); string[] lineSplit = line.Split("|"); newItem.Name = lineSplit[1]; newItem.Price = decimal.Parse(lineSplit[2]); newItem.SlotId = lineSplit[0]; newItem.ProductType = lineSplit[3]; newItem.StockCount = 5; TotalInventoryDictionary.Add(lineSplit[0], newItem); } }
public void UserItemChoice_Test_SlotNotFound() { //Arrange VendingMachine testVendingMachine = new VendingMachine(); testVendingMachine.RestockFromLines(sampleStockFileLines); testVendingMachine.AddMoney(20M); Exception e = new Exception(""); //Act try { VendingMachineItems resultItem = testVendingMachine.UserItemChoice("Z6"); } catch (Exception f) // catch insufficient funds error { e = f; // effectively makes e message "INVALID SlotMEssage" } //Assert Assert.AreEqual(e.Message, VendingMachine.INVALID_SLOT_MESSAGE); }
public void UserItemChoice_Test_GoodData() { //Arrange VendingMachine testVendingMachine = new VendingMachine(); testVendingMachine.RestockFromLines(sampleStockFileLines); testVendingMachine.AddMoney(20M); string expectedItemName = "M&Ms"; decimal expectedItemPrice = 3.05M; string expectedItemCategory = "Candy"; //Act VendingMachineItems resultItem = testVendingMachine.UserItemChoice("A1"); // Assert Assert.AreEqual(expectedItemName, resultItem.Name); Assert.AreEqual(expectedItemPrice, resultItem.Price); Assert.AreEqual(expectedItemCategory, resultItem.ProductType); }
public void UserItemChoice_Test_InsufficientFunds() // surround user item choice in try catch, // assert equals exception e.message, to invalid not enough money { // Arrange VendingMachine testVendingMachine = new VendingMachine(); testVendingMachine.RestockFromLines(sampleStockFileLines); //testVendingMachine.AddMoney(20M); // if this is uncommented this test will fail Exception e = new Exception(""); //Act try { VendingMachineItems resultItem = testVendingMachine.UserItemChoice("A1"); } catch (Exception f) // catch insufficient funds error { e = f; // effectively makes e message "insufficient funds" } //Assert Assert.AreEqual(e.Message, VendingMachine.INSUFFICIENT_FUNDS_MESSAGE); }
public void ReadFileOld() { using (StreamReader reader = new StreamReader(@"C:\Users\Student\git\c-module-1-capstone-team-2\19_Capstone\vendingmachine.csv")) { while (!reader.EndOfStream) { string inventoryLine = reader.ReadLine(); // Reads every line and assigns the whole line to the string string[] lineSplit = inventoryLine.Split("|"); // splits the string into an array. string productName = lineSplit[1]; decimal itemPrice = decimal.Parse(lineSplit[2]); string slotId = lineSplit[0]; string productType = lineSplit[3]; //VendingMachineItems item = new VendingMachineItems(productName, itemPrice, 5, slotId); VendingMachineItems item = new VendingMachineItems(); item.Name = lineSplit[1]; item.Price = decimal.Parse(lineSplit[2]); item.SlotId = lineSplit[0]; item.ProductType = lineSplit[3]; item.StockCount = 5; //(lineSplit[1], decimal.Parse(lineSplit[2]), 5, lineSplit[0], lineSplit[3]) TotalInventoryDictionary.Add(lineSplit[0], item); //A1 | Potato Crisps | 3.05 | Chip //B1 | Moonpie | 1.80 | Candy //B2 | Cowtales | 1.50 | Candy //C1 | Cola | 1.25 | Drink } } }