Skip to content

Commit

Permalink
Merge pull request #110 from CEGLincoln/master
Browse files Browse the repository at this point in the history
Changed the command system
  • Loading branch information
saradj authored Nov 5, 2019
2 parents 0939186 + 176c180 commit 4a90c87
Show file tree
Hide file tree
Showing 30 changed files with 288 additions and 350 deletions.
69 changes: 30 additions & 39 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,61 @@
import duke.command.ViewTodoListCommand;
import duke.command.ingredientCommand.ExitCommand;
import duke.command.ingredientCommand.RemoveAllExpired;
import duke.dish.Dish;
import duke.dish.DishList;
import duke.exception.DukeException;
import duke.fridge.Fridge;
import duke.ingredient.Ingredient;
import duke.order.Order;
import duke.order.OrderList;
import duke.parser.Parser;
import duke.storage.FridgeStorage;
import duke.storage.OrderStorage;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;

import java.io.IOException;

/**
* MAIN CLASS DUKE, start from main function.
*/
public class Duke {

private FridgeStorage fridgeStorage;
private Storage<Order> orderStorage;
private TaskList tasks;
private OrderStorage orderStorage;
private Ui ui;
private DishList dish;
private OrderList order;
private Fridge fridge;

private final String fridgeFilePath = "data/fridge.txt";
private final String orderFilePath = "data/order.txt";

public enum Type {
INGREDIENT, DISH, ORDER
}

/**
* The constructor method for Duke.
*
* @param fridgeFilePath used to specify the location of the fridge storage file in the hard disc.
* @param orderFilePath used to specify the location of the order storage file in the hard disc.
*/
public Duke(String fridgeFilePath, String orderFilePath) throws DukeException {
public Duke() {
dish = new DishList();
order = new OrderList();
fridge = new Fridge();
ui = new Ui();
fridgeStorage = new FridgeStorage(fridgeFilePath);
orderStorage = new OrderStorage(orderFilePath);
try {
fridgeStorage = new FridgeStorage(fridgeFilePath);
orderStorage = new OrderStorage(orderFilePath);
fridge = new Fridge(fridgeStorage);
order = new OrderList(orderStorage.getEntries().getAllEntries());
} catch (DukeException e) {
ui.showLoadingError();
e.printStackTrace();
tasks = new TaskList();
}
}

/**
* The execution core of the Duke class.
*/
public void run() throws IOException, InterruptedException {
public void run() {
String fullCommand;
//ui.clearScreen();
ui.showWelcome();

if (fridge.hasExpiredIngredients()) {
ui.showHasExpiring();
fullCommand = ui.readCommand();
Expand All @@ -74,10 +67,10 @@ public void run() throws IOException, InterruptedException {
ui.show(fridge.getExpiredIngredients().toString());
}
}
ui.showLine();

ui.showLine();
boolean isExit = false;
boolean back = false;

while (!isExit) {
try {
ui.chefDrawing();
Expand All @@ -93,19 +86,19 @@ public void run() throws IOException, InterruptedException {
}
case "q": {
Command command = new ExitCommand();
command.execute(null, ui, null);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
case "t": {
Command command = new ViewTodoListCommand();
command.execute(order, ui, orderStorage);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
case "a": {
Command<Ingredient> command = new RemoveAllExpired(fridge);
command.execute(fridge.getAllIngredients(), ui, fridgeStorage);
Command command = new RemoveAllExpired(fridge);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
Expand All @@ -121,7 +114,7 @@ public void run() throws IOException, InterruptedException {
}
if (fullCommand.trim().equals("q")) {
Command command = new ExitCommand();
command.execute(null, ui, null);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
Expand All @@ -133,8 +126,8 @@ public void run() throws IOException, InterruptedException {
ui.showIngredientTemplate();
continue;
}
Command<Ingredient> command = Parser.parse(fullCommand, Type.INGREDIENT);
command.execute(fridge.getAllIngredients(), ui, fridgeStorage);
Command command = Parser.parse(fullCommand, Type.INGREDIENT);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
} catch (DukeException e) {
System.out.println(e.getLocalizedMessage());
// e.printStackTrace();
Expand All @@ -152,16 +145,16 @@ public void run() throws IOException, InterruptedException {
if (fullCommand.trim().equals("back")) { break; }
if (fullCommand.trim().equals("q")) {
Command command = new ExitCommand();
command.execute(null, ui, null);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
if (fullCommand.trim().equals("template")) {
ui.showOrderTemplate();
continue;
}
Command<Order> command = Parser.parse(fullCommand, Type.ORDER);
command.execute(order, ui, orderStorage);
Command command = Parser.parse(fullCommand, Type.ORDER);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
} catch (DukeException e) {
System.out.println(e.getLocalizedMessage());
}
Expand All @@ -176,19 +169,19 @@ public void run() throws IOException, InterruptedException {
//ui.clearScreen();
if(fullCommand.trim().equals("q")) {
Command command = new ExitCommand();
command.execute(null, ui, null);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
isExit = command.isExit();
break;
}
if(fullCommand.trim().equals("back")) {
break;
}
if(fullCommand.trim().equals("template")) {
ui.clearScreen();
//ui.clearScreen();
continue;
}
Command<Dish> command = Parser.parse(fullCommand, Type.DISH);
command.execute(dish, ui, orderStorage);
Command command = Parser.parse(fullCommand, Type.DISH);
command.execute(null, dish, order, ui, fridgeStorage, orderStorage);
} catch (DukeException e) {
System.out.println(e.getLocalizedMessage());
}
Expand All @@ -198,7 +191,7 @@ public void run() throws IOException, InterruptedException {
default:
throw new DukeException("wrong input");
}
} catch (DukeException | IOException e) {
} catch (DukeException e) {
ui.showError(e.getMessage());
} finally {
ui.showLine();
Expand All @@ -209,9 +202,7 @@ public void run() throws IOException, InterruptedException {
/**
* =============== MAIN FUNCTION ===============.
*/
public static void main(String[] args) throws IOException, InterruptedException, DukeException {
String fridgeFilePath = "data/fridge.txt";
String orderFilePath = "data/order.txt";
new Duke(fridgeFilePath, orderFilePath).run();
public static void main(String[] args) {
new Duke().run();
}
}
}
22 changes: 10 additions & 12 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package duke.command;

import duke.command.ingredientCommand.ExitCommand;
import duke.dish.DishList;
import duke.exception.DukeException;
import duke.list.GenericList;
import duke.storage.Storage;
import duke.ingredient.IngredientsList;
import duke.order.OrderList;
import duke.storage.FridgeStorage;
import duke.storage.OrderStorage;
import duke.ui.Ui;

import java.io.IOException;
public abstract class Command {

public void execute(IngredientsList il, DishList dl, OrderList ol, Ui ui, FridgeStorage fs, OrderStorage os) throws DukeException {
throw new DukeException(" NANI??? Looks like one of our developer used the abstract class Cmd!");
}

public abstract class Command<T> {
public abstract void execute(GenericList<T> genlist, Ui ui, Storage storage) throws DukeException, IOException;
/**
* Returns the boolean indicating that it is( not) an {@link ExitCommand}.
*
* @return false by default
*/
public boolean isExit() {
return false;
}

}
27 changes: 0 additions & 27 deletions src/main/java/duke/command/FindIngredientCommand.java

This file was deleted.

13 changes: 7 additions & 6 deletions src/main/java/duke/command/ViewTodoListCommand.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package duke.command;

import duke.dish.DishList;
import duke.exception.DukeException;
import duke.ingredient.IngredientsList;
import duke.list.GenericList;
import duke.order.Order;
import duke.order.OrderList;
import duke.storage.FridgeStorage;
import duke.storage.OrderStorage;
import duke.storage.Storage;
import duke.ui.Ui;

public class ViewTodoListCommand extends Command<Order> {

public ViewTodoListCommand() {
}
public class ViewTodoListCommand extends Command {

@Override
public void execute(GenericList<Order> orderList, Ui ui, Storage storage) throws DukeException {
String info = ((OrderList)orderList).todoListToString();
public void execute(IngredientsList il, DishList dl, OrderList orderList, Ui ui, FridgeStorage fs, OrderStorage os) throws DukeException {
String info = (orderList).todoListToString();
System.out.println(info);
}
}
14 changes: 8 additions & 6 deletions src/main/java/duke/command/dishesCommand/AddDishCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

import duke.command.Command;
import duke.dish.Dish;
import duke.dish.DishList;
import duke.exception.DukeException;
import duke.list.GenericList;
import duke.storage.Storage;
import duke.ingredient.IngredientsList;
import duke.order.OrderList;
import duke.storage.FridgeStorage;
import duke.storage.OrderStorage;
import duke.ui.Ui;

public class AddDishCommand extends Command<Dish> {
public class AddDishCommand extends Command {

private Dish dish;

//constructor
public AddDishCommand(Dish dish) {
//super(dish);
this.dish = dish;
}

@Override
public void execute(GenericList<Dish> dishList, Ui ui, Storage storage) throws DukeException {
public void execute(IngredientsList il, DishList dishList, OrderList ol, Ui ui, FridgeStorage fs, OrderStorage os) throws DukeException {
boolean flag = true;
try {
if(dishList.size() == 0) { //if the list is empty, immediately add dish in it
Expand Down Expand Up @@ -45,5 +48,4 @@ public void execute(GenericList<Dish> dishList, Ui ui, Storage storage) throws D
throw new DukeException("unable to add dish");
}
}

}
15 changes: 9 additions & 6 deletions src/main/java/duke/command/dishesCommand/AddIngredient.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package duke.command.dishesCommand;

import duke.command.Command;
import duke.dish.Dish;
import duke.dish.DishList;
import duke.exception.DukeException;
import duke.ingredient.Ingredient;
import duke.list.GenericList;
import duke.storage.Storage;
import duke.ingredient.IngredientsList;
import duke.order.OrderList;
import duke.storage.FridgeStorage;
import duke.storage.OrderStorage;
import duke.ui.Ui;

public class AddIngredient extends Command<Dish> {
//Adds an ingredient to the dish
public class AddIngredient extends Command {

private Ingredient ingredient;

private int index;

//constructor
public AddIngredient(Ingredient ingredient, int index) {
super();
this.ingredient = ingredient;
this.index = index;
}

@Override
public void execute(GenericList<Dish> dishList, Ui ui, Storage storage) throws DukeException {
public void execute(IngredientsList il, DishList dishList, OrderList ol, Ui ui, FridgeStorage fs, OrderStorage os) throws DukeException {
try {
dishList.getEntry(index - 1).addIngredients(ingredient);
ui.showIngredients(ingredient,dishList.getEntry(index - 1));
Expand Down
Loading

0 comments on commit 4a90c87

Please sign in to comment.