コード例 #1
0
ファイル: MainMenu.cs プロジェクト: OlleSandahl/BibliotekP-
        public void ShowMenu(IStateContext context)
        {
            Console.Clear();
            Console.WriteLine("MainMenu\n");
            Console.WriteLine("1. Reserve book/s");
            Console.WriteLine("2. Return book/s");
            Console.WriteLine("3. Exit\n");
            Console.Write("Choice: ");

            int choice;

            int.TryParse(Console.ReadLine(), out choice);

            switch (choice)
            {
            case 1:
                context.SetState(new ReserveBookState());
                break;

            case 2:
                context.SetState(new ReturnBook());
                break;

            case 3:
                Environment.Exit(0);
                break;

            default:
                context.SetState(new MainMenu());
                break;
            }
        }
コード例 #2
0
        public void Handle(IStateContext <StateTypeA> context)
        {
            Console.WriteLine(StateType.ToString() + " is handling context.");

            // change context state
            context.SetState(new CloseState());
        }
コード例 #3
0
        public void Handle(IStateContext <StateTypeA> context)
        {
            var inputA = context.Input as InputA;

            Console.WriteLine("Input Param:" + inputA.Name);
            Console.WriteLine(StateType.ToString() + " is handling context.");

            // change context state
            context.SetState(new QueryState());
        }
コード例 #4
0
        public void ShowMenu(IStateContext context)
        {
            Console.Clear();
            Console.WriteLine("Booking Book Menu");

            IList <IBook> availableBooks = context.Library.GetAvailabeBooks();

            foreach (IBook book in availableBooks)
            {
                Console.WriteLine($"ISBN: {book.ISBN}\tTitel: {book.Title}");
            }
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("(0 = Finish booking)");
            Console.WriteLine(" ");

            int res = 0;

            do
            {
                res = Convert.ToInt32(Console.ReadLine());
                if (res != 0)
                {
                    IBook book = context.Library.GetBookFromISBN(res);
                    books.Add(book);
                    Console.WriteLine("The book with the ISBN; " + book.ISBN + " has been added to your bookings");
                }
            } while (res != 0);

            Console.WriteLine(" ");
            Console.WriteLine("Enter the membership number");
            int memberNr;

            int.TryParse(Console.ReadLine(), out memberNr);
            int librarienNr = context.logIn.Librarian.LibrNr;

            IBooking booking = context.Library.ReservBook(books, librarienNr, memberNr);

            Console.WriteLine(" ");
            Console.WriteLine("BookingNr: " + booking.BookingNr);
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("Press enter to get back to the main menu");
            Console.WriteLine();

            context.SetState(new MainMenu());
        }
コード例 #5
0
        public void ShowMenu(IStateContext context)
        {
            while (context.logIn == null)
            {
                Console.Clear();
                Console.WriteLine("Library Log-In/n");

                int libNr;
                Console.Write("LibrarienNr: ");
                int.TryParse(Console.ReadLine(), out libNr);

                Console.WriteLine("Password: ");
                string password = Console.ReadLine();

                context.logIn = context.Library.LogingIn(libNr, password);
            }
            context.SetState(new MainMenu());
        }
コード例 #6
0
ファイル: ReturnBook.cs プロジェクト: OlleSandahl/BibliotekP-
        public void ShowMeny(IStateContext context)
        {
            Console.Clear();
            Console.WriteLine("Return booking meny\n");

            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("please input your bookingnumber: ");
            int bookingNr;

            int.TryParse(Console.ReadLine(), out bookingNr);
            IList <IBook> books = context.Library.GetReservedBooks(bookingNr);

            foreach (IBook book in books)
            {
                Console.WriteLine(" ");
                Console.WriteLine($"Isbn: {book.ISBN}\tTitle {book.Title}");
            }
            Console.WriteLine(" ");


            Console.WriteLine("Please verify your bookingnumber to complete the return: ");
            int.TryParse(Console.ReadLine(), out bookingNr);

            IInvoice invoice = context.Library.ReturnBook(bookingNr);

            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine("Invoice");
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine($"\nInvoicenumber: {invoice.InvoiceNr}\tPrice: {invoice.TotalPrice}" + " Swedish crones");
            context.Library.ReturnBook(bookingNr);
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("Press enter to get to the start menu");
            Console.ReadLine();
            context.SetState(new MainMenu());
        }
コード例 #7
0
 public void HandleRejection()
 {
     System.Diagnostics.Debug.WriteLine("But I was told to have the job. What happened?!");
     _context.SetState("Normal");
 }
コード例 #8
0
ファイル: StateBase.cs プロジェクト: dbeuchler/MORYX-Platform
        /// <summary>
        /// Create a state machine of the given base type and will set it on the given context
        /// Will internally called by the <see cref="StateMachine"/> wrapper class
        /// </summary>
        internal static void Create <TStateBase>(IStateContext context, int?initialKey)
            where TStateBase : StateBase
        {
            var stateBaseType = typeof(TStateBase);

            // Check the base type
            if (!stateBaseType.IsAbstract)
            {
                throw new ArgumentException("The state base class must be abstract!");
            }

            // Load all fields
            // 1. Get all fields which are static constant with the attribute
            // 2. let attribute and create an anonymous array
            var definedStates = (from stateField in GetStateFields(stateBaseType)
                                 let att = stateField.GetCustomAttribute <StateDefinitionAttribute>()
                                           select new { Key = (int)stateField.GetValue(null), att.IsInitial, att.Type }).ToArray();

            if (definedStates.Length == 0)
            {
                throw new InvalidOperationException("There was no state constant defined in the given base type." +
                                                    $"There musst be at least one constant integer attributed with the {nameof(StateDefinitionAttribute)}.");
            }

            // If a initial key is set, we check if it exists
            if (initialKey.HasValue && definedStates.All(s => s.Key != initialKey.Value))
            {
                throw new InvalidOperationException($"There was no state defined with key: {initialKey}");
            }

            // Group by type to find multiple defined state types
            var duplicates = definedStates.GroupBy(state => state.Type).Where(g => g.Count() > 1).Select(g => g.Key).ToArray();

            if (duplicates.Any())
            {
                var typeNames = string.Join(", ", duplicates.Select(type => type.Name));
                throw new InvalidOperationException($"State types are only allowed once: {typeNames}");
            }

            var       stateMap     = new StateMap();
            StateBase initialState = null;

            foreach (var definedState in definedStates)
            {
                var instance = (StateBase)Activator.CreateInstance(definedState.Type, context, stateMap);
                instance.Key = definedState.Key;

                if (initialKey.HasValue && initialKey.Value == definedState.Key)
                {
                    initialState = instance;
                }
                else if (definedState.IsInitial && initialState == null)
                {
                    initialState = instance;
                }
                else if (definedState.IsInitial && initialState != null)
                {
                    throw new InvalidOperationException("At least one state must be flagged as '" +
                                                        $"{nameof(StateDefinitionAttribute.IsInitial)} = true'.");
                }

                stateMap.Add(definedState.Key, instance);
            }

            if (initialState == null)
            {
                throw new InvalidOperationException("There is no state flagged with " +
                                                    $"'{nameof(StateDefinitionAttribute.IsInitial)} = true'.");
            }

            context.SetState(initialState);
            initialState.OnEnter();
        }
コード例 #9
0
 public void ApplyForJob()
 {
     _context.SetState("Applied");
 }
コード例 #10
0
 public void HandleRejection()
 {
     System.Diagnostics.Debug.WriteLine("Rejected from interview, alchol anyone?! :-(");
     _context.SetState("Normal"); _context.SetState("Normal");
 }
コード例 #11
0
 public void HandleRejection()
 {
     _context.SetState("Normal");
 }