From e427700f90ff679b24413f7ee8b4ed08262c5a43 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 20 Jan 2022 19:10:19 +0100 Subject: [PATCH] Changed Menumanager implementation from using a single rootMenu to using a ArrayList directly for its submenus --- src/main/java/Application/MenuManager.java | 23 +++++++++++++------ .../java/Application/MenuManagerTest.java | 18 +++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 383e5f8..987c88e 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -1,24 +1,33 @@ package Application; +import java.util.ArrayList; + public class MenuManager { - private Menu rootMenu; + private ArrayList menuList; private Menu currentMenu; - public MenuManager(Menu rootMenu){ - this.rootMenu = rootMenu; - this.currentMenu = rootMenu; + public MenuManager(){ + menuList = new ArrayList<>(); + currentMenu = null; + } + + public void addMenu(Menu menu){ + menuList.add(menu); } public void select(int i){ - this.currentMenu = currentMenu.getMenu(i); + if(currentMenu == null) + this.currentMenu = menuList.get(i); + else + this.currentMenu = currentMenu.getMenu(i); } public void back() throws Exception { if(!this.inRootMenu()) this.currentMenu = this.currentMenu.getPreviousMenu(); else - throw new Exception("Menu is a root menu, a previous menu doesn't exist"); + throw new Exception("Menu is in the root menu, a previous menu doesn't exist"); } public Menu getCurrentMenu(){ @@ -26,7 +35,7 @@ public class MenuManager { } public boolean inRootMenu(){ - return this.currentMenu.equals(this.rootMenu); + return this.currentMenu == null; } } diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index d6031de..b1d79c1 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -10,18 +10,16 @@ import static org.junit.jupiter.api.Assertions.*; class MenuManagerTest { MenuManager mm; - Menu rootMenu; @BeforeEach void setUp() { - rootMenu = new Menu("Root"); Menu menu1 = new Menu("Menu1"); Menu menu2 = new Menu("Menu2"); Menu menu1_1 = new Menu("Menu1.1"); menu1.addMenu(menu1_1); - rootMenu.addMenu(menu1); - rootMenu.addMenu(menu2); - mm = new MenuManager(rootMenu); + mm = new MenuManager(); + mm.addMenu(menu1); + mm.addMenu(menu2); } @AfterEach @@ -30,11 +28,11 @@ class MenuManagerTest { @Test void testSelectFunction() { - assertEquals(mm.getCurrentMenu(), rootMenu); + assertNull(mm.getCurrentMenu()); mm.select(0); - assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0)); + assertEquals(mm.getCurrentMenu().getName(), "Menu1"); mm.select(0); - assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0).getMenu(0)); + assertEquals(mm.getCurrentMenu().getName(), "Menu1.1"); } @Test @@ -55,7 +53,7 @@ class MenuManagerTest { mm.select(0); try { mm.back(); - assertEquals(mm.getCurrentMenu(), rootMenu); + assertTrue(mm.inRootMenu()); } catch (Exception e) { Assertions.fail("Test failed: " + e.getMessage()); } @@ -63,7 +61,7 @@ class MenuManagerTest { mm.select(0); try { mm.back(); - assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0)); + assertEquals(mm.getCurrentMenu().getName(), "Menu1"); } catch (Exception e) { Assertions.fail("Test failed: " + e.getMessage()); }