Skip to content

Commit

Permalink
Merge pull request #203 from ziyi105/184-encode-pantrt-stock-with-regex
Browse files Browse the repository at this point in the history
184 encode pantrt stock with regex
  • Loading branch information
DextheChik3n authored Nov 1, 2023
2 parents f58f1e5 + d88d1df commit 770508a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Decoder {
public static Menu decodeMenuData(ArrayList<String> textLines) {
ArrayList<Dish> menuDishList = new ArrayList<>();
for(String dishString : textLines) {
String[] dishStringArray = dishString.split(" \\| ");
String[] dishStringArray = dishString.split(DIVIDER);
String dishName = dishStringArray[0];
float dishPrice = Float.parseFloat(dishStringArray[1]);
String[] ingredientStringArray = Arrays.copyOfRange(dishStringArray, 2, dishStringArray.length);
Expand Down Expand Up @@ -63,14 +63,19 @@ private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsSt
}

//@@author ziyi105
/**
* Decodes raw string from pantry stock data file and create ingredient object from the data
* @param encodedPantryStock raw string from pantry stock data file
* @return a new pantry object with data from the pantry stock data file
*/
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock) {
ArrayList<Ingredient> pantryStock = new ArrayList<>();

if (encodedPantryStock.isEmpty()) {
return new Pantry(ui);
}
for (String encodedData : encodedPantryStock) {
String[] decodedData = encodedData.split(" ");
String[] decodedData = encodedData.split(DIVIDER);
if (!isValidPantryStockFormat(decodedData)) {
ui.showToUser(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
} else {
Expand All @@ -82,6 +87,12 @@ public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock)
return new Pantry(ui, pantryStock);
}

/**
* Checks whether the pantry stock is in the format of ingredient name | quantity (int) | unit
* @param decodedPantryStock string array of the raw data string from pantry stock data file
* split with "|"
* @return true if the format is correct, false otherwise
*/
private static boolean isValidPantryStockFormat(String[] decodedPantryStock) {
if (decodedPantryStock.length != 3) {
ui.showToUser(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ private static StringBuilder encodeIngredientList(ArrayList<Ingredient> ingredie
}

//@@author ziyi105
/**
* Encodes the pantry stock into format ingredient name | quantity | unit
* @param pantry the pantry from current session
* @return an arrayList of string of ecoded pantry stock
*/
public static ArrayList<String> encodePantryStock(Pantry pantry) {
// Convert pantry stock to a list of String
ArrayList<String> pantryStockInString = new ArrayList<>();
ArrayList<Ingredient> pantryStock = pantry.getPantryStock();
for (Ingredient ingredient : pantryStock) {
String encodedIngredient = ingredient.getName() + " "
+ ingredient.getQty() + " " + ingredient.getUnit();
String encodedIngredient = ingredient.getName() + DIVIDER
+ ingredient.getQty() + DIVIDER + ingredient.getUnit();
pantryStockInString.add(encodedIngredient);
}
return pantryStockInString;
Expand Down

0 comments on commit 770508a

Please sign in to comment.