public string GetUserOption(MenuContainer menuContainer) { do { // Clear console before output of header Console.Clear(); // Print header PrintHeader(menuContainer.header); // Print out each menu item option PrintMenuItems(menuContainer.menuItems); // Get user input, pauses the loop string userInput = GetKeyChoice(); // Find the menu that the user chose MenuItem chosenMenu = menuContainer.menuItems.Find(x => (x.key == userInput)); // Execute chosen menu action, if there is one. if (chosenMenu != null) { // Check if we should auto exit this menu after action if (chosenMenu.autoExit > 0) { autoExitCount += chosenMenu.autoExit; } return userInput; } } while (true); }
public void PrintMenu(MenuContainer menuContainer) { nestedMenuCount++; do { // Clear console before output of header Console.Clear(); // Print header PrintHeader(menuContainer.header); // Print out each menu item option PrintListItems(menuContainer.textLines); // Print out each menu item option PrintMenuItems(menuContainer.menuItems); // Print optional footer PrintFooter(menuContainer.footer); // Display B for Back, or X for Exit. PrintBackOrExitKeyOptions(); // Get user input, pauses the loop string userInput = GetKeyChoice(); // Get out of menu if user pressed "B" for back or "X" for exit if (nestedMenuCount > 1 && userInput == "B" || nestedMenuCount == 1 && userInput == "X") { nestedMenuCount--; break; } // Find the menu that the user chose MenuItem chosenMenu = menuContainer.menuItems.Find(x => (x.key == userInput)); // Execute chosen menu action, if there is one. if (chosenMenu != null) { ExecuteMenuAction(chosenMenu); // Check if we should auto exit this menu after action if (chosenMenu.autoExit > 0) { autoExitCount += chosenMenu.autoExit; } } // Auto exit if (autoExitCount > 0) { autoExitCount--; nestedMenuCount--; break; } } while (true); }
private void DeleteMemberMenu(object member) { Member m = (Member)member; MenuContainer menu = new MenuContainer("Confirm your choise"); menu.menuItems.Add(new MenuItem("Y", string.Format("Do you really want to delete {0}", m.Name), DeleteMember,member, 3)); _menuView.PrintMenu(menu); }
private void PrintMemberInfo(object member) { // Cast object type to member Member m = (Member)member; MenuContainer menu = new MenuContainer(String.Format("{0} ({1})", m.Name, m.PersonalNumber)); menu.textLines.Add("Boats:"); int boatCount = 1; foreach (Boat b in m.Boats) { menu.menuItems.Add( new MenuItem( boatCount.ToString(), String.Format("{0} {1} meters long", b.BoatType, b.BoatLength), PrintMemberBoat, b ) ); boatCount++; } menu.menuItems.Add(new MenuItem("")); menu.menuItems.Add(new MenuItem("A", "Add boat", PrintAddBoat, m, 2)); menu.menuItems.Add(new MenuItem("")); menu.menuItems.Add(new MenuItem("N", "Change name", PrintEditMemberName, m, 2)); menu.menuItems.Add(new MenuItem("P", "Change personal number", PrintEditPersonalNumber, m, 2)); menu.menuItems.Add(new MenuItem("D", "Delete this member", DeleteMemberMenu, m)); menu.footer = m.Boats.Count > 0 ? "Pick a boat, add a new one or edit member." : "This person has no boats."; _menuView.PrintMenu(menu); }
private void PrintVerboseList() { MenuContainer menu = new MenuContainer("Verbose List"); foreach (Member member in _memberService.MemberList) { menu.menuItems.Add( new MenuItem( member.Id.ToString(), string.Format("{0} ({1})", member.Name, member.PersonalNumber), PrintMemberInfo, member ) ); foreach (Boat b in member.Boats) { menu.menuItems.Add(new MenuItem(String.Format(" → {0} {1} meters long", b.BoatType, b.BoatLength))); } } menu.footer = "Pick a member."; _menuView.PrintMenu(menu); }
private void PrintMemberBoat(object boat) { // Cast object type to boat Boat b = (Boat)boat; Member m = _memberService.GetMember(b.MemberId); MenuContainer menu = new MenuContainer(m.Name); menu.textLines.Add(String.Format("Boat type: {0}", b.BoatType)); menu.textLines.Add(String.Format("Length: {0}", b.BoatLength)); menu.menuItems.Add(new MenuItem("D", "Delete boat", DeleteMemberBoat, b, 2)); menu.menuItems.Add(new MenuItem("T", "Change boat type", PrintEditBoatType, b, 2)); menu.menuItems.Add(new MenuItem("L", "Change length", PrintEditBoatLength, b, 2)); menu.footer = "What would you like to do?"; _menuView.PrintMenu(menu); }
private void PrintMainMenu() { MenuContainer menu = new MenuContainer("Welcome to the happy boat club!"); menu.menuItems.Add(new MenuItem("1", "View compact list", PrintCompactList)); menu.menuItems.Add(new MenuItem("2", "View verbose list", PrintVerboseList)); menu.menuItems.Add(new MenuItem("3", "Add new member", AddNewMember)); _menuView.PrintMenu(menu); }
private void PrintEditBoatType(object boat) { Boat b = (Boat)boat; MenuContainer menu = new MenuContainer("Choose new boat type"); menu.menuItems.Add(new MenuItem("S", "Sailboat")); menu.menuItems.Add(new MenuItem("M", "Motorsailer")); menu.menuItems.Add(new MenuItem("C", "Canoe")); menu.menuItems.Add(new MenuItem("O", "Other")); switch (_menuView.GetUserOption(menu)) { case "S": b.BoatType = "Sailboat"; break; case "M": b.BoatType = "Motorsailer"; break; case "C": b.BoatType = "Canoe"; break; case "O": b.BoatType = "Other"; break; } // Try to save input try { _memberService.SaveBoat(b.MemberId, b); } catch (Exception e) { // Print out error _menuView.PrintError(e.Message); } }
private void PrintCompactList() { MenuContainer menu = new MenuContainer("Compact list"); foreach (Member member in _memberService.MemberList){ menu.menuItems.Add( new MenuItem( member.Id.ToString(), String.Format("{0} ({1}) {2} Boats", member.Name, member.PersonalNumber, member.Boats.Count), PrintMemberInfo, member ) ); } menu.footer = "Pick a member."; _menuView.PrintMenu(menu); }
private void PrintAddBoat(object member) { Member m = (Member)member; Boat b = new Boat(); string boatlength = _menuView.GetUserInputLine("Enter new boat length (meters with 1 decimal)"); decimal boatLengthDecimal; if (!decimal.TryParse(boatlength, out boatLengthDecimal)) { _menuView.PrintError(); return; } b.BoatLength = boatLengthDecimal; // Validate input if (!Validate(m)) { ShowErrorMessages(); } else { MenuContainer menu = new MenuContainer("Choose new boat type"); menu.menuItems.Add(new MenuItem("S", "Sailboat")); menu.menuItems.Add(new MenuItem("M", "Motorsailer")); menu.menuItems.Add(new MenuItem("C", "Canoe")); menu.menuItems.Add(new MenuItem("O", "Other")); switch (_menuView.GetUserOption(menu)) { case "S": b.BoatType = "Sailboat"; break; case "M": b.BoatType = "Motorsailer"; break; case "C": b.BoatType = "Canoe"; break; case "O": b.BoatType = "Other"; break; } // Try to save input try { _memberService.SaveBoat(m, b); } catch (Exception e) { // Print out error _menuView.PrintError(e.Message); } } }