Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            GumballMachine gumballMachine = new GumballMachine(5);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static void TestStatePattern()
        {
            GumballMachine machine = new GumballMachine(5);
            int            count   = 0;

            while (true)
            {
                count++;

                Console.WriteLine(machine.ToString());
                Console.ReadKey();

                Console.WriteLine("-------------------------");

                machine.InsertQuarter();
                machine.TurnCrank();
                Console.WriteLine(machine.ToString());
                Console.ReadKey();

                Console.WriteLine("-------------------------");

                machine.InsertQuarter();
                machine.TurnCrank();
                machine.InsertQuarter();
                machine.TurnCrank();
                Console.WriteLine(machine.ToString());

                Console.ReadKey();
                Console.WriteLine(string.Format("*********{0}*********", count));
            }
        }
Ejemplo n.º 3
0
        public void StateTest()
        {
            {
                var c = new 状态模式.基本.Context();
                c.Request();
                c.Request();
                c.Request();
            }
            {
                var mach = new GumballMachine();
                mach.InsertQuarter();
                mach.TurnCrank();
                mach.Dispense();
                mach.Dispense();
                mach.Dispense();

                mach.Print();

                mach.InsertQuarter();
                mach.TurnCrank();
                mach.Dispense();

                mach.Print();
            }
        }
Ejemplo n.º 4
0
        public void State_GumballMachineWinnerState()
        {
            GumballMachine machine = new GumballMachine(10);

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();

            machine.InsertQuarter();
            machine.TurnCrank();
        }
Ejemplo n.º 5
0
        private static void TestState()
        {
            GumballMachine gumballMachine = new GumballMachine(1);

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.ReleaseGum();
            Console.WriteLine("--Inserindo a moeda para alterar o estado.");
            gumballMachine.InsertQuarter();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.InsertQuarter();
            gumballMachine.ReleaseGum();
            Console.WriteLine("--Ejetando a moeda para alterar o estado.");
            gumballMachine.EjectQuarter();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.ReleaseGum();
            Console.WriteLine("--Inserindo novamente a moeda para alterar o estado.");
            gumballMachine.InsertQuarter();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.InsertQuarter();
            gumballMachine.ReleaseGum();
            Console.WriteLine("--Virando a engrenagem.");
            gumballMachine.TurnCrank();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.InsertQuarter();
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine("--Dispensando a goma.");
            gumballMachine.ReleaseGum();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Chamando todos os métodos que não mudam o estado do estado atual: {0}", gumballMachine.GetState());
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.ReleaseGum();
            gumballMachine.EjectQuarter();
            Console.WriteLine("--Repondo as gomas.");
            gumballMachine.Fill();
            Console.WriteLine("--{0} gomas.", gumballMachine.Count);
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("--Estado atual: {0}", gumballMachine.GetState());
        }
Ejemplo n.º 6
0
        public void InsertQuarterTwice()
        {
            GumballMachine gumballMachine = CreateGumballMachine(1);

            gumballMachine.InsertQuarter();
            gumballMachine.InsertQuarter();

            this.AssertMachineMessage(MachineMessages.InsertTwice);
        }
Ejemplo n.º 7
0
        public void TurnCrank_InHasQuarterState_WithBalls_And_Quarters_ShouldChangeGMStateToHasQuarterState()
        {
            var gm = new GumballMachine(5);

            gm.InsertQuarter();
            gm.InsertQuarter();
            gm.TurnCrank();

            Assert.AreEqual(GetGumballMachineString(4, "waiting for turn of crank"), gm.ToString());
        }
Ejemplo n.º 8
0
        public void State_GumballMachineTraversal()
        {
            GumballMachine machine = new GumballMachine(1);

            machine.InsertQuarter();
            machine.EjectQuarter();
            machine.InsertQuarter();
            machine.TurnCrank();
            machine.InsertQuarter();
        }
Ejemplo n.º 9
0
        public void InsertQuarter_InHasQuarterState_ShouldNotChangeGMState()
        {
            var gm = new GumballMachine(5);

            gm.InsertQuarter();
            gm.InsertQuarter();

            Assert.AreEqual("You inserted a quarter. Quarters count: 1\r\nYou inserted a quarter. Quarters count: 2\r\n", sw.ToString());
            Assert.AreEqual(GetGumballMachineString(5, "waiting for turn of crank"), gm.ToString());
        }
Ejemplo n.º 10
0
        public void DispenseTheLastGumball()
        {
            GumballMachine gumballMachine = CreateGumballMachine(1);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();

            this.AssertMachineMessage(MachineMessages.DispenseTheLastGumball);
        }
        public static void Start()
        {
            GumballMachine gumballMachine = new GumballMachine(5);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
        }
Ejemplo n.º 12
0
        public void CanInsertQuarterInHasQuarterState()
        {
            var  strState = "waiting for turn of crank";
            uint gumballs = 1;
            var  testStr  = GetGumballMachineTestParsedString(gumballs, strState);
            var  gM       = new GumballMachine(gumballs);

            gM.InsertQuarter();
            gM.InsertQuarter();
            Assert.AreEqual(gM.GetBallCount(), gumballs);
            Assert.AreEqual(gM.ToString(), testStr);
        }
Ejemplo n.º 13
0
        public void State_GumballMachineRefill()
        {
            GumballMachine machine = new GumballMachine(1);

            machine.InsertQuarter();
            machine.EjectQuarter();
            machine.InsertQuarter();
            machine.TurnCrank();
            machine.Refill(10);
            machine.InsertQuarter();
            Assert.AreEqual(10, machine.GetCount());
        }
Ejemplo n.º 14
0
        public void Refill_InHasQuarterState_ShouldRefill()
        {
            var gm = new GumballMachine(2);

            gm.InsertQuarter();
            gm.InsertQuarter();
            Assert.AreEqual(GetGumballMachineString(2, "waiting for turn of crank"), gm.ToString());

            sb.Remove(0, sb.Length);
            gm.Refill(2);

            Assert.AreEqual("Gumballs refilled. Gumballs count: 4\r\n", sw.ToString());
        }
Ejemplo n.º 15
0
        public void CanInsert2QuartersTurnCrankInHasQuarterStateAndDespenseBallAndMovetoHasQuarterStateIfBallsMoreThan0()
        {
            var  strState = "waiting for turn of crank";
            uint gumballs = 1;
            var  testStr  = GetGumballMachineTestParsedString(gumballs, strState);
            var  gM       = new GumballMachine(2);

            gM.InsertQuarter();
            gM.InsertQuarter();
            gM.TurnCrank();
            Assert.AreEqual(gM.GetBallCount(), gumballs);
            Assert.AreEqual(gM.ToString(), testStr);
        }
Ejemplo n.º 16
0
        public void CanEjectAllQuartersInHasQuarterStateAndMoveToNoQuarterState()
        {
            var  strState = "waiting for quarter";
            uint gumballs = 1;
            var  testStr  = GetGumballMachineTestParsedString(gumballs, strState);
            var  gM       = new GumballMachine(gumballs);

            gM.InsertQuarter();
            gM.InsertQuarter();
            gM.EjectQuarters();
            Assert.AreEqual(gM.GetBallCount(), gumballs);
            Assert.AreEqual(gM.ToString(), testStr);
        }
Ejemplo n.º 17
0
        public void EjectQuarter_InSoldOutState_ShouldReturnAllQuarters()
        {
            var gm = new GumballMachine(1);

            gm.InsertQuarter();
            gm.InsertQuarter();
            gm.TurnCrank();
            sb.Remove(0, sb.Length);
            gm.EjectQuarter();

            Assert.AreEqual("Quarter returned\r\n", sw.ToString());
            Assert.AreEqual(GetGumballMachineString(0, "sold out"), gm.ToString());
        }
Ejemplo n.º 18
0
        public void CanInsert2QuartersTurnCrankInHasQuarterStateAndDespenseBallAndMoveToSoldOutStateIfBalls0AndReturnQuarters()
        {
            var  strState = "sold out";
            uint gumballs = 0;
            var  testStr  = GetGumballMachineTestParsedString(gumballs, strState);
            var  gM       = new GumballMachine(1);

            gM.InsertQuarter();
            gM.InsertQuarter();
            gM.TurnCrank();
            Assert.AreEqual(gM.GetBallCount(), gumballs);
            Assert.AreEqual(gM.ToString(), testStr);
            Assert.AreEqual(gM.GetQuartersCount(), (uint)0);
        }
Ejemplo n.º 19
0
        void Demo1(GumballMachine gumballMachine)
        {
            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.EjectQuarter();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();

            Console.WriteLine(gumballMachine);
        }
Ejemplo n.º 20
0
        public void EjectQuarter_InHasQuarterState_ShouldReturnAllQuarters_And_ChangeGMStateToNoQuarterState()
        {
            var gm = new GumballMachine(5);

            gm.InsertQuarter();
            gm.InsertQuarter();

            Assert.AreEqual("You inserted a quarter. Quarters count: 1\r\nYou inserted a quarter. Quarters count: 2\r\n", sw.ToString());

            sb.Remove(0, sb.Length);
            gm.EjectQuarter();

            Assert.AreEqual(GetGumballMachineString(5, "waiting for quarter"), gm.ToString());
        }
Ejemplo n.º 21
0
        private void RunStateExample()
        {
            var gumBallMachine = new GumballMachine(5);

            gumBallMachine.InsertQuarter();
            gumBallMachine.TurnCrank();
        }
Ejemplo n.º 22
0
        static void Main(string[] args)
        {
            var gumballMachine = new GumballMachine(5);

            var i = 10;

            while (true)
            {
                while (i != 0)
                {
                    Console.WriteLine(gumballMachine);

                    gumballMachine.InsertQuarter();
                    gumballMachine.TurnCrank();

                    Console.WriteLine(gumballMachine);

                    i--;
                }

                Thread.Sleep(5000);
                gumballMachine.Refill(10);
            }

            Console.ReadKey();
        }
Ejemplo n.º 23
0
    static void Main()
    {
        GumballMachine machine = new GumballMachine(4);

        System.Console.WriteLine(machine.ToString());
        machine.InsertQuarter();
        machine.TurnCrank();

        System.Console.WriteLine(machine.ToString());
        machine.InsertQuarter();
        machine.TurnCrank();
        machine.InsertQuarter();
        machine.TurnCrank();

        System.Console.WriteLine(machine.ToString());
    }
Ejemplo n.º 24
0
        public void InsertQuarter_InSoldOutState_ShouldNotChangeGMState()
        {
            var gm = new GumballMachine(0);

            gm.InsertQuarter();

            Assert.AreEqual("You can't insert a quarter, the machine is sold out\r\n", sw.ToString());
        }
Ejemplo n.º 25
0
        public void InsertWhenSoldOut()
        {
            GumballMachine gumballMachine = CreateGumballMachine(NO_GUMBALLS);

            gumballMachine.InsertQuarter();

            this.AssertMachineMessage(MachineMessages.InsertWhenSoldOut);
        }
        public static void Run(string[] args)
        {
            int count = 0;

            if (args.Length < 2)
            {
                Console.WriteLine("GumballMachine <name> <inventory>");
                Environment.Exit(1);
            }

            count = int.Parse(args[1]);
            GumballMachine gumballMachine = new GumballMachine(args[0], count);

            GumballMonitor monitor = new GumballMonitor(gumballMachine);

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.EjectQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.Refill(3);
            Console.WriteLine();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.EjectQuarter();
            Console.WriteLine();

            Console.WriteLine(gumballMachine);

            gumballMachine.InsertQuarter();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            gumballMachine.InsertQuarter();
            gumballMachine.TurnCrank();
            Console.WriteLine();

            Console.WriteLine(gumballMachine);

            gumballMachine.Refill(3);

            monitor.Report();
        }
Ejemplo n.º 27
0
        public void EjectQuarter()
        {
            GumballMachine gumballMachine = CreateGumballMachine(1);

            gumballMachine.InsertQuarter();
            gumballMachine.EjectQuarter();

            this.AssertMachineMessage(MachineMessages.EjectSuccessfully);
        }
Ejemplo n.º 28
0
        public void TurnCrank_InHasQuarterState_WithOneBall_ShouldChangeGMStateToSoldOutState()
        {
            var gm = new GumballMachine(1);

            gm.InsertQuarter();
            gm.TurnCrank();

            Assert.AreEqual(GetGumballMachineString(0, "sold out"), gm.ToString());
        }
 public void GetGumball()
 {
     _gumballMachine.Count.P();
     _gumballMachine.GetStatus().P();
     _gumballMachine.InsertQuarter();
     _gumballMachine.TurnCrank();
     _gumballMachine.Count.P();
     _gumballMachine.GetStatus().P();
 }
Ejemplo n.º 30
0
        public void DispenseWhenHasQuarter()
        {
            GumballMachine gumballMachine = CreateGumballMachine(1);

            gumballMachine.InsertQuarter();
            gumballMachine.Dispense();

            this.AssertMachineMessage(MachineMessages.DispenseWhenHasQuarter);
        }
Ejemplo n.º 31
0
        static void Main(string[] args)
        {
            var gm = new GumballMachine(200, "Tuta");

            for (int i = 0; i < 200; i++)
            {
                gm.InsertQuarter();
                gm.TurnCrank();
            }

            Console.ReadKey();
        }