コード例 #1
0
        public bool SodaInventory_CheckIfSodaInStock(SodaModel soda)
        {
            var sodas       = RetrieveSodas();
            var outputSodas = sodas.Where(x => x.Name == soda.Name && x.SlotOccupied == soda.SlotOccupied).FirstOrDefault();

            return(outputSodas != null);
        }
コード例 #2
0
        public void RequestSoda_ShouldSayNotEnoughChange()
        {
            MockDataAccess   da    = new MockDataAccess();
            SodaMachineLogic logic = new SodaMachineLogic(da);

            string    user         = "******";
            SodaModel expcetedSoda = new SodaModel {
                Name = "Coke", SlotOccupied = "1"
            };
            var initialState = da.MachineInfo;

            da.UserCredit[user] = 1.75M;
            da.CoinInventory.Clear();

            var results = logic.RequestSoda(expcetedSoda, user);

            Assert.Null(results.soda);

            Assert.Equal(1.75M, da.UserCredit[user]);

            Assert.Equal(initialState.cashOnHand, da.MachineInfo.cashOnHand);
            Assert.Equal(initialState.totalIncome, da.MachineInfo.totalIncome);

            Assert.False(string.IsNullOrWhiteSpace(results.errorMessage));

            Assert.True(results.change.Count() == 0);
        }
コード例 #3
0
        public void RequestSoda_ShouldReturnSodaWithNoChange()
        {
            MockDataAccess   da    = new MockDataAccess();
            SodaMachineLogic logic = new SodaMachineLogic(da);

            string    user         = "******";
            SodaModel expcetedSoda = new SodaModel {
                Name = "Coke", SlotOccupied = "1"
            };
            var initialState = da.MachineInfo;

            da.UserCredit[user] = 0.75M;

            var results = logic.RequestSoda(expcetedSoda, user);

            Assert.Equal(expcetedSoda.Name, results.soda.Name);
            Assert.Equal(expcetedSoda.SlotOccupied, results.soda.SlotOccupied);

            Assert.Equal(0, da.UserCredit[user]);

            Assert.Equal(initialState.cashOnHand + 0.75M, da.MachineInfo.cashOnHand);
            Assert.Equal(initialState.totalIncome + 0.75M, da.MachineInfo.totalIncome);

            Assert.True(string.IsNullOrWhiteSpace(results.errorMessage));

            Assert.True(results.change.Count() == 0);
        }
コード例 #4
0
        public void RequestSoda_ShouldReturnSodaWithChange()
        {
            string    userId = "user2468";
            decimal   amount = 1.00M;
            SodaModel actualSoda;
            int       coinCount;
            SodaModel requestedSoda = new SodaModel {
                Name = "Pepsi", SlotOccupied = "A1"
            };

            (SodaModel soda, List <CoinModel> coins, string message)result;

            sodaMachineLogic.MoneyInserted(userId, amount);
            sodaMachineLogic.MoneyInserted(userId, amount);
            result     = sodaMachineLogic.RequestSoda(requestedSoda);
            actualSoda = result.soda;
            coinCount  = result.coins.Count;

            System.Console.WriteLine(result.coins.Count.ToString() + coinCount);
            Assert.Multiple(() =>
            {
                Assert.AreEqual(requestedSoda.Name, actualSoda.Name);
                Assert.AreEqual(requestedSoda.SlotOccupied, actualSoda.SlotOccupied);
                Assert.That(coinCount, Is.GreaterThan(0));
            });
        }
コード例 #5
0
        public void RequestSoda_ShouldSayOutOfStock()
        {
            MockDataAccess   da    = new MockDataAccess();
            SodaMachineLogic logic = new SodaMachineLogic(da);

            string    user         = "******";
            SodaModel expectedSoda = new SodaModel {
                Name = "Fanta", SlotOccupied = "4"
            };
            var initialState = da.MachineInfo;

            da.UserCredit[user] = 0.75M;

            var results = logic.RequestSoda(expectedSoda, user);

            Assert.Null(results.soda);

            Assert.Equal(0.75M, da.UserCredit[user]);

            Assert.Equal(initialState.cashOnHand, da.MachineInfo.cashOnHand);
            Assert.Equal(initialState.totalIncome, da.MachineInfo.totalIncome);

            Assert.False(string.IsNullOrWhiteSpace(results.errorMessage));

            Assert.True(results.change.Count() == 0);
        }
コード例 #6
0
        public void RequestSoda_ShouldReturnSodaWithAlternativeChange()
        {
            MockDataAccess   da    = new MockDataAccess();
            SodaMachineLogic logic = new SodaMachineLogic(da);

            string    user         = "******";
            SodaModel expcetedSoda = new SodaModel {
                Name = "Coke", SlotOccupied = "1"
            };
            var initialState = da.MachineInfo;

            da.UserCredit[user] = 1M;
            da.CoinInventory.RemoveAll(x => x.Amount == 0.25M);

            var results = logic.RequestSoda(expcetedSoda, user);

            Assert.Equal(expcetedSoda.Name, results.soda.Name);
            Assert.Equal(expcetedSoda.SlotOccupied, results.soda.SlotOccupied);

            Assert.Equal(0, da.UserCredit[user]);

            Assert.Equal(initialState.cashOnHand + 1M, da.MachineInfo.cashOnHand);
            Assert.Equal(initialState.totalIncome + 1M, da.MachineInfo.totalIncome);

            Assert.True(string.IsNullOrWhiteSpace(results.errorMessage));

            // Dime + Dime + Nickle = Quarter
            Assert.True(results.change.Count() == 3);
        }
コード例 #7
0
        public (SodaModel soda, List <CoinModel> coins, string message) RequestSoda(SodaModel soda)
        {
            try
            {
                SodaModel machineSoda = _dataAccess.SodaInventoryGetSoda(soda);
                decimal   userCredit  = _dataAccess.UserCreditTotal(_userId);
                decimal   sodaPrice   = _dataAccess.MachineInfoSodaPrice();

                if (machineSoda == null)
                {
                    throw new OutOfStockException();
                }

                if (userCredit < sodaPrice)
                {
                    throw new NotEnoughMoneyException();
                }

                List <CoinModel> coins  = new List <CoinModel>();
                decimal          change = userCredit - sodaPrice;

                _dataAccess.UserCreditInsert(_userId, -change);

                int dollarQuantity  = 0;
                int quarterQuantity = 0;

                while (change >= 1.00M)
                {
                    dollarQuantity++;
                    change = decimal.Subtract(change, 1.00M);
                }

                while (change >= 0.25M)
                {
                    quarterQuantity++;
                    change = decimal.Subtract(change, 0.25M);
                }

                coins.AddRange(_dataAccess.CoinInventoryWithdrawCoins(1.00M, dollarQuantity));
                coins.AddRange(_dataAccess.CoinInventoryWithdrawCoins(0.25M, quarterQuantity));

                _dataAccess.UserCreditClear(_userId);

                return(machineSoda, coins, "Enjoy the refreshment");
            }
            catch (OutOfStockException ex)
            {
                return(new SodaModel(), new List <CoinModel>(), ex.Message);
            }
            catch (NotEnoughMoneyException ex)
            {
                return(new SodaModel(), new List <CoinModel>(), ex.Message);
            }

            throw new Exception("Request soda failed");
        }
コード例 #8
0
        public void RequestSoda_ShouldSayOutOfStock()
        {
            string    expectedMessage = "Out of stock";
            SodaModel soda            = new SodaModel {
                Name = "Pepsi", SlotOccupied = "C1"
            };

            (SodaModel soda, List <CoinModel> coins, string message)result = sodaMachineLogic.RequestSoda(soda);

            Assert.AreEqual(expectedMessage, result.message);
        }
コード例 #9
0
        public void RequestSoda_ShouldSayNotEnoughMoney()
        {
            string    expectedMessage = "Not enough money";
            SodaModel soda            = new SodaModel {
                Name = "Pepsi", SlotOccupied = "A1"
            };

            (SodaModel soda, List <CoinModel> coins, string message)result = sodaMachineLogic.RequestSoda(soda);

            Assert.AreEqual(expectedMessage, result.message);
        }
コード例 #10
0
        public SodaModel SodaInventoryGetSoda(SodaModel soda)
        {
            List <SodaModel> sodaInventory = new List <SodaModel>();

            sodaInventory = SodaInventoryGetAll();

            return(sodaInventory.Find(sodaInventorySoda =>
                                      sodaInventorySoda.Name == soda.Name &&
                                      sodaInventorySoda.SlotOccupied == soda.SlotOccupied
                                      ));
        }
コード例 #11
0
        public void RequestSoda_ShouldReturnNoChange()
        {
            int       expectedCoinCount = 0;
            int       actualCount;
            SodaModel soda = new SodaModel {
                Name = "Pepsi", SlotOccupied = "A1"
            };

            (SodaModel soda, List <CoinModel> coins, string message)result = sodaMachineLogic.RequestSoda(soda);
            actualCount = result.coins.Count;

            Assert.AreEqual(expectedCoinCount, actualCount);
        }
コード例 #12
0
        public SodaModel SodaInventory_GetSoda(SodaModel soda, decimal amount)
        {
            var sodas      = RetrieveSodas();
            var outputSoda = sodas.Where(x => x.Name == soda.Name && x.SlotOccupied == soda.SlotOccupied).FirstOrDefault();

            if (outputSoda != null)
            {
                var info = RetrieveMachineInfo();
                info.cashOnHand  += amount;
                info.totalIncome += amount;
                SaveMachineInfo(info.sodaPrice, info.cashOnHand, info.totalIncome);
            }

            return(outputSoda);
        }
コード例 #13
0
        public IActionResult OnPostAddSoda()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (string.IsNullOrEmpty(SelectedSoda))
            {
                Message = "Please select soda with slot";
            }
            else
            {
                if (int.TryParse(SelectedSodaQuantity, out int quantity))
                {
                    if (quantity > 0 && quantity < 10)
                    {
                        List <SodaModel> sodas        = new List <SodaModel>();
                        string[]         sodaNameSlot = SelectedSoda.Split(" ");

                        for (int i = 0; i < quantity; i++)
                        {
                            SodaModel sodaModel = new SodaModel
                            {
                                Name         = sodaNameSlot[0],
                                SlotOccupied = sodaNameSlot[1]
                            };

                            sodas.Add(sodaModel);
                        }

                        _dataAccess.SodaInventoryAddSodas(sodas);

                        Message = $"Successfully added { quantity } { sodaNameSlot[0] } at slot { sodaNameSlot[1] }";
                    }
                    else
                    {
                        Message = "Quantity must be greater than 0 and less than 10";
                    }
                }
                else
                {
                    Message = "Please enter a number for quantity";
                }
            }

            return(OnGet());
        }
コード例 #14
0
        public IActionResult OnPostRequestSoda()
        {
            if (int.TryParse(SelectedSodaIndex, out int sodaIndex))
            {
                SodasInStock = _sodaMachineLogic.GetSodaInventory();

                if (sodaIndex > -1 && sodaIndex < SodasInStock.Count)
                {
                    UserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
                    _sodaMachineLogic.MoneyInserted(UserId, 0);

                    SodaModel requestSoda = SodasInStock[sodaIndex];

                    var(soda, coins, message) = _sodaMachineLogic.RequestSoda(requestSoda);

                    RequestSodaMessage = message;

                    if (coins.Count > 0)
                    {
                        decimal change = 0;

                        foreach (CoinModel coin in coins)
                        {
                            change += coin.Amount;
                        }

                        ChangeReturned = change;
                    }
                }
                else
                {
                    RequestSodaMessage = "Please select a soda";
                }
            }
            else
            {
                RequestSodaMessage = "Please select a soda";
            }

            return(OnGet());
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: josephttran/csharp-code
        static void Main()
        {
            ConfigureServices();
            SodaMachineLogic   sodaMachineLogic   = ActivatorUtilities.CreateInstance <SodaMachineLogic>(ServiceProvider);
            Menu               menu               = new Menu();
            SodaMachineDisplay sodaMachineDisplay = new SodaMachineDisplay();

            string userId = "user12";

            while (true)
            {
                menu.PrintMenu();
                string userChoice = Console.ReadLine();
                Console.WriteLine();

                switch (userChoice)
                {
                case "1":
                    sodaMachineDisplay.PrintTypesOfSoda(sodaMachineLogic.ListTypesOfSoda());
                    break;

                case "2":
                    sodaMachineDisplay.PrintSodaInStock(sodaMachineLogic.GetSodaInventory());
                    break;

                case "3":
                    sodaMachineDisplay.PrintSodaPrice(sodaMachineLogic.GetSodaPrice());
                    break;

                case "4":
                    sodaMachineDisplay.PrintInsertMoneyChoice(sodaMachineLogic.AcceptedCoinValues);
                    string userMoneyInput = Console.ReadLine();

                    if (decimal.TryParse(userMoneyInput, out decimal moneyResult))
                    {
                        decimal moneyInserted = sodaMachineLogic.MoneyInserted(userId, moneyResult);

                        if (moneyInserted != 0)
                        {
                            sodaMachineDisplay.PrintAmountInserted(moneyInserted);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid currency");
                    }

                    break;

                case "5":
                    sodaMachineDisplay.PrintAmountInserted(sodaMachineLogic.GetMoneyInsertedTotal(userId));
                    break;

                case "6":
                    List <SodaModel> availableSodas = sodaMachineLogic.ListTypesOfSoda();
                    sodaMachineDisplay.PrintSodaToBuyChoice(availableSodas);
                    string userSodaInput = Console.ReadLine();

                    if (int.TryParse(userSodaInput, out int sodaResult))
                    {
                        if (sodaResult >= 0 && sodaResult < availableSodas.Count)
                        {
                            SodaModel sodaInput = availableSodas[sodaResult];
                            var(soda, coins, message) = sodaMachineLogic.RequestSoda(sodaInput);

                            if (string.IsNullOrEmpty(soda.Name))
                            {
                                Console.WriteLine(message);
                            }
                            else
                            {
                                if (coins.Count > 0)
                                {
                                    decimal change = 0;

                                    foreach (CoinModel coin in coins)
                                    {
                                        change += coin.Amount;
                                    }

                                    Console.WriteLine($"returned { change }");
                                }

                                sodaMachineDisplay.PrintSuccessMessage(soda);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid soda choice");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid soda choice");
                    }

                    break;

                case "7":
                    decimal refund = sodaMachineLogic.IssueFullRefund(userId);
                    sodaMachineDisplay.PrintRefundAmount(refund);
                    break;

                default:
                    Console.WriteLine("Invalid Choice");
                    break;
                }
            }
        }
コード例 #16
0
 public void PrintSuccessMessage(SodaModel soda)
 {
     Console.WriteLine($"Thank you for you purchase. Enjoy your refreshing { soda.Name }.");
 }
コード例 #17
0
        public (SodaModel soda, List <CoinModel> change, string errorMessage) RequestSoda(SodaModel soda, string userId)
        {
            (SodaModel soda, List <CoinModel> change, string errorMessage)output = (null, new List <CoinModel>(), "An unexpected error occurred");
            decimal userCredit = _db.UserCredit_Total(userId);
            decimal sodaCost   = _db.MachineInfo_SodaPrice();

            if (userCredit >= sodaCost) // No change needed
            {
                bool sodaInStock = _db.SodaInventory_CheckIfSodaInStock(soda);

                if (sodaInStock == false)
                {
                    output.errorMessage = "Soda not in inventory.";
                }
                else
                {
                    try
                    {
                        var change = GetChange(sodaCost, userCredit);

                        var sodaToReturn = _db.SodaInventory_GetSoda(soda, userCredit);
                        _db.UserCredit_Clear(userId);
                        output = (sodaToReturn, change, "");
                    }
                    catch (Exception ex)
                    {
                        output.errorMessage = "There is not enough coins to give you change.";
                    }
                }
            }
            else // Not enough money
            {
                output = (null, new List <CoinModel>(), "User did not provide enough change.");
            }

            return(output);
        }