From e427700f90ff679b24413f7ee8b4ed08262c5a43 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 20 Jan 2022 19:10:19 +0100 Subject: [PATCH 1/6] 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()); } From 691ca536ea046068731c4e9b50185e55386e354a Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 20 Jan 2022 19:26:55 +0100 Subject: [PATCH 2/6] 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 From 298fde40551908d875cfe08d80ac701af86b5f71 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 20 Jan 2022 19:29:57 +0100 Subject: [PATCH 3/6] Refactored MenuManagerTest for better naming convention --- src/test/java/Application/MenuManagerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index 83c632a..45e7b65 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -27,7 +27,7 @@ class MenuManagerTest { } @Test - void testSelectFunction() { + void select() { assertNull(mm.getCurrentMenu()); mm.select(0); assertEquals(mm.getCurrentMenu().getName(), "Menu1"); @@ -36,14 +36,14 @@ class MenuManagerTest { } @Test - void testIsRootMenuFunction() { + void inRootMenu() { assertTrue(mm.inRootMenu()); mm.select(0); assertFalse(mm.inRootMenu()); } @Test - void testBackFunction() { + void back() { try { mm.back(); Assertions.fail("Test failed: shouldn't be able to go back in rootMenu"); From 08f8fbc5960501deee15be69eb055f1daeddae0b Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Fri, 21 Jan 2022 00:18:55 +0100 Subject: [PATCH 4/6] Added getSize() method to MenuManager with test --- src/main/java/Application/MenuManager.java | 10 ++++++++-- src/test/java/Application/MenuManagerTest.java | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 539ed66..62944b1 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -16,6 +16,12 @@ public class MenuManager { menuList.add(menu); } + public int getSize() { + if(inRootMenu()) + return menuList.size(); + return currentMenu.getSubMenuList().size(); + } + public void select(int i) { if (currentMenu == null) this.currentMenu = menuList.get(i); @@ -41,10 +47,10 @@ public class MenuManager { public String getFormattedMenuList() { StringBuilder result = new StringBuilder(); ArrayList baseMenuList = this.menuList; - if(!inRootMenu()) + if (!inRootMenu()) baseMenuList = currentMenu.getSubMenuList(); - for(int i = 0; i < baseMenuList.size(); i++) + 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 45e7b65..111bf13 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -73,4 +73,13 @@ class MenuManagerTest { mm.select(0); assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n"); } + + @Test + void getSize() { + assertEquals(mm.getSize(), 2); + mm.select(0); + assertEquals(mm.getSize(), 1); + mm.select(0); + assertEquals(mm.getSize(), 0); + } } \ No newline at end of file From 55f173324acfe0800987d1deefb08b908fe6ca61 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Fri, 21 Jan 2022 00:26:27 +0100 Subject: [PATCH 5/6] Changed select method with test for MenuManager to avoid ArrayIndexOutOfBoundsException --- src/main/java/Application/MenuManager.java | 2 ++ src/test/java/Application/MenuManagerTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 62944b1..4aa1be5 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -23,6 +23,8 @@ public class MenuManager { } public void select(int i) { + if(i < 0 || i >= this.getSize()) + return; if (currentMenu == null) this.currentMenu = menuList.get(i); else diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index 111bf13..3ad5701 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -29,8 +29,18 @@ class MenuManagerTest { @Test void select() { assertNull(mm.getCurrentMenu()); + //Negative index + mm.select(-1); + assertNull(mm.getCurrentMenu()); + mm.select(0); assertEquals(mm.getCurrentMenu().getName(), "Menu1"); + //Dose nothing when index to big + mm.select(1); + assertEquals(mm.getCurrentMenu().getName(), "Menu1"); + + mm.select(0); + assertEquals(mm.getCurrentMenu().getName(), "Menu1.1"); mm.select(0); assertEquals(mm.getCurrentMenu().getName(), "Menu1.1"); } From 823c9d96c4554f541a01a167e380ced04c80b77e Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Fri, 21 Jan 2022 00:32:00 +0100 Subject: [PATCH 6/6] Refactored test names for MenuTest class for better naming convention --- src/test/java/Application/MenuTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/Application/MenuTest.java b/src/test/java/Application/MenuTest.java index a0297a6..d0494b6 100644 --- a/src/test/java/Application/MenuTest.java +++ b/src/test/java/Application/MenuTest.java @@ -29,17 +29,17 @@ class MenuTest { } @Test - void testCorrectName() { + void getName() { assertEquals("Root", rootMenu.getName()); } @Test - void testAddMenuFunction() { + void addMenu() { assertTrue(rootMenu.getMenu(0).getName().equals("Menu1") && rootMenu.getMenu(1).getName().equals("Menu2") && rootMenu.getMenu(0).getMenu(0).getName().equals("Menu1.1")); } @Test - void testPreviousMenuFunction(){ + void getPreviousMenu(){ assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1")); }