public MenuStateMachine(IGraphicUserInterface gui)
 {
     this.gui                     = gui;
     this.currentState            = MenuStateMachineState.Initialized;
     this.menuPrinter             = new MenuPrinter(gui);
     this.menuTransitionValidator = new MenuTransitionValidator();
     this.isStarted               = false;
     this.beverageFactory         = new BeverageFactory();
 }
        public void Start()
        {
            isStarted = true;
            while (isStarted)
            {
                int userInput;
                switch (currentState)
                {
                case MenuStateMachineState.Initialized:
                    gui.ClearScreen();
                    menuPrinter.PrintHelloMessage();
                    userInput    = gui.ReadInputInteger();
                    currentState = menuTransitionValidator.ValidateStateTransition(userInput, currentState);
                    break;

                case MenuStateMachineState.OrderingBeverage:
                    gui.ClearScreen();
                    menuPrinter.PrintAvailableBeverages();
                    userInput    = gui.ReadInputInteger();
                    beverage     = beverageFactory.CreateBeverage(userInput);
                    currentState = menuTransitionValidator.ValidateStateTransition(userInput, currentState, beverage);
                    break;

                case MenuStateMachineState.SelectBeverageSize:
                    gui.ClearScreen();
                    menuPrinter.PrintAvailableBeverageSizes();
                    userInput    = gui.ReadInputInteger();
                    currentState = menuTransitionValidator.ValidateStateTransition(userInput, currentState, beverage);
                    beverage.SetBeverageSize(userInput.ToBeverageSize());
                    break;

                case MenuStateMachineState.SelectCoffeeType:
                    gui.ClearScreen();
                    menuPrinter.PrintAvailableCoffeeTypes();
                    userInput    = gui.ReadInputInteger();
                    currentState = menuTransitionValidator.ValidateStateTransition(userInput, currentState, beverage);
                    (beverage as ICoffee).SetCoffeeType(userInput.ToCoffeeType());
                    break;

                case MenuStateMachineState.Finished:
                    gui.ClearScreen();
                    gui.PrintMessageOnLine("Thank you for ordering! Here is your coffee!");
                    gui.PrintMessageOnLine($"Your order: {beverage.GetBeverageDetails()}");
                    gui.PrintMessageOnLine("Please enjoy and come again!");
                    currentState = MenuStateMachineState.Exit;
                    break;

                case MenuStateMachineState.Exit:
                    isStarted = false;
                    break;
                }
            }
        }
示例#3
0
        public MenuStateMachineState ValidateStateTransition(int userInput, MenuStateMachineState currentState, IBeverage beverage = null)
        {
            switch (currentState)
            {
            case MenuStateMachineState.Initialized:
                if (userInput == 1)
                {
                    return(MenuStateMachineState.OrderingBeverage);
                }
                else
                {
                    return(MenuStateMachineState.Exit);
                }

            case MenuStateMachineState.OrderingBeverage:
                if (userInput >= 1 && userInput <= 10)
                {
                    if (beverage.HasStandardSize() && !beverage.HasCoffee())
                    {
                        return(MenuStateMachineState.Finished);
                    }

                    if (beverage.HasStandardSize() && beverage.HasCoffee())
                    {
                        return(MenuStateMachineState.SelectCoffeeType);
                    }

                    if (!beverage.HasStandardSize())
                    {
                        return(MenuStateMachineState.SelectBeverageSize);
                    }
                }
                return(MenuStateMachineState.OrderingBeverage);

            case MenuStateMachineState.SelectBeverageSize:
                if (userInput >= 1 && userInput <= 4)
                {
                    if (beverage.HasCoffee())
                    {
                        return(MenuStateMachineState.SelectCoffeeType);
                    }
                    else
                    {
                        return(MenuStateMachineState.Finished);
                    }
                }
                return(MenuStateMachineState.SelectBeverageSize);

            case MenuStateMachineState.SelectCoffeeType:
                if (userInput >= 1 && userInput <= 2)
                {
                    return(MenuStateMachineState.Finished);
                }
                return(MenuStateMachineState.SelectBeverageSize);

            case MenuStateMachineState.Finished:
                return(MenuStateMachineState.Exit);

            default:
                return(MenuStateMachineState.Exit);
            }
        }