Skip to content

Commit

Permalink
mvp of menu with add and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuthu17 committed Mar 9, 2020
1 parent b9a6f6c commit 71c4034
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/Dish.java
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;
}

}
52 changes: 52 additions & 0 deletions src/main/java/Menu.java
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;
}

}
57 changes: 57 additions & 0 deletions src/test/java/MenuTest.java
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"));
}

}

0 comments on commit 71c4034

Please sign in to comment.