diff --git a/src/main/java/seedu/dietbook/database/DataBase.java b/src/main/java/seedu/dietbook/database/DataBase.java index d671260c6c..cd40ca85d5 100644 --- a/src/main/java/seedu/dietbook/database/DataBase.java +++ b/src/main/java/seedu/dietbook/database/DataBase.java @@ -121,7 +121,7 @@ public void printAllData() { System.out.println("Finished Printing out all data"); } - // -------- Search functions -------- + // ----- Food search functions ------- /** * This method searchs the whole data base and returns the first food item whose name contains the provided string. @@ -134,6 +134,10 @@ public Food searchFoodByName(String food) { return foodStream().filter(x -> x.getName().contains(food)).findFirst().orElseThrow(); } + public Food searchFoodByIndex(int index) { + return foodStream().skip(index - 1).findFirst().orElseThrow(); + } + /** * This method searchs the whole data base and returns all of the food whose name contains the provided string. * @param food part of the name of the food e.g. chicken diff --git a/src/main/java/seedu/dietbook/database/data.txt b/src/main/java/seedu/dietbook/database/data.txt index 7a211bc4e1..122f849a47 100644 --- a/src/main/java/seedu/dietbook/database/data.txt +++ b/src/main/java/seedu/dietbook/database/data.txt @@ -38,10 +38,15 @@ Ayam Penyet Set|699|0|0|0 Steamed Chicken Set |475|0|0|0 Ikan Grouper Penyet Set|669|0|0|0 &%UP -Michelin Star Restaurant -Bouillabaisse with cock crab and poached lobster|520|45|35|56 -Chicken wings with Reblochon pomme purée|450|25|32|66 -Sea bass with prawn tortellini, fennel purée and white wine sauce|530|76|25|43 +Korean +kimchi fried rice|520|45|35|56 +ginseng chicken|450|25|32|66 +ramen|530|76|25|43 +&%UP +Gong Cha +gong cha green tea|100|0|0|0 +gong cha ooloong tea|100|0|0|0 +gong cha bubble tea|200|0|0|0 &%UP &%UP &%STOP diff --git a/src/main/resources/data.txt b/src/main/resources/data.txt index 7a211bc4e1..6dd0327f19 100644 --- a/src/main/resources/data.txt +++ b/src/main/resources/data.txt @@ -26,22 +26,27 @@ &%START Science canteen Halal Mini Wok -Prawn Mee Soup(Dry)(Large)|490|0|0|0 -Prawn Mee Soup(Dry)(Small)|390|0|0|0 -Fried Hokkien Prawn Mee(Large)|470|0|0|0 -Fried Hokkien Prawn Mee(Small)|350|0|0|0 -Clay Pot Chicken|440|0|0|0 -Black Pepper Chicken|490|0|0|0 +Prawn Mee Soup(Dry)(Large)|490|30|20|26 +Prawn Mee Soup(Dry)(Small)|390|25|15|19 +Fried Hokkien Prawn Mee(Large)|470|40|20|20 +Fried Hokkien Prawn Mee(Small)|350|30|15|15 +Clay Pot Chicken|440|34|15|15 +Black Pepper Chicken|490|34|16|16 &%UP Ayam Penyet -Ayam Penyet Set|699|0|0|0 -Steamed Chicken Set |475|0|0|0 -Ikan Grouper Penyet Set|669|0|0|0 +Ayam Penyet Set|699|45|30|30 +Steamed Chicken Set |475|35|20|20 +Ikan Grouper Penyet Set|669|50|40|50 &%UP -Michelin Star Restaurant -Bouillabaisse with cock crab and poached lobster|520|45|35|56 -Chicken wings with Reblochon pomme purée|450|25|32|66 -Sea bass with prawn tortellini, fennel purée and white wine sauce|530|76|25|43 +Korean +kimchi fried rice|520|45|35|56 +ginseng chicken|450|25|32|66 +ramen|530|76|25|43 +&%UP +Gong Cha +gong cha green tea|100|0|0|0 +gong cha ooloong tea|100|0|0|0 +gong cha bubble tea|200|0|0|0 &%UP &%UP &%STOP diff --git a/src/test/java/seedu/dietbook/database/DataBaseTest.java b/src/test/java/seedu/dietbook/database/DataBaseTest.java index d1823de021..ecff6b75fc 100644 --- a/src/test/java/seedu/dietbook/database/DataBaseTest.java +++ b/src/test/java/seedu/dietbook/database/DataBaseTest.java @@ -12,12 +12,26 @@ public static void main(String[] args) { database.printAllData(); // ---- Using stream version to print ----- - System.out.println("------------ printing using food stream ------------"); - database.foodStream().forEach(System.out::println); + // System.out.println("------------ printing using food stream ------------"); + // database.foodStream().forEach(System.out::println); // ---- Printing out as list ----- - System.out.println("---------- printing food using list --------------"); - database.getFoodList().forEach(System.out::println); + // System.out.println("---------- printing food using list --------------"); + // database.getFoodList().forEach(System.out::println); + + // ---- get food list string ---- + System.out.print(database.getFoodListString()); + + // ---- search food by index test ---- + System.out.println("-------- testing the searchFoodByIndex function --------"); + System.out.println("Input : 1 ## OutPut : " + database.searchFoodByIndex(1)); + System.out.println("Input : 10 ## Output : " + database.searchFoodByIndex(10)); + System.out.println("Trying a negative test case : Input : 10000"); + try { + database.searchFoodByIndex(10000); + } catch (NoSuchElementException e) { + System.out.println("The index is too high!" + e); + } // ---- search food by name test ----- try { diff --git a/src/test/resources/data.txt b/src/test/resources/data.txt new file mode 100644 index 0000000000..6dd0327f19 --- /dev/null +++ b/src/test/resources/data.txt @@ -0,0 +1,53 @@ +##################################################################### +# 3 LEVEL DATA BASE # +# Canteen -----> Store ------> Food # +# Commands : # +# &%START : start reading data from the data base # +# &%STOP : stop reading data from the data base # +# &%UP : goes down 1 level e.g. Canteen ---> Store # +# &%DOWN : goes down 1 level e.g. Canteen ---> Store # +# &%ADD format : adds the item with the given format # +# # +# Comments : any line that starts with # is ignored # +# # +# Canteen format : {name} # +# Store format : {name} # +# Food format : {name}|{Calorie}|{Carb}|{Protein}|{Fat} # +##################################################################### + +###################################################################### +# Version 0.1 : # +# there is only UP, once a store or canteen is # +# specified we automatically go down 1 level , for this version # +# there is no going out of a store and then coming back to add more# +# Units : Calorie : kcal : Carbs : g Protein : g : Fats : g # +###################################################################### + +&%START +Science canteen +Halal Mini Wok +Prawn Mee Soup(Dry)(Large)|490|30|20|26 +Prawn Mee Soup(Dry)(Small)|390|25|15|19 +Fried Hokkien Prawn Mee(Large)|470|40|20|20 +Fried Hokkien Prawn Mee(Small)|350|30|15|15 +Clay Pot Chicken|440|34|15|15 +Black Pepper Chicken|490|34|16|16 +&%UP +Ayam Penyet +Ayam Penyet Set|699|45|30|30 +Steamed Chicken Set |475|35|20|20 +Ikan Grouper Penyet Set|669|50|40|50 +&%UP +Korean +kimchi fried rice|520|45|35|56 +ginseng chicken|450|25|32|66 +ramen|530|76|25|43 +&%UP +Gong Cha +gong cha green tea|100|0|0|0 +gong cha ooloong tea|100|0|0|0 +gong cha bubble tea|200|0|0|0 +&%UP +&%UP +&%STOP +