Skip to content

Commit

Permalink
Merge pull request #322 from ziyi105/271-logger
Browse files Browse the repository at this point in the history
#271 Add logger to EditPrice, Help, Decoder and Encoder
  • Loading branch information
ziyi105 authored Nov 12, 2023
2 parents cc9d80e + 2f24c1e commit ee1d2d2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/seedu/cafectrl/command/EditPriceCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.dish.Dish;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Ui;

import java.util.logging.Logger;

//@@author ziyi105
/**
* Edit the price of a dish of a certain index
Expand All @@ -15,6 +18,7 @@ public class EditPriceCommand extends Command {
+ "edit_price dish/DISH_INDEX price/NEW_PRICE\n"
+ "Example: edit_price dish/1 price/4.50";

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
protected Menu menu;
protected Ui ui;
private final int menuID;
Expand All @@ -32,6 +36,7 @@ public EditPriceCommand(int menuID, float newPrice, Menu menu, Ui ui) {
* Set new price of the dish and show edit price message
*/
public void execute() {
logger.info("Executing EditPriceCommand...");
Dish dish = menu.getDishFromId(this.menuID - Ui.OFFSET_LIST_INDEX);

// Checks for original price
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/cafectrl/command/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.ui.Ui;

import java.util.logging.Logger;

//@@author ziyi105
public class HelpCommand extends Command {
public static final String COMMAND_WORD = "help";
public static final String MESSAGE_USAGE = "To view all commands:\n"
+ COMMAND_WORD;

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
protected Ui ui;


public HelpCommand(Ui ui) {
this.ui = ui;
}

@Override
public void execute() {
logger.info("Executing HelpCommand...");
ui.printLine();
ui.showHelp();
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui,
final Matcher matcher = userInputPattern.matcher(userInput.trim());

if (!matcher.matches()) {
logger.warning("Unmatching regex!");
logger.warning("Unmatched regex!");
return new IncorrectCommand(ErrorMessages.UNKNOWN_COMMAND_MESSAGE, ui);
}

Expand Down Expand Up @@ -150,6 +150,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui,
return prepareShowSalesByDay(arguments, ui, sales, menu);

default:
logger.warning(ErrorMessages.UNKNOWN_COMMAND_MESSAGE);
return new IncorrectCommand(ErrorMessages.UNKNOWN_COMMAND_MESSAGE, ui);
}
}
Expand Down Expand Up @@ -180,6 +181,7 @@ private static Command prepareEditPriceCommand(Menu menu, String arguments, Ui u

// Checks whether the overall pattern of edit price arguments is correct
if (!matcher.find()) {
logger.log(Level.WARNING, "Unmatched regex!");
return new IncorrectCommand(ErrorMessages.MISSING_ARGUMENT_FOR_EDIT_PRICE, ui);
}

Expand All @@ -192,23 +194,27 @@ private static Command prepareEditPriceCommand(Menu menu, String arguments, Ui u
String dishIndexText = matcher.group(dishIndexGroup).trim();

// Check whether the index is empty
if (dishIndexText.equals("")) {
if (dishIndexText.isEmpty()) {
logger.warning("Empty dish index!");
return new IncorrectCommand(ErrorMessages.MISSING_DISH_IN_EDIT_PRICE, ui);
}

dishIndex = Integer.parseInt(dishIndexText);

// Check whether the dish index is valid
if (!menu.isValidDishIndex(dishIndex)) {
logger.warning("Invalid dish index!");
return new IncorrectCommand(ErrorMessages.INVALID_DISH_INDEX, ui);
}
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Invalid dish index type!", e);
return new IncorrectCommand(ErrorMessages.WRONG_DISH_INDEX_TYPE_FOR_EDIT_PRICE, ui);
}

try {
newPrice = parsePriceToFloat(matcher.group(newPriceGroup).trim());
} catch (ParserException e) {
logger.log(Level.WARNING, "Invalid price!", e);
return new IncorrectCommand(e.getMessage(), ui);
}

Expand All @@ -230,7 +236,7 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
try {
// Checks whether the overall pattern of add arguments is correct
if (!matcher.matches()) {
logger.log(Level.WARNING, "Unmatching regex!");
logger.log(Level.WARNING, "Unmatched regex!");
return new IncorrectCommand(ErrorMessages.INVALID_ADD_DISH_FORMAT_MESSAGE
+ AddDishCommand.MESSAGE_USAGE, ui);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ private static void checkUnitValidity(String unit) throws Exception {
* @return a new pantry object with data from the pantry stock data file
*/
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock) {
logger.info("Decoding Pantry_stock.txt to PantryStock...");
ArrayList<Ingredient> pantryStock = new ArrayList<>();
Ingredient ingredient;

if (encodedPantryStock.isEmpty()) {
return new Pantry(ui);
}
for (String encodedData : encodedPantryStock) {
logger.info("Line to decode: " + encodedData);
String[] decodedData = encodedData.split(DIVIDER);
if (!isValidPantryStockFormat(decodedData)) {
ui.showToUser(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
Expand All @@ -116,6 +118,7 @@ public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock)
try {
qty = Integer.parseInt(qtyText);
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Line corrupted: " + e.getMessage(), e);
ui.showToUser(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
continue;
}
Expand All @@ -129,6 +132,7 @@ public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock)
ingredient = new Ingredient(ingredientName, qty, unit);
pantryStock.add(ingredient);
} else {
logger.info(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
ui.showToUser(ErrorMessages.ERROR_IN_PANTRY_STOCK_DATA);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static ArrayList<String> encodePantryStock(Pantry pantry) {
encodedIngredient.append(ingredient.getUnit());
encodedIngredient.append(System.lineSeparator());
pantryStockInString.add(encodedIngredient.toString());
logger.info("Encoded ingredient: " + ingredient.getName());
}
ArrayList<String> pantryStockInStringHashed = hashEncoding(pantryStockInString);
return pantryStockInStringHashed;
Expand Down

0 comments on commit ee1d2d2

Please sign in to comment.