Skip to content

Commit

Permalink
Merge pull request #5 from leowyh/master
Browse files Browse the repository at this point in the history
Applied A-Checkstyle to code after merging with Jiayu's code
  • Loading branch information
leowyh authored Sep 16, 2019
2 parents 095ce0a + eeccd89 commit 8b06d55
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 141 deletions.
14 changes: 7 additions & 7 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author Leow Yong Heng
*/

public class Duke{
public class Duke {
private static Ui ui;
private static TaskList tasklist;
private static Storage storage;
Expand All @@ -22,7 +22,7 @@ public class Duke{
* Creates a Duke instance and initialises the required attributes.
* @param filepath Filepath to the storage.
*/
private Duke(String filepath){
private Duke(String filepath) {
ui = new Ui();
storage = new Storage(filepath);
ArrayList<Task> arraylist = storage.load();
Expand All @@ -32,12 +32,12 @@ private Duke(String filepath){
/**
* Method to run the Duke instance and take in the inputs of the user.
*/
private void run(){
ui.StartMessage();
private void run() {
ui.startMessage();

boolean isExit = false;
while(!isExit){
String input = ui.readinput();
while (!isExit) {
String input = ui.readInput();
isExit = Parser.parse(input, tasklist, ui, storage);
}
}
Expand All @@ -46,7 +46,7 @@ private void run(){
* The main method of the Duke program, which instantiates a duke instance with the filepath to the storage.
* @param args Unused.
*/
public static void main(String[] args){
public static void main(String[] args) {
new Duke("data/duke.txt").run();
}
}
151 changes: 72 additions & 79 deletions src/main/java/command/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,47 @@ public class Parser {
* @return Returns boolean variable to indicate when to stop parsing for input.
* @throws DukeException if input is not valid.
*/
public static boolean parse(String input, TaskList tasklist, Ui ui, Storage storage){
public static boolean parse(String input, TaskList tasklist, Ui ui, Storage storage) {
try {
if (IsBye(input)) {
if (isBye(input)) {
//print bye message
ui.ByeMessage();
ui.byeMessage();
ui.getIn().close();
return true;

} else if (IsList(input)) {
} else if (isList(input)) {
//print out current list
ui.PrintList(tasklist, "list");
ui.printList(tasklist, "list");

} else if (IsDone(input)) {
ProcessDone(input, tasklist, ui);
} else if (isDone(input)) {
processDone(input, tasklist, ui);

} else if (IsDeadline(input)) {
ProcessDeadline(input, tasklist, ui);
storage.save(tasklist.ReturnArrayList());
} else if (isDeadline(input)) {
processDeadline(input, tasklist, ui);
storage.save(tasklist.returnArrayList());

} else if (IsTodo(input)) {
ProcessTodo(input, tasklist, ui);
storage.save(tasklist.ReturnArrayList());
} else if (isTodo(input)) {
processTodo(input, tasklist, ui);
storage.save(tasklist.returnArrayList());

} else if (IsEvent(input)) {
ProcessEvent(input, tasklist, ui);
storage.save(tasklist.ReturnArrayList());
} else if (isEvent(input)) {
processEvent(input, tasklist, ui);
storage.save(tasklist.returnArrayList());

} else if (IsDelete(input)) {
ProcessDelete(input, tasklist, ui);
storage.save(tasklist.ReturnArrayList());
} else if (isDelete(input)) {
processDelete(input, tasklist, ui);
storage.save(tasklist.returnArrayList());

} else if (IsFind(input)) {
ProcessFind(input, tasklist, ui);
} else if (isFind(input)) {
processFind(input, tasklist, ui);

} else {
throw new DukeException(" \u2639 OOPS!!! I'm sorry, but I don't know what that means :-(");
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");

}

}
catch (DukeException e){
ui.ExceptionMessage(e.getMessage());
} catch (DukeException e) {
ui.exceptionMessage(e.getMessage());
return true;
}

Expand All @@ -82,19 +81,18 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessFind(String input, TaskList tasklist, Ui ui){
try{
private static void processFind(String input, TaskList tasklist, Ui ui) {
try {
TaskList findlist = new TaskList();
String[] splitspace = input.split(" ", 2);
for (Task tasks : tasklist.ReturnArrayList()){
if(tasks.getDescription().contains(splitspace[1])){
findlist.AddTask(tasks);
for (Task tasks : tasklist.returnArrayList()) {
if (tasks.getDescription().contains(splitspace[1])) {
findlist.addTask(tasks);
}
}
ui.PrintList(findlist, "find");
}
catch(ArrayIndexOutOfBoundsException e) {
ui.ExceptionMessage(" \u2639 OOPS!!! The content to find cannot be empty.");
ui.printList(findlist, "find");
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! The content to find cannot be empty.");
}
}

Expand All @@ -104,16 +102,16 @@ private static void ProcessFind(String input, TaskList tasklist, Ui ui){
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessDelete(String input, TaskList tasklist, Ui ui){
try{
private static void processDelete(String input, TaskList tasklist, Ui ui) {
try {
String[] arr = input.split(" ", 2);
int numdelete = Integer.parseInt(arr[1]) - 1;
String task = tasklist.get(numdelete).GiveTask();
tasklist.DeleteTask(numdelete);
ui.PrintDeleteMessage(task, tasklist);
String task = tasklist.get(numdelete).giveTask();
tasklist.deleteTask(numdelete);
ui.printDeleteMessage(task, tasklist);

}catch(ArrayIndexOutOfBoundsException e){
ui.ExceptionMessage(" \u2639 OOPS!!! Please input the list number to delete.");
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" OOPS!!! Please input the list number to delete.");
}
}

Expand All @@ -123,15 +121,15 @@ private static void ProcessDelete(String input, TaskList tasklist, Ui ui){
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessDone(String input, TaskList tasklist, Ui ui){
try{
private static void processDone(String input, TaskList tasklist, Ui ui) {
try {
String[] arr = input.split(" ", 2);
int numdone = Integer.parseInt(arr[1]) - 1;
tasklist.get(numdone).SetDone();
ui.PrintDoneMessage(numdone, tasklist);
tasklist.get(numdone).setDone();
ui.printDoneMessage(numdone, tasklist);

}catch(ArrayIndexOutOfBoundsException e){
ui.ExceptionMessage(" \u2639 OOPS!!! Please input the list number to indicate as done.");
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" OOPS!!! Please input the list number to indicate as done.");
}
}

Expand All @@ -141,7 +139,7 @@ private static void ProcessDone(String input, TaskList tasklist, Ui ui){
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessDeadline(String input, TaskList tasklist, Ui ui){
private static void processDeadline(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
String[] splitslash = splitspace[1].split("/", 2);
Expand All @@ -150,14 +148,12 @@ private static void ProcessDeadline(String input, TaskList tasklist, Ui ui){
String taskTime = splittime[1];
Date formattedtime = dataformat.parse(taskTime);
Deadline deadline = new Deadline(taskDescription, dataformat.format(formattedtime));
tasklist.AddTask(deadline);
ui.PrintAddedMessage(deadline, tasklist);
}
catch (ArrayIndexOutOfBoundsException e){
ui.ExceptionMessage(" \u2639 OOPS!!! The description of a deadline cannot be empty.");
}
catch (ParseException e){
ui.ExceptionMessage(" \u2639 OOPS!!! Format of time is wrong.");
tasklist.addTask(deadline);
ui.printAddedMessage(deadline, tasklist);
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! The description of a deadline cannot be empty.");
} catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong.");
}
}

Expand All @@ -167,15 +163,14 @@ private static void ProcessDeadline(String input, TaskList tasklist, Ui ui){
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessTodo(String input, TaskList tasklist, Ui ui){
private static void processTodo(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
Todo todotoadd = new Todo(splitspace[1]);
tasklist.AddTask(todotoadd);
ui.PrintAddedMessage(todotoadd, tasklist);
}
catch (ArrayIndexOutOfBoundsException e) {
ui.ExceptionMessage(" \u2639 OOPS!!! The description of a todo cannot be empty.");
tasklist.addTask(todotoadd);
ui.printAddedMessage(todotoadd, tasklist);
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! The description of a todo cannot be empty.");
}
}

Expand All @@ -185,7 +180,7 @@ private static void ProcessTodo(String input, TaskList tasklist, Ui ui){
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void ProcessEvent(String input, TaskList tasklist, Ui ui){
private static void processEvent(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
String[] splitslash = splitspace[1].split("/", 2);
Expand All @@ -194,47 +189,45 @@ private static void ProcessEvent(String input, TaskList tasklist, Ui ui){
String taskTime = splittime[1];
Date formattedtime = dataformat.parse(taskTime);
Event event = new Event(taskDescription, dataformat.format(formattedtime));
tasklist.AddTask(event);
ui.PrintAddedMessage(event, tasklist);
}
catch(ArrayIndexOutOfBoundsException e) {
ui.ExceptionMessage(" \u2639 OOPS!!! The description of a event cannot be empty.");
}
catch (ParseException e){
ui.ExceptionMessage(" \u2639 OOPS!!! Format of time is wrong.");
tasklist.addTask(event);
ui.printAddedMessage(event, tasklist);
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! The description of a event cannot be empty.");
} catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong.");
}
}


private static boolean IsBye(String input){
private static boolean isBye(String input) {
return input.equals("bye");
}

private static boolean IsList(String input){
private static boolean isList(String input) {
return input.equals("list");
}

private static boolean IsDone(String input){
private static boolean isDone(String input) {
return input.startsWith("done");
}

private static boolean IsDeadline(String input){
private static boolean isDeadline(String input) {
return input.startsWith("deadline");
}

private static boolean IsTodo(String input){
private static boolean isTodo(String input) {
return input.startsWith("todo");
}

private static boolean IsEvent(String input){
private static boolean isEvent(String input) {
return input.startsWith("event");
}

private static boolean IsDelete(String input){
private static boolean isDelete(String input) {
return input.startsWith("delete");
}

private static boolean IsFind(String input){
private static boolean isFind(String input) {
return input.startsWith("find");
}
}
33 changes: 15 additions & 18 deletions src/main/java/command/Storage.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package command;

import task.Task;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import java.io.*;
import task.Task;
import java.util.ArrayList;

/**
* Storage that saves and loads the tasklist of the user.
*/
public class Storage{
public class Storage {
private static String filepath;

/**
* Creates a Storage instance with the required attributes.
* @param filepath Filepath to the storage file.
*/
public Storage(String filepath){
public Storage(String filepath) {
Storage.filepath = filepath;
}

/**
* Loads an ArrayList containing the Task object from the storage file.
* @return The ArrayList containing the Task object.
*/
public static ArrayList<Task> load(){
try
{

public static ArrayList<Task> load() {
try {

FileInputStream file = new FileInputStream(filepath);
ObjectInputStream out = new ObjectInputStream(file);
Expand All @@ -37,17 +40,11 @@ public static ArrayList<Task> load(){
file.close();

return arraylist;
}
catch (EOFException e) {
} catch (EOFException e) {
System.out.println("File is empty");

}
catch (IOException ioe)
{
} catch (IOException ioe) {
ioe.printStackTrace();
}
catch (ClassNotFoundException c)
{
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
}
Expand All @@ -58,7 +55,7 @@ public static ArrayList<Task> load(){
* Saves the tasklist of the user as an ArrayList containing the task object.
* @param tasklist Tasklist of the user.
*/
public static void save(ArrayList<Task> tasklist){
public static void save(ArrayList<Task> tasklist) {
try {
FileOutputStream file = new FileOutputStream(filepath);
ObjectOutputStream out = new ObjectOutputStream(file);
Expand Down
Loading

0 comments on commit 8b06d55

Please sign in to comment.