forked from nus-cs2103-AY2122S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use classes stated in A-MoreOOP
- Loading branch information
Showing
37 changed files
with
433 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.