From 691ca536ea046068731c4e9b50185e55386e354a Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 20 Jan 2022 19:26:55 +0100 Subject: [PATCH] Added getFormattedMenuList method to MenuManager with test for printing to Console --- src/main/java/Application/MenuManager.java | 26 ++++++++++++++----- .../java/Application/MenuManagerTest.java | 7 +++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 987c88e..539ed66 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -7,35 +7,47 @@ public class MenuManager { private ArrayList menuList; private Menu currentMenu; - public MenuManager(){ + public MenuManager() { menuList = new ArrayList<>(); currentMenu = null; } - public void addMenu(Menu menu){ + public void addMenu(Menu menu) { menuList.add(menu); } - public void select(int i){ - if(currentMenu == null) + public void select(int i) { + if (currentMenu == null) this.currentMenu = menuList.get(i); else this.currentMenu = currentMenu.getMenu(i); } public void back() throws Exception { - if(!this.inRootMenu()) + if (!this.inRootMenu()) this.currentMenu = this.currentMenu.getPreviousMenu(); else throw new Exception("Menu is in the root menu, a previous menu doesn't exist"); } - public Menu getCurrentMenu(){ + public Menu getCurrentMenu() { return this.currentMenu; } - public boolean inRootMenu(){ + public boolean inRootMenu() { return this.currentMenu == null; } + public String getFormattedMenuList() { + StringBuilder result = new StringBuilder(); + ArrayList baseMenuList = this.menuList; + if(!inRootMenu()) + baseMenuList = currentMenu.getSubMenuList(); + + for(int i = 0; i < baseMenuList.size(); i++) + result.append(i + 1).append(": ").append(baseMenuList.get(i).getName()).append("\n"); + + return result.toString(); + } + } diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index b1d79c1..83c632a 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -66,4 +66,11 @@ class MenuManagerTest { Assertions.fail("Test failed: " + e.getMessage()); } } + + @Test + void getFormattedMenuList(){ + assertEquals(mm.getFormattedMenuList(), "1: Menu1\n2: Menu2\n"); + mm.select(0); + assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n"); + } } \ No newline at end of file