Ejemplo n.º 1
0
 public void SetUp()
 {
     Vm    = new VendingMachine.VendingMachine();
     State = new VendingMachine.States.NoMoneyInsertedState(Vm);
     Iq    = new ItemQueue(1.5);
     Iq.QueueItem(new Item("TestName", "TestFlavor", "TestWrapperColor"));
     Vm.ItemQueues.Add(QueueName, Iq);
 }
Ejemplo n.º 2
0
 public void SetUp()
 {
     Vm = new VendingMachine.VendingMachine();
     Iq = new ItemQueue(1.5);
     for (int i = 0; i < 5; i++)
     {
         Iq.QueueItem(new Item("TestName" + i, "TestFlavor" + i, "TestWrapperColor" + i));
     }
     Vm.ItemQueues.Add(QueueName, Iq);
 }
        public void InitialValueIsZero()
        {
            VendingMachine vendingMachine;

            Scenario()
                .Given(vendingMachine = new VendingMachine())

                .WhenNothing()

                .Then(vendingMachine.Total, Is(AnInt.EqualTo(0)));
        }
        public void Setup()
        {
            candyA = new Candy("Hershey", 2.99m, "Sweet", "Blue");
            candyB = new Candy("Twix", 2.99m, "Sweet", "Blue");
            Dictionary <string, Candy> products = new Dictionary <string, Candy>()
            {
                { candyA.name, candyA },
                { candyB.name, candyB }
            };

            vendingMachine = new VendingMachine.VendingMachine(products);
        }
        public void AddingUnrecognisedCoinAddsToReturnedCoins()
        {
            VendingMachine vendingMachine;

            Scenario()
                .Given(vendingMachine = new VendingMachine())

                .When(() => vendingMachine.AddCoin("2"))
                .When(() => vendingMachine.AddCoin("3"))
                .When(() => vendingMachine.AddCoin("9"))

                .Then(vendingMachine,
                    Is(AVendingMachine.With()
                        .Total(2)
                        .ReturnedCoins(AList.InOrder().WithOnlyValues("3", "9"))));
        }
        public void DispensesProductForExactMoney()
        {
            VendingMachine vendingMachine;

            Scenario()
                .Given(vendingMachine = new VendingMachine())
                .Given(() => vendingMachine.AddCoin("100"))

                .When(() => vendingMachine.RequestProduct(Product.Cola))

                .Then(vendingMachine,
                    Is(AVendingMachine.With()
                        .Total(0)
                        .DispensedProducts(Product.Cola)
                        .Display("THANK YOU")
                        .ReturnedCoins()));
        }
        public void AddingCoinsIncrementsValue()
        {
            VendingMachine vendingMachine;

            Scenario()
                .Given(vendingMachine = new VendingMachine())

                .When(() => vendingMachine.AddCoin("1"))
                .When(() => vendingMachine.AddCoin("2"))
                .When(() => vendingMachine.AddCoin("5"))
                .When(() => vendingMachine.AddCoin("10"))
                .When(() => vendingMachine.AddCoin("20"))
                .When(() => vendingMachine.AddCoin("50"))
                .When(() => vendingMachine.AddCoin("100"))

                .Then(vendingMachine,
                    Is(AVendingMachine.With()
                        .Total(188)));
        }
        // создание торгового автомата
        public IVendingMachine CreateVendingMachine()
        {
            // начальные деньги в автомате
            var machineMoney = new Wallet();

            // продукты
            var products = new Dictionary<Product, uint>
            {
                // кексы по 50 рублей, 4 штуки
                { new Product{Name = "Cupcake", Price = 50U }, 4U },
                // печенье по 10 рублей, 3 штуки
                { new Product{Name = "Cookies", Price = 10U }, 3U },
                // вафли по 30 рублей, 10 штук
                { new Product{Name = "Waffles", Price = 30U}, 10U },
            };

            // создание торгового автомата
            var vendingMachine = new VendingMachine(products, machineMoney);
            return vendingMachine;
        }
        public void GivesChange()
        {
            VendingMachine vendingMachine;
            ICalculateChange calculateChange;

            Scenario()
                .Given(calculateChange = AMock<ICalculateChange>()
                    .WhereMethod(c => c.ChangeFor(35)).Returns(new[] { 20, 10, 5 })
                    .Instance)
                .Given(vendingMachine = new VendingMachine(calculateChange))
                .Given(() => vendingMachine.AddCoin("100"))

                .When(() => vendingMachine.RequestProduct(Product.Candy))

                .Then(vendingMachine,
                    Is(AVendingMachine.With()
                        .Total(0)
                        .DispensedProducts(Product.Candy)
                        .Display("THANK YOU")
                        .ReturnedCoins("20", "10", "5")));
        }
Ejemplo n.º 10
0
 static void Main(string[] args)
 {
     VendingMachine vendingmachine = new VendingMachine();
 }
        public void ShowsErrorIfNotEnoughMoneyAdded()
        {
            VendingMachine vendingMachine;

            Scenario()
                .Given(vendingMachine = new VendingMachine())

                .When(() => vendingMachine.RequestProduct(Product.Cola))

                .Then(vendingMachine,
                    Is(AVendingMachine.With()
                        .Total(0)
                        .Display("INSERT MORE COINS")
                        .DispensedProducts()));
        }
Ejemplo n.º 12
0
 public Ready(VendingMachine vendingMachine)
 {
     this.vendingMachine = vendingMachine;
 }
Ejemplo n.º 13
0
 public DispenseItem(VendingMachine vendingMachine)
 {
     this.vendingMachine = vendingMachine;
 }
Ejemplo n.º 14
0
 public TransactionCancelled(VendingMachine vendingMachine)
 {
     this.vendingMachine = vendingMachine;
 }
Ejemplo n.º 15
0
 public void SetUp()
 {
     vm = new VendingMachine();
 }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            VendingMachineConfig.Load();
            VendingMachine vendingMachine = new VendingMachine(VendingMachineConfig.VendingMachineItems);

            Console.Out.WriteLine("Welcome to the Vending Machine! Enjoy your snack");
            Console.Out.WriteLine(Instructions());

            bool exit = false;

            while (!exit)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);

                switch (key.KeyChar)
                {
                    case '1':
                    {
                        Console.Out.WriteLine(vendingMachine.PrintItems());
                        break;
                    }
                    case '2':
                    {
                        Console.Out.WriteLine("How much money will you insert? (e.g 0.50 or 1)");
                        string userInput = Console.In.ReadLine();

                        float money = 0.0f;
                        if (float.TryParse(userInput, out money) && money > 0.0f)
                        {
                            vendingMachine.InsertCoin(money);
                            Console.Out.WriteLine(string.Format("Current Balance: ${0:F2}.", vendingMachine.CurrentBalance));
                        }
                        else
                        {
                            Console.Out.WriteLine("That is not a valid amount.");
                        }
                        break;
                    }
                    case '3':
                    {
                        Console.Out.WriteLine(string.Format("Current Balance: ${0:F2}.", vendingMachine.CurrentBalance));
                        break;
                    }
                    case '4':
                    {
                        Console.Out.WriteLine("Enter the key for the snack that you want:");
                        string userInput = Console.In.ReadLine();

                        VendingMachineItem item = null;
                        bool success = vendingMachine.SelectItem(userInput, out item);
                        if (success)
                        {
                            Console.Out.WriteLine("You successfully purchased {0}.", item.Name);

                            if (vendingMachine.CurrentBalance > 0.0)
                            {
                                Console.Out.WriteLine("Your change is ${0:F2}.", vendingMachine.RefundBalance());
                            }
                        }
                        else
                        {
                            if (item == null)
                            {
                                Console.Out.WriteLine("The key entered is not valid.");
                            }
                            else if (item.Remaining == 0)
                            {
                                Console.Out.WriteLine("{0} is sold out.", item.Name);
                            }
                            else if (item.Cost > vendingMachine.CurrentBalance)
                            {
                                Console.Out.WriteLine(string.Format("There is not enough money in the machine. Please add ${0:F2}.", item.Cost - vendingMachine.CurrentBalance));
                            }
                            else
                            {
                                Console.Out.WriteLine("There was an unexpected error making this purchase...");
                            }
                        }
                        break;
                    }
                    case '5':
                    {
                        float refundAmount = vendingMachine.RefundBalance();
                        if (refundAmount > 0.0f)
                        {
                            Console.Out.WriteLine(string.Format("Your change is: ${0:F2}.", refundAmount));
                        }
                        else
                        {
                            Console.Out.WriteLine("You did not insert any money.");
                        }
                        break;
                    }
                    case 'r':
                    {
                        ResetData();
                        vendingMachine = new VendingMachine(VendingMachineConfig.VendingMachineItems);
                        Console.Out.WriteLine("The data has been reset.");
                        break;
                    }
                    case 'q':
                    {
                        exit = true;
                        VendingMachineConfig.VendingMachineItems = vendingMachine.GetItems();
                        VendingMachineConfig.Save();
                        break;
                    }
                    default:
                    {
                        Console.Out.WriteLine("\nI did not understand your request.");
                        Console.Out.WriteLine(Instructions());
                        break;
                    }
                }
            }

            Console.Out.WriteLine("Good bye!\nPress ENTER key to continue...");
            Console.In.Read();
        }
Ejemplo n.º 17
0
 public DispenseChange(VendingMachine vendingMachine)
 {
     this.vendingMachine = vendingMachine;
 }
Ejemplo n.º 18
0
 public override void ChangeVendingState(VendingMachine context)
 {
     context.setState(new VendingStockState());
 }
Ejemplo n.º 19
0
 public void SetUp()
 {
     Vm    = new VendingMachine.VendingMachine();
     State = new VendingMachine.States.MoneyInsertedState(Vm);
     Iq    = new ItemQueue(1.5);
 }
Ejemplo n.º 20
0
        // Hopkins, Shawn, hopkinss
        // Exercise 03 - Vending Machine
        // [email protected]

        static void Main(string[] args)
        {
            // 1) Creates and instance of can rack and set maxInventory
            // 2)  Creates an instance of PurchasePrice and sets price
            var vendingMachine = new VendingMachine(1, 75.0m);

            // display the PurchasePrice.Price property
            Console.WriteLine($"The price of a soda is {vendingMachine.PurchasePrice.Price} cents\n");

            // Calls the CanRack.FillTheCanRack method to set the inventory of each flavor to maxinventory
            vendingMachine.CanRack.FillTheCanRack();
            vendingMachine.CanRack.DebugWriteCanRackContents(); // Write canrack inventory to debug window

            bool isVending = true;

            while (isVending)
            {
                Console.WriteLine($"--Current inventory--");
                Console.ForegroundColor = ConsoleColor.Blue;

                // display contents of CanRack for each Can using can.tostring override
                foreach (Content v in vendingMachine.CanRack.Contents().OrderBy(x => (int)x.Flavor))
                {
                    var suf = v.Amount > 1 ? "s" : string.Empty;
                    Console.WriteLine($"{(int)v.Flavor}) There is {v.Amount} can{suf} of {v.Flavor} soda in the rack   ");
                }
                Console.ForegroundColor = ConsoleColor.White;

                // Parse user response into enum
                Console.Write("\nEnter a flavor (hit q to quit): ");

                var exitKey = Console.ReadKey();
                if (exitKey.Key == ConsoleKey.Q)
                {
                    Environment.Exit(0);
                }

                var selection = exitKey.KeyChar.ToString() + Console.ReadLine();
                if (Enum.TryParse <Flavor>(selection, true, out Flavor soda))
                {
                    if (Enum.IsDefined(typeof(Flavor), soda))
                    {
                        // If its not emtpy remove a can
                        if (!vendingMachine.CanRack.IsEmpty(soda))
                        {
                            vendingMachine.CanRack.RemoveACanOf(soda);
                            Console.WriteLine($"Here's your can of {soda} soda\nhit any key to continue");
                            Console.ReadKey();
                        }
                        // Otherwise prompt to add a can
                        else
                        {
                            Console.Write($"\n\nThe {soda} soda is empty. Do you wish to add a can (y/n)? ");

                            if (Console.ReadKey().Key == ConsoleKey.Y)
                            {
                                vendingMachine.CanRack.AddACanOf(soda);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid soda flavor\nhit any key to continue");
                        Console.ReadKey();
                    }
                }
                else
                {
                    Console.WriteLine("Invalid soda flavor\nhit any key to continue");
                    Console.ReadKey();
                }
                Console.Clear();
            }
        }