Skip to content

Commit

Permalink
Handle tampering of menu.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaniceTang committed Nov 13, 2023
1 parent 4df42b0 commit 1357238
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private static void decodeDishString(String dishString, ArrayList<Dish> menuDish
try {
String[] dishStringArray = dishString.split(DIVIDER);
String dishName = dishStringArray[0].trim();
checkNameValidity(dishName);
float dishPrice = Float.parseFloat(dishStringArray[1]);
String[] ingredientStringArray = Arrays.copyOfRange(dishStringArray, 2, dishStringArray.length);
ArrayList<Ingredient> ingredientsList = decodeIngredientData(ingredientStringArray);
Expand All @@ -68,6 +69,12 @@ private static void decodeDishString(String dishString, ArrayList<Dish> menuDish
}
}

private static void checkNameValidity(String name) throws Exception {
if (Parser.isNameLengthInvalid(name) || name.isEmpty() || Parser.containsSpecialChar(name)) {
throw new Exception();
}
}

/**
* Decodes an array of strings representing ingredient data into a list of Ingredient objects.
*
Expand All @@ -80,14 +87,22 @@ private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsSt
logger.info("Ingredient to decode: " + ingredientString);
String[] array = ingredientString.split(INGREDIENT_DIVIDER);
String name = array[0].trim();
checkNameValidity(name);
int qty = Integer.parseInt(array[1].trim());
checkQtyValidity(qty);
String unit = array[2].trim();
checkUnitValidity(unit);
ingredientList.add(new Ingredient(name, qty, unit));
}
return ingredientList;
}

private static void checkQtyValidity(int qty) throws Exception {
if (Parser.isInvalidQty(qty)) {
throw new Exception();
}
}

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

0 comments on commit 1357238

Please sign in to comment.