Skip to content

Commit

Permalink
Merge branch 'master' into #159-Update-DG-with-Multi-day-Order-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Cazh1 committed Nov 1, 2023
2 parents f907eeb + f78c56a commit 9c7f19c
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 133 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ bin/
/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
META-INF/MANIFEST.MF
data/menu.txt
data/pantry_stock.txt
data/orders.txt
data/

6 changes: 0 additions & 6 deletions data/orders.txt

This file was deleted.

Empty file removed data/pantry_stock.txt
Empty file.
23 changes: 9 additions & 14 deletions src/main/java/seedu/cafectrl/CafeCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import seedu.cafectrl.parser.Parser;
import seedu.cafectrl.parser.ParserUtil;
import seedu.cafectrl.storage.Storage;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.io.IOException;

/**
* CafeCtrl application's entry point.
* Initializes the application and starts the interaction with the user.
Expand All @@ -29,18 +26,15 @@ public class CafeCtrl {
/**
* Private constructor for the CafeCtrl class, used for initializing the user interface and menu list.
*/
private CafeCtrl() throws IOException {

private CafeCtrl() {
this.ui = new Ui();
this.ui.showToUser(Messages.INITIALISE_STORAGE_MESSAGE);
this.storage = new Storage(this.ui);
this.currentDate = new CurrentDate();
this.sales = new Sales();
this.menu = this.storage.loadMenu();
this.pantry = this.storage.loadPantryStock(menu);
this.pantry = this.storage.loadPantryStock();
this.sales = this.storage.loadOrderList(menu);
currentDate = new CurrentDate();
}

private void setup() {
ui.showWelcome();
}

/**
Expand All @@ -49,7 +43,8 @@ private void setup() {
* <p> This method consistently receives user input, parses commands, and executes the respective command
* until the user enters a "bye" command, terminating the application.</p>
*/
private void run() throws IOException {
private void run() {
ui.showWelcome();
ui.printLine();
do {
try {
Expand All @@ -63,12 +58,12 @@ private void run() throws IOException {
ui.printLine();
}
} while (!command.isExit());

this.storage.saveAll(this.menu, this.sales, this.pantry);
}

public static void main(String[] args) throws IOException {
public static void main(String[] args) {
CafeCtrl cafeCtrl = new CafeCtrl();
cafeCtrl.setup();
cafeCtrl.run();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/data/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public float getTotalOrderListCost() {
public void printOrderList(Menu menu, Ui ui) {
ArrayList<Order> aggregatedOrders = menu.getAggregatedOrders();
if (orderList.isEmpty()) {
ui.showToUser("No orders for this day.");
ui.showToUser("No sales for this day.");
return;
}

Expand Down
57 changes: 43 additions & 14 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.cafectrl.data.Order;
import seedu.cafectrl.data.OrderList;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.parser.exception.ParserException;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.data.Menu;
Expand All @@ -35,6 +36,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* Parse everything received from the users on terminal
* into a format that can be interpreted by other core classes
Expand Down Expand Up @@ -183,7 +185,7 @@ private static Command prepareEditPriceCommand(Menu menu, String arguments, Ui u
return new IncorrectCommand(ErrorMessages.INVALID_DISH_INDEX, ui);
}
return new EditPriceCommand(dishIndex, newPrice, menu, ui);
} catch (IllegalArgumentException e) {
} catch (ParserException e) {
return new IncorrectCommand(ErrorMessages.WRONG_ARGUMENT_TYPE_FOR_EDIT_PRICE, ui);
}
}
Expand All @@ -210,22 +212,23 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
float price = parsePriceToFloat(matcher.group(PRICE_MATCHER_GROUP_LABEL));
String ingredientsListString = matcher.group(INGREDIENTS_MATCHER_GROUP_LABEL);

if (isNameLengthInvalid(dishName)) {
throw new ParserException(ErrorMessages.INVALID_DISH_NAME_LENGTH_MESSAGE);
}

if (isRepeatedDishName(dishName, menu)) {
return new IncorrectCommand(Messages.REPEATED_DISH_MESSAGE, ui);
throw new ParserException(Messages.REPEATED_DISH_MESSAGE);
}

ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString);

Dish dish = new Dish(dishName, ingredients, price);

return new AddDishCommand(dish, menu, ui);
} catch (IllegalArgumentException e) {
return new IncorrectCommand(ErrorMessages.INVALID_ADD_DISH_FORMAT_MESSAGE
+ AddDishCommand.MESSAGE_USAGE, ui);
} catch (ArithmeticException e) {
return new IncorrectCommand(ErrorMessages.INVALID_PRICE_MESSAGE, ui);
} catch (NullPointerException e) {
return new IncorrectCommand(ErrorMessages.NULL_DISH_NAME_MESSAGE, ui);
return new IncorrectCommand(ErrorMessages.NULL_NAME_DETECTED_MESSAGE, ui);
} catch (Exception e) {
return new IncorrectCommand(e.getMessage(), ui);
}
}

Expand All @@ -234,9 +237,10 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
* @param ingredientsListString user's input string of ingredients, multiple ingredients seperated by ',' is allowed
* @return Ingredient objects that consists of the dish
* @throws IllegalArgumentException if the input string of ingredients is in an incorrect format.
* @throws ParserException if the input string does not match the constraints
*/
private static ArrayList<Ingredient> parseIngredients(String ingredientsListString)
throws IllegalArgumentException {
throws IllegalArgumentException, ParserException {
String[] inputIngredientList = {ingredientsListString};
ArrayList<Ingredient> ingredients = new ArrayList<>();

Expand All @@ -252,7 +256,8 @@ private static ArrayList<Ingredient> parseIngredients(String ingredientsListStri
Matcher ingredientMatcher = ingredientPattern.matcher(inputIngredient);

if (!ingredientMatcher.matches()) {
throw new IllegalArgumentException();
throw new ParserException(ErrorMessages.INVALID_ADD_DISH_FORMAT_MESSAGE
+ AddDishCommand.MESSAGE_USAGE);
}

String ingredientName = ingredientMatcher.group(INGREDIENT_NAME_REGEX_GROUP_LABEL).trim();
Expand All @@ -261,6 +266,10 @@ private static ArrayList<Ingredient> parseIngredients(String ingredientsListStri

int ingredientQty = Integer.parseInt(ingredientQtyString);

if (isNameLengthInvalid(ingredientName)) {
throw new ParserException(ErrorMessages.INVALID_INGREDIENT_NAME_LENGTH_MESSAGE);
}

Ingredient ingredient = new Ingredient(ingredientName, ingredientQty, ingredientUnit);

ingredients.add(ingredient);
Expand All @@ -275,12 +284,12 @@ private static ArrayList<Ingredient> parseIngredients(String ingredientsListStri
* @return price in float format
* @throws ArithmeticException if price > 10000000000.00
*/
public static float parsePriceToFloat(String priceText) throws ArithmeticException {
static float parsePriceToFloat(String priceText) throws ParserException {
float price = Float.parseFloat(priceText);
float maxPriceValue = (float) 10000000000.00;

if (price > maxPriceValue) {
throw new ArithmeticException();
throw new ParserException(ErrorMessages.INVALID_PRICE_MESSAGE);
}

return price;
Expand All @@ -291,8 +300,9 @@ public static float parsePriceToFloat(String priceText) throws ArithmeticExcepti
* @param inputDishName dish name entered by the user
* @param menu contains all the existing Dishes
* @return boolean of whether a repeated dish name is detected
* @throws NullPointerException if the input string is null
*/
public static boolean isRepeatedDishName(String inputDishName, Menu menu) throws NullPointerException {
static boolean isRepeatedDishName(String inputDishName, Menu menu) throws NullPointerException {
if (inputDishName == null) {
throw new NullPointerException();
}
Expand All @@ -309,6 +319,26 @@ public static boolean isRepeatedDishName(String inputDishName, Menu menu) throws
return false;
}

/**
* Checks the length of the name is too long
* @param inputName name
* @return boolean of whether the name is more than max character limit set
* @throws NullPointerException if the input string is null
*/
static boolean isNameLengthInvalid(String inputName) throws NullPointerException {
int maxNameLength = 35;

if (inputName == null) {
throw new NullPointerException();
}

if (inputName.length() > maxNameLength) {
return true;
}

return false;
}

//@@author NaychiMin
/**
* Parses arguments in the context of the ListIngredient command.
Expand Down Expand Up @@ -376,7 +406,6 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant

String ingredientsListString = matcher.group(0);


try {
ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString);
return new BuyIngredientCommand(ingredients, ui, pantry);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/cafectrl/parser/exception/ParserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package seedu.cafectrl.parser.exception;
/**
* Represents a parse error encountered by parser.
*/
public class ParserException extends Exception {
/**
* @param errorMessage contains relevant information on failed constraint(s)
*/
public ParserException(String errorMessage) {
super(errorMessage);
}
}
6 changes: 4 additions & 2 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsSt
}

//@@author ziyi105
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock, Menu menu) {
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock) {
ArrayList<Ingredient> pantryStock = new ArrayList<>();

if (encodedPantryStock.isEmpty()) {
Expand Down Expand Up @@ -107,7 +107,9 @@ private static boolean isValidPantryStockFormat(String[] decodedPantryStock) {
*/
public static Sales decodeSales(ArrayList<String> textLines, Menu menu) {
ArrayList<OrderList> orderLists = new ArrayList<>();

if(textLines.isEmpty()) {
return new Sales();
}
//for each 'order' in text file
for (String line : textLines) {
String[] orderData = line.split(DIVIDER);
Expand Down
Loading

0 comments on commit 9c7f19c

Please sign in to comment.