Skip to content

Commit

Permalink
Merge pull request #317 from ShaniceTang/303-corrupted-menu-file
Browse files Browse the repository at this point in the history
Task 303, Handle corrupted menu.txt file
  • Loading branch information
NaychiMin authored Nov 11, 2023
2 parents 527098c + 182e7e1 commit 181a257
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/AboutUs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Display | Name | Github Profile | Portfolio
--------|:--------:|:----------------------------------------:|:---------:
![](../images/aboutUs/shanice.jpg) | Shanice | [Github](https://github.com/ShaniceTang) | [Portfolio](team/shanicetang.md)
![](images/aboutUs/shanice.jpg) | Shanice | [Github](https://github.com/ShaniceTang) | [Portfolio](team/shanicetang.md)
![](https://via.placeholder.com/100.png?text=Photo) | Naychi | [Github](https://github.com/NaychiMin/tp) | [Portfolio](team/NaychiMin.md)
![](https://via.placeholder.com/100.png?text=Photo) | Zi Yi | [Github](https://github.com/ziyi105) | [Portfolio](team/ziyi105.md)
![](https://via.placeholder.com/100.png?text=Photo) | Dexter Hoon | [Github](https://github.com/DextheChik3n) | [Portfolio](team/dexthechik3n.md)
Expand Down
Binary file added docs/images/aboutUs/shanice.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 22 additions & 4 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;


Expand All @@ -27,6 +28,7 @@
public class Decoder {

private static final String DIVIDER = "\\| ";
private static final String INGREDIENT_DIVIDER = " - ";
private static final Ui ui = new Ui();
private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
//@@author ShaniceTang
Expand All @@ -41,14 +43,23 @@ public static Menu decodeMenuData(ArrayList<String> textLines) {
ArrayList<Dish> menuDishList = new ArrayList<>();
for(String dishString : textLines) {
logger.info("Line to decode: " + dishString);
decodeDishString(dishString, menuDishList);
}
return new Menu(menuDishList);
}

private static void decodeDishString(String dishString, ArrayList<Dish> menuDishList) {
try {
String[] dishStringArray = dishString.split(DIVIDER);
String dishName = dishStringArray[0].trim();
float dishPrice = Float.parseFloat(dishStringArray[1]);
String[] ingredientStringArray = Arrays.copyOfRange(dishStringArray, 2, dishStringArray.length);
ArrayList<Ingredient> ingredientsList = decodeIngredientData(ingredientStringArray);
menuDishList.add(new Dish(dishName, ingredientsList, dishPrice));
} catch (Exception e) {
logger.log(Level.WARNING, "Line corrupted: " + e.getMessage(), e);
ui.showToUser(ErrorMessages.INVALID_MENU_DATA + dishString);
}
return new Menu(menuDishList);
}

/**
Expand All @@ -57,19 +68,26 @@ public static Menu decodeMenuData(ArrayList<String> textLines) {
* @param ingredientsStringArray An array of strings containing encoded ingredient data.
* @return An ArrayList of Ingredient objects containing the decoded ingredient information.
*/
private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsStringArray) {
private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsStringArray) throws Exception {
ArrayList<Ingredient> ingredientList = new ArrayList<>();
for(String ingredientString : ingredientsStringArray) {
logger.info("Ingredient to decode: " + ingredientString);
String[] array = ingredientString.split(" ");
String[] array = ingredientString.split(INGREDIENT_DIVIDER);
String name = array[0].trim();
int qty = Integer.parseInt(array[1]);
int qty = Integer.parseInt(array[1].trim());
String unit = array[2].trim();
checkUnitValidity(unit);
ingredientList.add(new Ingredient(name, qty, unit));
}
return ingredientList;
}

private static void checkUnitValidity(String unit) throws Exception {
if (!Parser.isValidUnit(unit) || Parser.isEmptyUnit(unit)) {
throw new Exception();
}
}

//@@author ziyi105
/**
* Decodes raw string from pantry stock data file and create ingredient object from the data
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class Encoder {
public static final String NULL_ORDER_DAY = "the last day has no orders but please account for it";
private static final String DIVIDER = " | ";
private static final String INGREDIENT_DIVIDER = " - ";
private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
//@@author ShaniceTang
/**
Expand All @@ -36,7 +37,7 @@ public static ArrayList<String> encodeMenu(Menu menu) {
for(Dish dish : menuDishList) {
StringBuilder dishString = new StringBuilder();
dishString.append(dish.getName() + DIVIDER);
dishString.append(dish.getPrice() + DIVIDER);
dishString.append(dish.getPrice());
dishString.append(encodeIngredientList(dish.getIngredients()));
dishString.append(System.lineSeparator());
menuStringList.add(String.valueOf(dishString));
Expand All @@ -54,9 +55,10 @@ public static ArrayList<String> encodeMenu(Menu menu) {
private static StringBuilder encodeIngredientList(ArrayList<Ingredient> ingredientList) {
StringBuilder ingredientListString = new StringBuilder();
for(Ingredient ingredient : ingredientList) {
ingredientListString.append(ingredient.getName() + " ");
ingredientListString.append(ingredient.getQty() + " ");
ingredientListString.append(ingredient.getUnit() + DIVIDER);
ingredientListString.append(DIVIDER);
ingredientListString.append(ingredient.getName() + INGREDIENT_DIVIDER);
ingredientListString.append(ingredient.getQty() + INGREDIENT_DIVIDER);
ingredientListString.append(ingredient.getUnit());
}
return ingredientListString;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ErrorMessages {
+ "price can only have up to 2 decimal place!";
public static final String MISSING_DISH_IN_EDIT_PRICE = "Sorry, I didnt catch the dish index, "
+ "did you forget to include it in your command?";
public static final String INVALID_MENU_DATA = "menu.txt: Invalid format, this dish will be removed -> ";

public static final String NAME_CANNOT_CONTAIN_SPECIAL_CHAR = "Is there a special character"
+ "in the name?\n I have poor memory and am unable to remember names with special characters"
Expand Down

0 comments on commit 181a257

Please sign in to comment.