Skip to content

Commit

Permalink
Refactor to use classes stated in A-MoreOOP
Browse files Browse the repository at this point in the history
  • Loading branch information
zognin committed Aug 21, 2021
1 parent 892ec37 commit 8e8a313
Show file tree
Hide file tree
Showing 37 changed files with 433 additions and 394 deletions.
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Description of the feature.

## Usage

### `Keyword` - Describe action
### `Keyword` - Describe command

Describe the action and its outcome.
Describe the command and its outcome.

Example of usage:

Expand Down
73 changes: 39 additions & 34 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
import action.DukeAction;
import entity.list.DukeTaskList;
import entity.message.ExitMessage;
import entity.message.GreetMessage;
import entity.message.Message;
import command.Command;
import tasklist.TaskList;
import parser.Parser;
import ui.Ui;
import ui.message.Message;

import entity.data.Data;
import entity.data.DukeFile;
import storage.Storage;
import storage.StorageFile;
import exception.DukeException;

import java.io.IOException;
import java.util.Scanner;

/**
* Encapsulates a chatbot that greets the user,
* adds valid inputs to a task list,
* updates tasks in the list,
* and exits when the user types `bye`.
*/
public class Duke {
public static void main(String[] args) {
private StorageFile storageFile;
private TaskList list;
private Ui ui;
private Parser parser;

public Duke() {
// Load data
DukeFile listFile = Data.loadListFile();
if (listFile == null) {
return;
}
StorageFile storageFile = Storage.loadListFile();
this.storageFile = storageFile;

// Scan data to a list
DukeTaskList list = Data.scanListFileDataToList(listFile);
if (list == null) {
TaskList list = Storage.scanListFileDataToList(storageFile);
this.list = list;

this.ui = new Ui();
this.parser = new Parser();
}

private void run() {
// Check config
if (this.storageFile == null || this.list == null) {
return;
}

// Greet
GreetMessage greetingMessage = new GreetMessage("Hello! I'm Duke, what shall we do today?");
greetingMessage.print();

// Process input
Scanner inputScanner = new Scanner(System.in);
String inputMessage = inputScanner.nextLine();
String exitCommand = "bye";
this.ui.showWelcome();

while (!inputMessage.trim().equals(exitCommand)) {
// Process user inputs
String inputMessage = this.ui.readInputMessage();
while (!this.parser.detectExitCommand(inputMessage)) {
Message outputMessage;

try {
DukeAction action = DukeAction.makeAction(inputMessage);
action.executeAction(list);
outputMessage = action.getOutputMessage();
Command command = this.parser.makeCommand(inputMessage);
command.execute(this.list);
outputMessage = command.getOutputMessage();
} catch (DukeException e) {
outputMessage = e.getOutputMessage();
} catch (IOException e) {
outputMessage = new Message("There is a problem when changing data to the file");
}

// Print output message
outputMessage.print();
inputMessage = inputScanner.nextLine();
this.ui.showMessage(outputMessage);
inputMessage = this.ui.readInputMessage();
}

// Exit
ExitMessage exitMessage = new ExitMessage("Bye, see you again");
exitMessage.print();
ui.showGoodbye();
}

public static void main(String[] args) {
new Duke().run();
}
}
71 changes: 0 additions & 71 deletions src/main/java/action/DukeAction.java

This file was deleted.

42 changes: 0 additions & 42 deletions src/main/java/action/DukeActionAdd.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/action/DukeActionDelete.java

This file was deleted.

35 changes: 0 additions & 35 deletions src/main/java/action/DukeActionDone.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/action/DukeActionList.java

This file was deleted.

42 changes: 42 additions & 0 deletions src/main/java/command/AddCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package command;

import tasklist.Task;
import tasklist.TaskList;
import ui.message.AddMessage;
import exception.ErrorAccessingFile;
import exception.InvalidTaskTimeFormatException;
import exception.InvalidTaskTypeException;
import exception.MissingCommandDescriptionException;
import exception.InvalidDateTimeException;
import type.DukeCommandTypeEnum;

public class AddCommand extends Command {
private String description;
private DukeCommandTypeEnum actionType;
private Task task;
private TaskList list;

public AddCommand(String description, DukeCommandTypeEnum actionType) {
this.description = description;
this.actionType = actionType;
}

public static AddCommand createCommand(String description, DukeCommandTypeEnum actionType)
throws MissingCommandDescriptionException {
// Validate before creating the action
Command.validateDescriptionNotEmpty(actionType, description);

return new AddCommand(description, actionType);
}

public void execute(TaskList list) throws InvalidTaskTypeException, InvalidTaskTimeFormatException, ErrorAccessingFile, InvalidDateTimeException {
Task task = Task.createTask(this.description, this.actionType);
list.addTaskToList(task);
this.task = task;
this.list = list;
}

public AddMessage getOutputMessage() {
return new AddMessage(task.toString(), list.getNumberOfTasks());
}
}
Loading

0 comments on commit 8e8a313

Please sign in to comment.