From 13572381eff659279dc9c922c31d9b150f5028b4 Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Mon, 13 Nov 2023 15:58:29 +0800 Subject: [PATCH] Handle tampering of menu.txt --- src/main/java/seedu/cafectrl/storage/Decoder.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 80d1909538..961f102d52 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -58,6 +58,7 @@ private static void decodeDishString(String dishString, ArrayList 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 ingredientsList = decodeIngredientData(ingredientStringArray); @@ -68,6 +69,12 @@ private static void decodeDishString(String dishString, ArrayList 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. * @@ -80,7 +87,9 @@ private static ArrayList 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)); @@ -88,6 +97,12 @@ private static ArrayList decodeIngredientData(String[] ingredientsSt 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();