Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 291, Add logging to keep track of program execution #298

Merged
merged 12 commits into from
Nov 10, 2023
Merged
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ text-ui-test/EXPECTED-UNIX.TXT
META-INF/MANIFEST.MF
data/

cafeCtrl.log
cafeCtrl.log.lck
26 changes: 26 additions & 0 deletions src/main/java/seedu/cafectrl/CafeCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
import seedu.cafectrl.storage.Storage;
import seedu.cafectrl.ui.Ui;

import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;


/**
* CafeCtrl application's entry point.
* Initializes the application and starts the interaction with the user.
*/
public class CafeCtrl {

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private final Ui ui;
private Menu menu;
private Command command;
Expand All @@ -28,6 +37,7 @@ public class CafeCtrl {
*/

private CafeCtrl() {
initLogger();
this.ui = new Ui();
this.storage = new Storage(this.ui);
this.sales = new Sales();
Expand All @@ -36,6 +46,7 @@ private CafeCtrl() {
this.sales = this.storage.loadOrderList(menu);
this.currentDate = new CurrentDate(sales);

logger.info( "CafeCtrl initialised successfully");
assert sales.getOrderLists().size() == currentDate.getCurrentDay() + 1;
}

Expand All @@ -54,14 +65,29 @@ private void run() {
ParserUtil parserUtil = new Parser();
command = parserUtil.parseCommand(menu, fullUserInput, ui, pantry, sales, currentDate);
command.execute();
logger.info(command.getClass().getName() + " executed.");
} catch (Exception e) {
logger.log(Level.WARNING, "Error executing command: " + e.getMessage(), e);
ui.showToUser(e.getMessage());
} finally {
ui.printLine();
}
} while (!command.isExit());

this.storage.saveAll(this.menu, this.sales, this.pantry);
logger.info("CafeCtrl terminated.");
}

private void initLogger() {
logger.setUseParentHandlers(false);
try {
FileHandler fileHandler = new FileHandler("cafeCtrl.log");
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/seedu/cafectrl/command/AddDishCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package seedu.cafectrl.command;

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

import java.util.logging.Logger;
//@@author DextheChik3n
/**
* Adds a menu item to the user
Expand All @@ -14,7 +17,7 @@ public class AddDishCommand extends Command {
+ "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n"
+ "Example:"
+ COMMAND_WORD + " name/chicken rice price/3.00 ingredient/rice qty/200g, ingredient/chicken qty/100g";

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

Expand All @@ -26,6 +29,7 @@ public AddDishCommand(Dish dish, Menu menu, Ui ui) {
}
@Override
public void execute() {
logger.info("Executing AddDishCommand...");
menu.addDish(dish);
ui.printAddDishMessage(dish);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/cafectrl/command/AddOrderCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Order;
import seedu.cafectrl.data.OrderList;
Expand All @@ -10,6 +11,7 @@
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.util.logging.Logger;
import java.text.DecimalFormat;

public class AddOrderCommand extends Command {
Expand All @@ -19,7 +21,7 @@ public class AddOrderCommand extends Command {
+ " name/DISH_NAME qty/QUANTITY\n"
+ "Example: " + COMMAND_WORD
+ "name/chicken rice qty/2";

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
protected Pantry pantry;
protected OrderList orderList;
protected Menu menu;
Expand All @@ -36,6 +38,7 @@ public AddOrderCommand(Order order, Ui ui, Pantry pantry, OrderList orderList, M
}
@Override
public void execute() {
logger.info("Executing AddOrderCommand...");
orderList.addOrder(order);
Chef chef = new Chef(order, pantry, ui);
chef.cookDish();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.data.dish.Ingredient;
import seedu.cafectrl.ui.Ui;
import seedu.cafectrl.parser.Parser;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

//@@author ShaniceTang
/**
Expand All @@ -19,13 +22,13 @@ public class BuyIngredientCommand extends Command {
+ "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n"
+ "Example:"
+ COMMAND_WORD + " ingredient/milk qty/200ml, ingredient/chicken qty/100g";

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private static final int FIRST_INDEX = 0;
protected Ui ui;
protected Pantry pantry;
private ArrayList<Ingredient> ingredients;
private ArrayList<Ingredient> ingredientsToBePrinted = new ArrayList<>();
private String ingredientString = ""; // Used to store the message about the bought ingredients
private int FIRST_INDEX = 0;

/**
* Constructs a BuyIngredientCommand with the specified ingredients, user interface, and pantry.
Expand All @@ -45,11 +48,13 @@ public BuyIngredientCommand(ArrayList<Ingredient> ingredients, Ui ui, Pantry pan
*/
@Override
public void execute() {
logger.info("Executing BuyIngredientCommand...");
try {
addIngredient();
ui.printBuyIngredientHeader();
ui.showToUser(ingredientString.strip());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "BuyIngredientCommand unsuccessful: " + e.getMessage(), e);
ui.showToUser(e.getMessage());
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/cafectrl/command/DeleteDishCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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.Level;
import java.util.logging.Logger;

//@@author ShaniceTang
/**
* Deletes a menu item identified using it's last displayed index from the menu.
Expand All @@ -16,6 +20,7 @@ public class DeleteDishCommand extends Command {
+ "Parameters: INDEX\n"
+ "Example: " + COMMAND_WORD + " 1";

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

Expand All @@ -27,12 +32,14 @@ public DeleteDishCommand(int listIndex, Menu menu, Ui ui) {

@Override
public void execute() {
logger.info("Executing DeleteDishCommand...");
try {
int dishIndexToBeDeleted = index - Ui.OFFSET_LIST_INDEX;
Dish selectedDish = menu.getMenuItemsList().get(dishIndexToBeDeleted);
ui.printDeleteMessage(selectedDish);
menu.removeDish(dishIndexToBeDeleted);
} catch (IndexOutOfBoundsException e) {
logger.log(Level.WARNING, "DeleteDishCommand unsuccessful: " + e.getMessage(), e);
ui.showToUser(ErrorMessages.INVALID_DISH_INDEX);
throw new IndexOutOfBoundsException();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/cafectrl/command/IncorrectCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package seedu.cafectrl.command;

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

import java.util.logging.Logger;

/**
* Represents an incorrect command. Upon execution, produces some feedback to the user.
*/
public class IncorrectCommand extends Command{
private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
public final String feedbackToUser;
protected Ui ui;

Expand All @@ -16,6 +20,7 @@ public IncorrectCommand(String feedbackToUser, Ui ui) {

@Override
public void execute() {
logger.warning("Executing IncorrectCommand: " + feedbackToUser);
ui.showToUser(feedbackToUser);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package seedu.cafectrl.command;

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

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Lists all ingredients used in the selected dish to the user.
*/
Expand All @@ -16,6 +20,7 @@ public class ListIngredientCommand extends Command {
+ "Parameters: DISH_INDEX\n"
+ "Example: " + COMMAND_WORD + " 1";

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

Expand All @@ -27,6 +32,7 @@ public ListIngredientCommand(int listIndex, Menu menu, Ui ui) {

@Override
public void execute() {
logger.info("Executing ListIngredientCommand...");
try {
Dish selectedDish = menu.getMenuItemsList().get(index - Ui.OFFSET_LIST_INDEX);
ui.showIngredientsHeader(selectedDish);
Expand All @@ -38,6 +44,7 @@ public void execute() {

ui.showIngredientsEndCap();
} catch (IndexOutOfBoundsException e) {
logger.log(Level.WARNING, "ListIngredientCommand unsuccessful: " + e.getMessage(), e);
throw new IllegalArgumentException(ErrorMessages.UNLISTED_DISH);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/cafectrl/command/ListMenuCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package seedu.cafectrl.command;

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

import java.text.DecimalFormat;
import java.util.logging.Logger;

/**
* Lists all dishes in the menu to the user.
Expand All @@ -15,6 +17,7 @@ public class ListMenuCommand extends Command {
+ COMMAND_WORD;
private static final DecimalFormat dollarValue = new DecimalFormat("0.00");

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private final Menu menu;
private final Ui ui;

Expand All @@ -35,6 +38,7 @@ public ListMenuCommand(Menu menu, Ui ui) {
*/
@Override
public void execute() {
logger.info("Executing ListMenuCommand...");
if (menu.getSize() == 0) {
printEmptyMenu(ui);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Ui;

import java.util.logging.Logger;

//@@author NaychiMin
public class ListSaleByDayCommand extends Command {
public static final String COMMAND_WORD = "list_sale";
public static final String MESSAGE_USAGE = "To show sales for a chosen day:\n"
+ COMMAND_WORD + " day/DAY_TO_DISPLAY\n"
+ "Example: " + COMMAND_WORD + " day/1";

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private final int day;
private final Ui ui;
private final Sales sales;
Expand All @@ -26,11 +30,11 @@ public ListSaleByDayCommand(int day, Ui ui, Sales sales, Menu menu) {

@Override
public void execute() {
logger.info("Executing ShowSalesByDayCommand...");
try {
sales.printSaleByDay(ui, menu, day);
} catch (Exception e) {
ui.showToUser(ErrorMessages.INVALID_SALE_DAY);
}

}
}
6 changes: 6 additions & 0 deletions src/main/java/seedu/cafectrl/command/ListTotalSales.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.ui.Ui;

import java.util.logging.Logger;

public class ListTotalSales extends Command {
public static final String COMMAND_WORD = "list_total_sales";
public static final String MESSAGE_USAGE = "To show sales for all days:\n" + COMMAND_WORD;

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private Sales sales;
private Ui ui;
private Menu menu;
Expand All @@ -19,6 +24,7 @@ public ListTotalSales(Sales sales, Ui ui, Menu menu) {

@Override
public void execute() {
logger.info("Executing ShowSalesCommand...");
sales.printSales(ui, menu);
}
}
6 changes: 5 additions & 1 deletion src/main/java/seedu/cafectrl/command/NextDayCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package seedu.cafectrl.command;

import seedu.cafectrl.CafeCtrl;
import seedu.cafectrl.data.CurrentDate;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.data.OrderList;
import seedu.cafectrl.ui.Ui;

public class NextDayCommand extends Command {
import java.util.logging.Logger;

public class NextDayCommand extends Command {
public static final String COMMAND_WORD = "next_day";
public static final String MESSAGE_USAGE = "To travel to next day:\n"
+ COMMAND_WORD;

private static Logger logger = Logger.getLogger(CafeCtrl.class.getName());
private final Ui ui;
private final Sales sales;
private final CurrentDate currentDate;
Expand All @@ -31,6 +34,7 @@ public NextDayCommand(Ui ui, Sales sales, CurrentDate currentDate) {
*/
@Override
public void execute() {
logger.info("Executing NextDayCommand...");
ui.printLine();
currentDate.nextDay();
int nextDay = currentDate.getCurrentDay();
Expand Down
Loading
Loading