Skip to content

Commit

Permalink
MoreOOP
Browse files Browse the repository at this point in the history
  • Loading branch information
Krit-K committed Aug 29, 2019
1 parent 43ff40f commit bbe2e9f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import java.util.Date;

public class Deadline extends Task {
protected String by;
protected Date by;

public Deadline(String description, String by) {
public Deadline(String description, Date by) {
super(description);
this.by = by;
}
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

Expand All @@ -6,22 +9,22 @@ public class Duke {
protected static ArrayList<Task> taskList = new ArrayList<>();

public static void main(String[] args) throws DukeException, FileNotFoundException {
taskList = FileSaver.loadTask();
taskList = Storage.load();

Scanner sc = new Scanner(System.in);
ArrayList<Task> commandList = new ArrayList<>();
String line = " ____________________________________________________________\n";
System.out.println(line +
" Hello! I'm Duke\n" + //duke greeting
" What can I do for you?\n" + line);
Ui ui = new Ui();
Parser parser = new Parser();
ui.greet();

int x = 1; //switch
do { //to recycle the try catch block
try {
while (sc.hasNext()) {
String command = sc.next();

if (command.equals("bye")) {
System.out.println(line + " Bye. Hope to see you again soon!\n" + line);
ui.farewell();
x = 2; //break the do while loop
break;
} else if (command.equals("list")) {
Expand All @@ -39,7 +42,7 @@ public static void main(String[] args) throws DukeException, FileNotFoundExcepti
System.out.println(line + " Nice! I've marked this task as done:\n"
+ " [" + taskList.get(taskNum - 1).getStatusIcon() + "]"
+ taskList.get(taskNum - 1).toString() + "\n" + line);
FileSaver.saveTaskList(taskList); //save file
Storage.saveTaskList(taskList); //save file

} else if (command.equals("todo")) {
String wordsAfter = sc.nextLine();
Expand All @@ -51,7 +54,7 @@ public static void main(String[] args) throws DukeException, FileNotFoundExcepti
System.out.print(line + " Got it. I've added this task: "
+ "\n " + todoTask.toString() + "\n");
System.out.print(" Now you have " + taskList.size() + " tasks in the list." + "\n" + line);
FileSaver.saveTaskList(taskList); //save file
Storage.saveTaskList(taskList); //save file

} else if (command.equals("deadline")) {
String wordsAfter = sc.nextLine();
Expand All @@ -62,25 +65,26 @@ public static void main(String[] args) throws DukeException, FileNotFoundExcepti
System.out.print(line + " Got it. I've added this task: "
+ "\n " + dlTask.toString() + "\n");
System.out.print(" Now you have " + taskList.size() + " tasks in the list." + "\n" + line);
FileSaver.saveTaskList(taskList); //save file
Storage.saveTaskList(taskList); //save file

} else if (command.equals("event")) {
String wordsAfter = sc.nextLine();
int pos = wordsAfter.indexOf("/");
Task eTask = new Event(wordsAfter.substring(0, pos), wordsAfter.substring(pos + 3));
commandList.add(eTask);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
Task eTask = new Event(wordsAfter.substring(0, pos), sdf.parse(wordsAfter.substring(pos + 3)));
taskList.add(eTask);
System.out.print(line + " Got it. I've added this task: "
+ "\n " + eTask.toString() + "\n" + " Now you have "
+ taskList.size() + " tasks in the list." + "\n" + line);
FileSaver.saveTaskList(taskList); //save file
Storage.saveTaskList(taskList); //save file

} else if (command.equals("delete")) {
int taskNum = sc.nextInt();
System.out.println(line + " Noted. I've removed this task: ");
System.out.println(" " + taskList.get(taskNum - 1). toString());
taskList.remove(taskNum - 1);
System.out.print(" Now you have " + taskList.size() + "tasks in the list.\n" + line);
FileSaver.saveTaskList(taskList); //save file
Storage.saveTaskList(taskList); //save file

} else {
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;

class Parser {
public void formatTime(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
sdf.parse(time);
}
}
6 changes: 3 additions & 3 deletions src/main/java/FileSaver.java → src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.List;
import java.util.Scanner;

public class FileSaver {
public class Storage {
private static File file = new File("../duke.txt");

public static ArrayList<Task> loadTask() throws FileNotFoundException {
public static ArrayList<Task> load() throws FileNotFoundException {

ArrayList<Task> taskList = new ArrayList<>();
try {
Expand All @@ -20,7 +20,7 @@ public static ArrayList<Task> loadTask() throws FileNotFoundException {

String[] task = sc.nextLine().split("\\|");
String taskType = task[0].trim();
boolean isTaskDone = task[2].trim().equals("1") ? true : false;
boolean isTaskDone = task[2].trim().equals("1");

switch(taskType) {
case "T":
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;

public class TaskList {

private ArrayList<Task> taskList = new ArrayList<>();

public TaskList(ArrayList<Task> taskList) {
this.taskList = taskList;
}

public void addTask(Task task) {
taskList.add(task);
}

public void deleteTask(int taskNum) {
this.taskList.remove(taskNum);
}

public ArrayList<Task> getTaskList() {
return taskList;
}
}
21 changes: 21 additions & 0 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Ui {

private Scanner sc = new Scanner(System.in);
protected static ArrayList<Task> taskList = new ArrayList<>();

String line = " ____________________________________________________________\n";

public void greet() { //duke greet
System.out.println(line +
" Hello! I'm Duke\n" +
" What can I do for you?\n" + line);
}

public void farewell() {
System.out.println(line + " Bye. Hope to see you again soon!\n" + line);
}

}

0 comments on commit bbe2e9f

Please sign in to comment.