forked from nus-cs2113-AY1920S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2113-AY1920S2#5 from gmuthu17/master
mvp of menu with add and delete
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.ArrayList; | ||
|
||
public class Dish { | ||
|
||
private ArrayList<String> ingredients; | ||
|
||
private String name; | ||
|
||
public Dish(String name, ArrayList<String> ingredients ) { | ||
this.name = name; | ||
this.ingredients = ingredients; | ||
} | ||
|
||
public ArrayList<String> getIngredients() { | ||
return ingredients; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
|
||
public class Menu { | ||
|
||
private static HashMap<String, Dish> dishMap = new HashMap<String, Dish>(); | ||
|
||
public static void addDish (String input) { | ||
ArrayList<String> 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<String> parseIngredients(String input) { | ||
ArrayList<String> ingredients = new ArrayList<String>(); | ||
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<String, Dish> getDishMap() { | ||
return dishMap; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> l1 = new ArrayList<String>(); | ||
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<String> l2 = new ArrayList<String>(); | ||
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")); | ||
} | ||
|
||
} |