diff --git a/src/main/java/Dish.java b/src/main/java/Dish.java new file mode 100644 index 000000000..f9da3d53f --- /dev/null +++ b/src/main/java/Dish.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; + +public class Dish { + + private ArrayList ingredients; + + private String name; + + public Dish(String name, ArrayList ingredients ) { + this.name = name; + this.ingredients = ingredients; + } + + public ArrayList getIngredients() { + return ingredients; + } + +} diff --git a/src/main/java/Menu.java b/src/main/java/Menu.java new file mode 100644 index 000000000..b2fa25367 --- /dev/null +++ b/src/main/java/Menu.java @@ -0,0 +1,52 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +public class Menu { + + private static HashMap dishMap = new HashMap(); + + public static void addDish (String input) { + ArrayList ingredients = parseIngredients(input); + String name = parseName(input); + Dish d = new Dish(name, ingredients); + dishMap.put(name, d); + } + + public static void deleteDish (String name) { + dishMap.remove(name); + } + + public static ArrayList parseIngredients(String input) { + ArrayList ingredients = new ArrayList(); + input = input.substring(input.indexOf("i/") + 2); + input = input.substring(0, input.indexOf(";")); + String[] splitString = input.split(","); + for (String str: splitString) { + ingredients.add(str.trim()); + } + return ingredients; + } + + public static String parseName(String input) { + input = input.substring(input.indexOf("n/") + 2); + input = input.substring(0, input.indexOf(";")); + return input; + } + + public static void printDishes() { + for (String name: dishMap.keySet()) { + String iList = ""; + for (String str: dishMap.get(name).getIngredients()) { + iList += str + ","; + } + iList = iList.substring(0, iList.length()-1); + System.out.println("Name: " + name + "; Ingredients: " + iList); + } + } + + public static HashMap getDishMap() { + return dishMap; + } + +} diff --git a/src/test/java/MenuTest.java b/src/test/java/MenuTest.java new file mode 100644 index 000000000..b2df7d396 --- /dev/null +++ b/src/test/java/MenuTest.java @@ -0,0 +1,57 @@ +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +class MenuTest { + + @Test + public void parseIngredientsTest() { + String d1a = "n/bacon pizza; i/cheese, bacon;"; + ArrayList l1 = new ArrayList(); + l1.add("cheese"); + l1.add("bacon"); + assertEquals(l1, Menu.parseIngredients(d1a)); + String d1b = "i/cheese, bacon; n/bacon pizza;"; + assertEquals(l1, Menu.parseIngredients(d1b)); + String d2 = "i/chicken, rice, chili; n/chicken biryani;"; + ArrayList l2 = new ArrayList(); + l2.add("chicken"); + l2.add("rice"); + l2.add("chili"); + assertEquals(l2, Menu.parseIngredients(d2)); + } + + @Test + public void parseNameTest() { + String d1 = "n/bacon pizza; i/cheese, bacon;"; + assertEquals("bacon pizza", Menu.parseName(d1)); + String d2 = "i/chicken, rice, chili; n/chicken biryani;"; + assertEquals("chicken biryani", Menu.parseName(d2)); + } + + @Test + public void addDishTest() { + String d1 = "n/bacon pizza; i/cheese, bacon;"; + Menu.addDish(d1); + assertTrue(Menu.getDishMap().containsKey("bacon pizza")); + String d2 = "i/chicken, rice, chili; n/chicken biryani;"; + Menu.addDish(d2); + assertTrue(Menu.getDishMap().containsKey("chicken biryani")); + } + + @Test + public void deleteDishTest() { + String d1 = "n/bacon pizza; i/cheese, bacon;"; + Menu.addDish(d1); + assertTrue(Menu.getDishMap().containsKey("bacon pizza")); + String d2 = "i/chicken, rice, chili; n/chicken biryani;"; + Menu.addDish(d2); + assertTrue(Menu.getDishMap().containsKey("chicken biryani")); + Menu.deleteDish("bacon pizza"); + assertFalse(Menu.getDishMap().containsKey("bacon pizza")); + Menu.deleteDish("chicken biryani"); + assertFalse(Menu.getDishMap().containsKey("chicken biryani")); + } + +}