diff --git a/MODEL.md b/MODEL.md new file mode 100644 index 00000000..7f2a7b7f --- /dev/null +++ b/MODEL.md @@ -0,0 +1,7 @@ +### class Basket + +| Methods | Members | Scenario | Output | +|--------------------------------------|---------------------------|--------------------------------------------------------------------------------------------------|---------| +| `add(String bagelName)` | `ArrayList items` | Adds a bagel item to basket
If basket is full, we tell the user it is | boolean | +| `remove(String bagleName)` | `ArrayList items` | Removes a bagel item from the basket and returns true
In case the bagel is not in the list we | boolean | +| `changeBasketCapacity(int capacity)` | `int capacity` | Alters the basket's capacity | void | diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..a5507154 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,46 @@ package com.booleanuk.core; +import java.util.ArrayList; + public class Basket { + private int maxCapcity = 5; + private ArrayList items = new ArrayList<>(); + + public boolean add(String bagelName) { + if (items.contains(bagelName) || maxCapcity < items.size()) return false; + items.add(bagelName); + return true; + } + + public boolean remove(String bagelName) { + if (!items.contains(bagelName)) return false; + items.remove(bagelName); + return true; + } + + public void changeBasketCapacity(int capacity) { + if (capacity <= 0) return; + + maxCapcity = capacity; + + if (maxCapcity < items.size()) { + ArrayList _newItemListing = new ArrayList<>(); + + for (int i = 0; i < maxCapcity; i++) + _newItemListing.add(items.get(i)); + + items = _newItemListing; + } + } + + public int getCapacity() { + return maxCapcity; + } + public String getItemsAsString() { + StringBuilder _sb = new StringBuilder(); + for (String item : items) + _sb.append(" - ").append(item).append("\n"); + return _sb.toString(); + } } diff --git a/src/main/java/com/migzus/terminal/menus/Button.java b/src/main/java/com/migzus/terminal/menus/Button.java new file mode 100644 index 00000000..c31e4e4e --- /dev/null +++ b/src/main/java/com/migzus/terminal/menus/Button.java @@ -0,0 +1,17 @@ +package com.migzus.terminal.menus; + +public class Button { + public boolean disabled = false; + public final String displayName; + + private final Callable callable; + + public Button(String name, Callable callback) { + displayName = name; + callable = callback; + } + + public void onSelect() { + callable.call(); + } +} diff --git a/src/main/java/com/migzus/terminal/menus/Callable.java b/src/main/java/com/migzus/terminal/menus/Callable.java new file mode 100644 index 00000000..8d3eac57 --- /dev/null +++ b/src/main/java/com/migzus/terminal/menus/Callable.java @@ -0,0 +1,39 @@ +package com.migzus.terminal.menus; + +import java.lang.reflect.Method; + +public class Callable { + private Method method; + private final Object obj; + + public Callable(Object obj, String methodName) { + try { + method = obj.getClass().getMethod(methodName); + } + catch (Exception e) { + System.out.println(e.getMessage()); + System.out.println("There are no methods with the name " + methodName + " in the " + obj.getClass().getName() + " class!"); + } + + this.obj = obj; + } + + public Object call(Object...args) { + if (method == null) { + System.out.println("Invalid call. Method was not found therefore you cannot invoke the method."); + return null; + } + + try { + return method.invoke(obj, args); + } + catch (Exception e) { + System.out.println(e.getMessage()); + System.out.println("Cannot invoke call on callback! method variable is: " + method); + } + + return null; + } + + public Object getTargetReference() { return obj; } +} diff --git a/src/main/java/com/migzus/terminal/menus/Menu.java b/src/main/java/com/migzus/terminal/menus/Menu.java new file mode 100644 index 00000000..9e3687c7 --- /dev/null +++ b/src/main/java/com/migzus/terminal/menus/Menu.java @@ -0,0 +1,119 @@ +package com.migzus.terminal.menus; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Menu { + public static Menu currentActiveMenu; + public Menu parentMenu; + + public Callable afterMenuPrintCallback; + + public String name = "Menu"; + + private int selectIndex; + private final ArrayList