Skip to content

Commit

Permalink
Merge branch 'branch-A-JavaDoc'
Browse files Browse the repository at this point in the history
  • Loading branch information
gachia committed Sep 1, 2019
2 parents d771dc5 + 456b267 commit 1d1d5fa
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 14 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ test {
useJUnitPlatform()
}

run {
standardInput = System.in;
}

application {
// Change this to your main class.
mainClassName = "Duke"
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/*
* Represents the Command for adding Tasks and its subclasses
* A subclass of Command
*/
public class AddCommand extends Command {

private String taskCmd;
//Time
final String timePattern = "d MMMM yyyy, h:mma";
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(timePattern);

/*
* Constructor for AddCommand to set the Task command
* @param taskCmd User input of what Task is being generated
*/
public AddCommand(String taskCmd) {
super();
this.taskCmd = taskCmd;
}

/*
* Overridden execute method from Command to add a Task object into the list of tasks.
* The method will check the user input for a valid Command and adds the appropriate Task
* accordingly. It will throw an exception if the user inputs are unrecognisable for the
* method to execute correctly.
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
* @throws DukeException If taskCmd and taskName is invalid and format of subsequent fields,
* such as /by, /at and dateTime, is wrong
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException {
String taskName;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
/*
* Represents the user input as a Command object
*/
public abstract class Command {

protected boolean hasExit;

/*
* Default Constructor to set hasExit boolean variable to false
* Will be set to true if ExitCommand is called
*/
public Command() {
hasExit = false;
}

/*
* Abstract method for the sub-classes of Command to use. This method will execute
* differently based on the sub-class called
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
* @throws DukeException If there is an error
*/
abstract void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException;

/*
* Returns boolean variable hasExit for checking exit status
* @return hasExit boolean variable
*/
public boolean isExit() {
return hasExit;
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
/*
* Represents a different type of Task called Deadline that holds an additional
* parameter for date and time
* A sub-class of Task
*/
public class Deadline extends Task {
protected String by;

/*
* Constructor to set the description of deadline and due date and time
* @param desc The description of the Deadline
* @param by The due date and time of the Deadline
*/
public Deadline(String desc, String by) {
super(desc);
this.by = by;
}

/*
* Constructor with additional parameter to set its completion status
* @param desc The description of the Deadline
* @param by The due date and time of the Deadline
* @param isDone The boolean variable to note if Deadline is completed
*/
public Deadline(String desc, String by, boolean isDone) {
super(desc, isDone);
this.by = by;
}

/*
* Overridden writeFormat method to specify that it is a Deadline
* when saving the data
* @return Format for saving data
*/
@Override
public String writeFormat() {
return "D " + isDone + " " + description + "/" + by;
}

/*
* Overridden toString method to print out Deadline object
* @return Printing format of Deadline
*/
@Override
public String toString() {
return "[D]" + super.getTask() + " (by: " + by + ")";
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import java.util.Scanner;

/*
* Represents the Command for deleting Tasks for the list
* A sub-class of Command
*/
public class DeleteCommand extends Command {

/*
* Overridden execute method from Command to delete a Task object from the list of tasks.
* The method will check the user input for a valid index and deletes the selected
* Task object from the list. It will throw an exception if the index is invalid
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
* @throws DukeException If deletion < 0 or > size of list
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException {
int deletion = ui.readIndex();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
/*
* Represents the Command for setting Tasks as completed
* A sub-class of Command
*/
public class DoneCommand extends Command {

/*
* Overridden execute method from Command to mark a Task object as completed.
* The method will check the user input for a valid index and change the selected
* Task object's isDone boolean variable to true.
* It will throw an exception if the index is invalid
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
* @throws DukeException If taskNo < 0 or > size of list
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException {
int taskNo = ui.readIndex();
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/*
* Main class responsible for running Duke Chatbot
*/
public class Duke {

private Storage storage;
private TaskList taskList;
private Ui ui;

/*
* Constructor of Duke class
* @param filepath The directory of the text file for populating task list
*/
public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
Expand All @@ -21,6 +22,9 @@ public Duke(String filePath) {
}
}

/*
* Method to start up Duke Chatbot
*/
public void run() {
ui.showWelcome();
boolean isExit = false;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/*
* Represents Exceptions from the Duke project
*/
public class DukeException extends Exception {

/*
* Constructor to print custom error messages
* @param error Error message
*/
public DukeException(String error) {
super(error);
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
/*
* Represents a different type of Task called Event that holds an
* additional parameter for the venue
*/
public class Event extends Task {

protected String at;

/*
* Constructor to set the description of event and venue
* @param desc The description of the Event
* @param at The venue of the Event
*/
public Event(String desc, String at) {
super(desc);
this.at = at;
}

/*
* Constructor with additional parameter to set its completion status
* @param desc The description of the Event
* @param at The venue of the Event
* @param isDone The boolean variable to note if Event is completed
*/
public Event(String desc, String at, boolean isDone) {
super(desc, isDone);
this.at = at;
}

/*
* Overridden writeFormat method to specify that it is a Event
* when saving the data
* @return Format for saving data
*/
@Override
public String writeFormat() {
return "E " + isDone + " " + description + "/" + at;
}

/*
* Overridden toString method to print out Event object
* @return Printing format of Event
*/
@Override
public String toString() {
return "[E]" + super.getTask() + " (at: " + at + ")";
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/*
* Represents the Command for exiting the Duke Chatbot
* A sub-class of Command
*/
public class ExitCommand extends Command {

/*
* Overridden execute method from Command to save the list data to the specified
* text file. The method will set the hasExit boolean variable to true, call
* the saveData method from storage and display the Goodbye Message to the user
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException {
public void execute(Storage storage, TaskList tasks, Ui ui) {
hasExit = true;
storage.saveData(tasks.getTaskList());
ui.showGoodbye();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/*
* Represents the Command for printing out all of the tasks in the list
* A sub-class of Command
*/
public class ListCommand extends Command {

/*
* Overridden execute method to print out all of the tasks inside the list
* using the method from TaskList
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing methods and user input field
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) {
tasks.showTaskList();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/*
* Responsible for interpreting Commands
*/
public class Parser {

/*
* Checks user input for a Command and returns the associated Command class
* @param userCmd The command from user input
* @return Command object
* @throws DukeException If user input is an invalid command
*/
public static Command parse(String userCmd) throws DukeException {
switch (userCmd) {
case "list":
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@
import java.io.FileWriter;
import java.util.Scanner;

/*
* Represents the read and write of data into a text file
*/
public class Storage {

private String filePath;

/*
* Constructor to specify the file path of the text file used
* for saving and loading of Tasks for the list
* @param filePath directory of text file
*/
public Storage(String filePath) {
this.filePath = filePath;
}

/*
* Returns a ArrayList of Task type for Duke to use as an initial list.
* Data is obtained from a text file with a specific format
* @return List of Tasks to be used
* @throws DukeException When format is wrong and filePath is invalid
*/
public ArrayList<Task> loadData() throws DukeException {
try {
File f = new File(filePath);
Expand Down Expand Up @@ -50,7 +64,14 @@ public ArrayList<Task> loadData() throws DukeException {
}
}

public void saveData(ArrayList<Task> list) throws DukeException {
/*
* Saves all of the Tasks inside the list into a text file for future usage.
* It will throw an exception if the file path was not specified in storage upon initialisation.
* However, an IOException is thrown instead of DukeException to be able
* to exit the program despite not saving
* @param list List of Tasks used in Duke
*/
public void saveData(ArrayList<Task> list) {
try {
FileWriter fw = new FileWriter(filePath);
String data = "";
Expand All @@ -60,7 +81,7 @@ public void saveData(ArrayList<Task> list) throws DukeException {
fw.write(data);
fw.close();
} catch (IOException e) {
throw new DukeException("Error writing file.");
System.out.println("Failed to save data. File path may not have been specified.");
}
}
}
Loading

0 comments on commit 1d1d5fa

Please sign in to comment.