Nick Stolbov
3 years ago
3 changed files with 80 additions and 27 deletions
-
51src/main/java/Application/MenuManager.java
-
50src/test/java/Application/MenuManagerTest.java
-
6src/test/java/Application/MenuTest.java
@ -1,32 +1,61 @@ |
|||||
package Application; |
package Application; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
|
||||
public class MenuManager { |
public class MenuManager { |
||||
|
|
||||
private Menu rootMenu; |
|
||||
|
private ArrayList<Menu> menuList; |
||||
private Menu currentMenu; |
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 int getSize() { |
||||
|
if(inRootMenu()) |
||||
|
return menuList.size(); |
||||
|
return currentMenu.getSubMenuList().size(); |
||||
} |
} |
||||
|
|
||||
public void select(int i){ |
|
||||
this.currentMenu = currentMenu.getMenu(i); |
|
||||
|
public void select(int i) { |
||||
|
if(i < 0 || i >= this.getSize()) |
||||
|
return; |
||||
|
if (currentMenu == null) |
||||
|
this.currentMenu = menuList.get(i); |
||||
|
else |
||||
|
this.currentMenu = currentMenu.getMenu(i); |
||||
} |
} |
||||
|
|
||||
public void back() throws Exception { |
public void back() throws Exception { |
||||
if(!this.inRootMenu()) |
|
||||
|
if (!this.inRootMenu()) |
||||
this.currentMenu = this.currentMenu.getPreviousMenu(); |
this.currentMenu = this.currentMenu.getPreviousMenu(); |
||||
else |
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(){ |
|
||||
|
public Menu getCurrentMenu() { |
||||
return this.currentMenu; |
return this.currentMenu; |
||||
} |
} |
||||
|
|
||||
public boolean inRootMenu(){ |
|
||||
return this.currentMenu.equals(this.rootMenu); |
|
||||
|
public boolean inRootMenu() { |
||||
|
return this.currentMenu == null; |
||||
|
} |
||||
|
|
||||
|
public String getFormattedMenuList() { |
||||
|
StringBuilder result = new StringBuilder(); |
||||
|
ArrayList<Menu> 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(); |
||||
} |
} |
||||
|
|
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue