From 95dc251cd8c71836b54bfd667b9b41db137fe8d1 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 13 Jan 2022 14:23:47 +0100 Subject: [PATCH] Added previousMenu function to Menu and did refactoring --- src/main/java/Application/Menu.java | 15 +++++++++++ src/test/java/Application/MenuTest.java | 34 +++++++++++++++---------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/Application/Menu.java b/src/main/java/Application/Menu.java index 95ea678..0651965 100644 --- a/src/main/java/Application/Menu.java +++ b/src/main/java/Application/Menu.java @@ -5,14 +5,17 @@ import java.util.ArrayList; public class Menu { private String name; + private Menu previousMenu; private ArrayList subMenuList; public Menu(String name){ setName(name); + setPreviousMenu(null); subMenuList = new ArrayList<>(); } public void addMenu(Menu menu){ + menu.setPreviousMenu(this); subMenuList.add(menu); } @@ -24,7 +27,19 @@ public class Menu { this.name = name; } + public Menu getMenu(int i){ + return getSubMenuList().get(i); + } + public ArrayList getSubMenuList() { return this.subMenuList; } + + private void setPreviousMenu(Menu menu){ + this.previousMenu = menu; + } + + public Menu getPreviousMenu(){ + return this.previousMenu; + } } diff --git a/src/test/java/Application/MenuTest.java b/src/test/java/Application/MenuTest.java index 3ca3193..a0297a6 100644 --- a/src/test/java/Application/MenuTest.java +++ b/src/test/java/Application/MenuTest.java @@ -6,12 +6,22 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class MenuTest { + 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); } @AfterEach @@ -19,20 +29,18 @@ class MenuTest { } @Test - void testCorrectName(){ - Menu menu = new Menu("Item 1"); - assertTrue(menu.getName().equals("Item 1")); + void testCorrectName() { + assertEquals("Root", rootMenu.getName()); } @Test - void testAddMenuFunction(){ - ArrayList menus = new ArrayList<>(); - Menu menu1 = new Menu("Menu 1"); - Menu menu2 = new Menu("Menu 2"); - Menu menu1_1 = new Menu("Menu 1.1"); - menu1.addMenu(menu1_1); - menus.add(menu1); - menus.add(menu2); - assertTrue(menus.get(0).equals(menu1) && menus.get(1).equals(menu2) && menus.get(0).getSubMenuList().get(0).equals(menu1_1)); + void testAddMenuFunction() { + 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(){ + assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1")); + } + } \ No newline at end of file