Skip to content

Commit

Permalink
Merge branch 'master' into 301-negative-ingredient-qty-v2.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/seedu/cafectrl/ui/ErrorMessages.java
#	src/main/java/seedu/cafectrl/ui/Messages.java
  • Loading branch information
DextheChik3n committed Nov 12, 2023
2 parents 0dc39bb + c2e5d0c commit 1f53b9c
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class BuyIngredientCommand extends Command {
public static final String COMMAND_WORD = "buy_ingredient";
public static final String MESSAGE_USAGE = "\nTo buy ingredient:\n"
public static final String MESSAGE_USAGE = "To buy ingredient:\n"
+ COMMAND_WORD + " ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY"
+ "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n"
+ "Example:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class ListIngredientCommand extends Command {
public static final String COMMAND_WORD = "list_ingredients";
public static final String MESSAGE_USAGE = "To list out the ingredients needed "
+ "along with the quantity for a specific dish:\n"
+ "Parameters: DISH_INDEX\n"
+ "Example: " + COMMAND_WORD + " 1";
+ "Parameters: index/DISH_INDEX\n"
+ "Example: " + COMMAND_WORD + " index/1";

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
protected Ui ui;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/cafectrl/data/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void printOrderList(Menu menu, Ui ui) {

ui.showSalesAll(aggregatedOrder.getDishName(),
aggregatedOrder.getQuantity(),
dollarValue.format(aggregatedOrder.calculateTotalOrderCost()));
dollarValue.format(aggregatedOrder.getTotalOrderCost()));
}

ui.showSalesBottom();
Expand All @@ -80,10 +80,10 @@ private void aggregateOrder(Order order, ArrayList<Order> aggregatedOrders) {
logger.info("Aggregating order...");
if (order.getIsComplete()) {
int index = getIndexByDishName(aggregatedOrders, order.getDishName());
aggregatedOrders.get(index).setQuantity(aggregatedOrders.get(index).getQuantity()
+ order.getQuantity());
aggregatedOrders.get(index).setTotalOrderCost(aggregatedOrders.get(index).getTotalOrderCost()
+ order.getTotalOrderCost());
aggregatedOrders.get(index)
.setQuantity(aggregatedOrders.get(index).getQuantity() + order.getQuantity());
aggregatedOrders.get(index)
.setTotalOrderCost(aggregatedOrders.get(index).getTotalOrderCost() + order.getTotalOrderCost());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/data/Pantry.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public boolean isDishCooked(ArrayList<Ingredient> dishIngredients) {
*/
private Ingredient getIngredient(Ingredient dishIngredient) {
return pantryStock.stream()
.filter(ingredient -> ingredient.getName().trim().equals(dishIngredient.getName().trim()))
.filter(ingredient -> ingredient.getName().trim().equalsIgnoreCase(dishIngredient.getName().trim()))
.findFirst()
.orElse(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/data/Sales.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* The Sales class represents sales data over a period of time, maintaining a collection of order lists.
*/
public class Sales {
public static final int DAY_DISPLAY_OFFSET = 1;
private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private static final int DAY_DISPLAY_OFFSET = 1;
private static ArrayList<OrderList> orderLists;
private int daysAccounted;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/cafectrl/data/dish/Dish.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class Dish {
public Dish(String name, ArrayList<Ingredient> ingredients, float price) {
this.name = name;
this.ingredients = ingredients;
this.price = price;
this.price = Math.abs(price);
}
public Dish(String name, float price) {
this.name = name;
this.ingredients = null;
this.price = price;
this.ingredients = new ArrayList<>();
this.price = Math.abs(price);
}

public String getName() {
Expand All @@ -36,7 +36,7 @@ public String getPriceString() {
}

public void setPrice(float newPrice) {
this.price = newPrice;
this.price = Math.abs(newPrice);
}

@Override
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Parser implements ParserUtil {
+ "qty/([A-Za-z0-9\\s]+)";

/** The rest of Command Handler Patterns*/
private static final String LIST_INGREDIENTS_ARGUMENT_STRING = "(.+)";
private static final String LIST_INGREDIENTS_ARGUMENT_STRING = "index/(.+)";
private static final String DELETE_ARGUMENT_STRING = "(\\d+)";
private static final String EDIT_PRICE_ARGUMENT_STRING = "dish/(.*)\\sprice/(.*)";
private static final String BUY_INGREDIENT_ARGUMENT_STRING = "(ingredient/[A-Za-z0-9\\s]+ qty/[A-Za-z0-9\\s]+"
Expand Down Expand Up @@ -135,7 +135,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui,
return prepareBuyIngredient(arguments, ui, pantry, menu);

case HelpCommand.COMMAND_WORD:
return prepareHelpCommand(ui);
return prepareHelpCommand(ui, arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand(ui, pantry);
Expand Down Expand Up @@ -568,14 +568,18 @@ private static Command prepareListIngredient(Menu menu, String arguments, Ui ui)
}

try {
int dishIndex = Integer.parseInt(matcher.group(1));
int dishIndex = Integer.parseInt(matcher.group(1).trim());

if (dishIndex < 0) {
throw new Exception();
}

if (!menu.isValidDishIndex(dishIndex)) {
return new IncorrectCommand(ErrorMessages.UNLISTED_DISH, ui);
}

return new ListIngredientCommand(dishIndex, menu, ui);
} catch (NumberFormatException e) {
} catch (Exception e) {
return new IncorrectCommand(ErrorMessages.INVALID_DISH_INDEX_TO_LIST, ui);
}
}
Expand Down Expand Up @@ -691,8 +695,12 @@ public static boolean containsSpecialChar(String text) {
}

//@@author ziyi105
private static Command prepareHelpCommand(Ui ui) {
return new HelpCommand(ui);
private static Command prepareHelpCommand(Ui ui, String arguments) {
if (arguments.isEmpty()) {
return new HelpCommand(ui);
} else {
return new IncorrectCommand(ErrorMessages.WRONG_HELP_FORMAT, ui);
}
}

//@@author Cazh1
Expand Down Expand Up @@ -797,9 +805,12 @@ private static Command prepareShowSalesByDay(String arguments, Ui ui, Sales sale
}

try {
int day = Integer.parseInt(matcher.group(1));
int day = Integer.parseInt(matcher.group(1).trim());
if (day < 0) {
throw new Exception();
}
return new ListSaleByDayCommand(day, ui, sales, menu);
} catch (NumberFormatException e) {
} catch (Exception e) {
return new IncorrectCommand(ErrorMessages.INVALID_DAY_FORMAT, ui);
}
}
Expand Down
111 changes: 79 additions & 32 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import seedu.cafectrl.data.dish.Ingredient;
import seedu.cafectrl.parser.Parser;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.util.ArrayList;
Expand All @@ -31,6 +30,7 @@ public class Decoder {
private static final String INGREDIENT_DIVIDER = " - ";
private static final Ui ui = new Ui();
private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());

//@@author ShaniceTang
/**
* Decodes an ArrayList of string lines into a Menu object, reconstructing its content.
Expand Down Expand Up @@ -87,6 +87,7 @@ private static void checkUnitValidity(String unit) throws Exception {
throw new Exception();
}
}
//@@author

//@@author ziyi105
/**
Expand Down Expand Up @@ -125,6 +126,7 @@ public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock)

// Check whether the parameters are correct
if (!Parser.containsSpecialChar(ingredientName)
&& !Parser.isNameLengthInvalid(ingredientName)
&& !Parser.isRepeatedIngredientName(ingredientName, pantryStock)
&& !Parser.isInvalidQty(qty)
&& !Parser.isEmptyUnit(unit)
Expand Down Expand Up @@ -170,48 +172,95 @@ public static Sales decodeSales(ArrayList<String> textLines, Menu menu) {
logger.info("Decoding orders.txt to Sales...");
boolean salesOrderTextTamperDetectionMessagePrinted = false;
ArrayList<OrderList> orderLists = new ArrayList<>();

if (textLines.isEmpty()) {
return new Sales();
}

//for each 'order' in text file
for (String line : textLines) {
logger.info("Line to decode: " + line);
try {
String[] orderData = line.split(DIVIDER);
int day = Integer.parseInt(orderData[0].trim()) - 1;
String dishName = orderData[1].trim();
if (dishName.equals(Encoder.NULL_ORDER_DAY)) {
orderLists = fillOrderListSize(orderLists, day);
continue;
}
int quantity = Integer.parseInt(orderData[2].trim());
float totalOrderCost = Float.parseFloat(orderData[3].trim());
boolean isComplete = "true".equals(orderData[4].trim());
Dish dish = menu.getDishFromName(dishName);
if (dish == null) {
ui.showDecodedInvalidDish(dishName);
continue;
}
Order orderedDish = new Order(menu.getDishFromName(dishName), quantity, totalOrderCost, isComplete);
//increase size of orderLists if needed
//this can be used in the event that the text file's first order is not day 0
orderLists = fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
} catch (IndexOutOfBoundsException e) {
ui.showToUser(Messages.SALES_LAST_DAY_TEXT_TAMPERED, System.lineSeparator());
} catch (NumberFormatException e) {
if (!salesOrderTextTamperDetectionMessagePrinted) {
ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator());
salesOrderTextTamperDetectionMessagePrinted = true;
}
if (line.isEmpty()) {
continue;
}
decodeSalesData(line, orderLists, menu);
}
if (orderLists.isEmpty()) {
return new Sales();
}
return new Sales(orderLists);
}

/**
* Decodes the sales data from a single order line and adds it to the list of OrderList objects.
*
* @param orderLine The order line in the format "day|dishName|quantity|totalOrderCost|isComplete".
* @param orderLists The list of OrderList objects to which the decoded order will be added.
* @param menu Menu instance to retrieve Dish objects based on dishName.
*/
private static void decodeSalesData(String orderLine, ArrayList<OrderList> orderLists, Menu menu) {
try {
String[] orderData = orderLine.split(DIVIDER);
int day = Integer.parseInt(orderData[0].trim()) - Sales.DAY_DISPLAY_OFFSET;
String dishName = orderData[1].trim();

//@@author Cazh1
//keeps track of the number of days cafe has been operating for
if (dishName.equals(Encoder.NULL_ORDER_DAY)) {
fillOrderListSize(orderLists, day);
return;
}

int quantity = Integer.parseInt(orderData[2].trim());
float decodedDishPrice = Float.parseFloat(orderData[3].trim());
String completeStatus = orderData[4].trim();
float totalOrderCost = quantity * decodedDishPrice;

Dish dish = menu.getDishFromName(dishName);
boolean isDataAccurate = isDishValid(orderLine, dish)
&& isCompleteStatusAccurate(orderLine, completeStatus);
if (!isDataAccurate) {
return;
}

Dish dishToAdd = new Dish(dishName, decodedDishPrice);
//creates new order and adds to orderList for specific day
boolean isComplete = Boolean.parseBoolean(completeStatus.toLowerCase());
Order orderedDish = new Order(dishToAdd, quantity, totalOrderCost, isComplete);
fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
} catch (Exception e) {
logger.log(Level.WARNING, "Line corrupted: " + e.getMessage(), e);
System.out.println(e.getMessage());
ui.showToUser(ErrorMessages.INVALID_SALES_DATA + orderLine);
}
}

/**
* Checks if the Dish is valid (exists in menu) and shows an error message if it's not.
*
* @param orderLine The order line in the format "day|dishName|quantity|totalOrderCost|isComplete".
* @param dish The Dish object to be validated.
* @return True if the Dish is valid, false otherwise.
*/
private static boolean isDishValid(String orderLine, Dish dish) {
if (dish != null) {
return true;
}
ui.showToUser(ErrorMessages.INVALID_ORDER_DATA + orderLine);
return false;
}

private static boolean isCompleteStatusAccurate(String orderLine, String completeStatus) {
if (completeStatus.equalsIgnoreCase("true")
|| completeStatus.equalsIgnoreCase("false")) {
return true;
}
ui.showToUser(ErrorMessages.INVALID_ORDER_STATUS + orderLine);
return false;
}


//@@author Cazh1
/**
* Increases the size of the orderlist when there is gap between the previous order and the next
Expand All @@ -220,11 +269,9 @@ public static Sales decodeSales(ArrayList<String> textLines, Menu menu) {
* @param day The day of the next order
* @return orderLists after filling in the gaps
*/
private static ArrayList<OrderList> fillOrderListSize(ArrayList<OrderList> orderLists, int day) {
private static void fillOrderListSize(ArrayList<OrderList> orderLists, int day) {
while (orderLists.size() <= day) {
orderLists.add(new OrderList());
}
return orderLists;
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static ArrayList<String> encodeSales(Sales sales) {
orderString.append((day + 1) + DIVIDER);
orderString.append(order.getDishName() + DIVIDER);
orderString.append(order.getQuantity() + DIVIDER);
orderString.append(order.calculateTotalOrderCost() + DIVIDER);
orderString.append(order.getOrderedDish().getPrice() + DIVIDER);
orderString.append(order.getIsComplete());
orderString.append(System.lineSeparator());
encodedList.add(String.valueOf(orderString));
Expand Down
Loading

0 comments on commit 1f53b9c

Please sign in to comment.