-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lin Weilin] iP #368
base: master
Are you sure you want to change the base?
[Lin Weilin] iP #368
Changes from 9 commits
556af3f
da98b2d
f1267e3
6dadf28
464bf09
a03b4ff
93bcdaa
4e46cba
2a0d657
38c70c8
5fa18f0
fede33d
0875429
446b67b
cc040c0
ac9f6fa
e209948
d64c6e2
c837839
7b1c7c8
6cfe59e
7dd4bf2
efd490e
4dd86d6
63a7e15
a1e008c
0aa85b9
9d0d3ad
0e677a9
8dd634a
30fe297
08739db
c3c9b23
fc08d66
05d2448
86e1c0b
f499929
098bc19
017ad98
4bad7ab
4eb813f
d826e7d
e245b55
04c97d2
58d0416
071a988
5e9baa0
e3827ec
04b33d7
3a0362a
cad8c22
92764d1
8df56e7
f41a5fe
df0211b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task{ | ||
|
||
String taskDescription; | ||
LocalDate deadLine; | ||
|
||
public Deadline(String taskString, LocalDate deadline) { | ||
super (taskString.substring(9, taskString.indexOf("/") - 1)); | ||
|
||
taskDescription = taskString.substring(9, taskString.indexOf("/") - 1); | ||
deadLine = deadline; | ||
} | ||
@Override | ||
public String getTask() { | ||
return this.taskDescription; | ||
} | ||
|
||
public String getDeadline() { | ||
return this.deadLine.format(DateTimeFormatter.ofPattern("MMM d yyyy")); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " ( by: " + this.getDeadline() + " )" ; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good way to separate the different display behaviors of different task types 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,112 @@ | ||
import java.io.BufferedWriter; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.FileWriter; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Duke { | ||
public static void addTaskToFile(String fileName, String task) { | ||
try { | ||
BufferedWriter temp = new BufferedWriter(new FileWriter(fileName, true)); | ||
temp.write(task); | ||
temp.close(); | ||
} catch (IOException e) { | ||
System.out.println("There is an error in saving tasks."); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
System.out.println("Hello! I'm Happie \nWhat can I do for you?"); | ||
try { | ||
File taskSaved = new File("C:/Users/linwe/Documents/TaskSaved.txt"); | ||
FileWriter myWriter = new FileWriter("C:/Users/linwe/Documents/TaskSaved.txt"); | ||
myWriter.write(""); | ||
} catch (IOException e) { | ||
System.out.println("There is an error in saving tasks."); | ||
} | ||
Scanner sc = new Scanner(System.in); | ||
String input = sc.nextLine(); | ||
ArrayList<Task> taskList = new ArrayList<>(); | ||
|
||
|
||
|
||
while(!input.equals("bye")) { | ||
|
||
if (input.equals("list")) { | ||
System.out.println("Here are some tasks in your list:"); | ||
for (int i = 1; i < taskList.size() + 1; i++) { | ||
System.out.println(i + "." + (taskList.get(i - 1)).toString()); | ||
} | ||
|
||
} else if(input.length() > 4 && (input.substring(0, 4)).equals("mark")) { | ||
String taskStr = input.substring(5); | ||
int taskNum = Integer.parseInt(taskStr) - 1; | ||
Task originalTask = taskList.get(taskNum); | ||
originalTask.markTask(); | ||
System.out.println("Nice! I've marked this task as done: \n " + originalTask); | ||
|
||
} else if(input.length() > 6 && (input.substring(0, 6)).equals("unmark")) { | ||
String taskStr = input.substring(7); | ||
int taskNum = Integer.parseInt(taskStr) - 1; | ||
Task originalTask = taskList.get(taskNum); | ||
originalTask.unmarkTask(); | ||
System.out.println("Ok, I've marked this task as not done yet: \n " + originalTask); | ||
|
||
} else if(input.length() > 4 && (input.substring(0, 4)).equals("todo")) { | ||
ToDo task = new ToDo(input); | ||
taskList.add(task); | ||
addTaskToFile("C:/Users/linwe/Documents/TaskSaved.txt", task.getTaskType() + " | " | ||
+ task.currentTaskStatus() + " | " + task.getTask() + "\n"); | ||
|
||
System.out.println("Got it. I've added this task: \n " + task + | ||
"\nNow you have " + taskList.size() + " tasks in the list."); | ||
|
||
} else if(input.length() > 8 &&(input.substring(0, 8)).equals("deadline")) { | ||
LocalDate deadline = LocalDate.parse(input.substring(input.indexOf("/") + 4)); | ||
Deadline task = new Deadline(input, deadline); | ||
taskList.add(task); | ||
addTaskToFile("C:/Users/linwe/Documents/TaskSaved.txt", task.getTaskType() + " | " | ||
+ task.currentTaskStatus() + " | " + task.getTask() + " | " + task.getDeadline() + "\n"); | ||
|
||
System.out.println("Got it. I've added this task: \n " + task + | ||
"\nNow you have " + taskList.size() + " tasks in the list."); | ||
|
||
} else if(input.length() > 5 && (input.substring(0, 5)).equals("event")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to surround reserved words with a whitespace for consistency |
||
LocalDate startDate = LocalDate.parse(input.substring(input.indexOf("/") + 6, input.lastIndexOf("/") - 1)); | ||
LocalDate endDate = LocalDate.parse(input.substring(input.lastIndexOf("/") + 4)); | ||
Event task = new Event(input, startDate, endDate); | ||
taskList.add(task); | ||
addTaskToFile("C:/Users/linwe/Documents/TaskSaved.txt", task.getTaskType() + " | " | ||
+ task.currentTaskStatus() + " | " + task.getTask() + " | " + task.getTimeline() + "\n"); | ||
|
||
System.out.println("Got it. I've added this task: \n " + task + | ||
"\nNow you have " + taskList.size() + " tasks in the list."); | ||
|
||
} else if(input.length() > 6 && (input.substring(0, 6)).equals("delete")) { | ||
String taskStr = input.substring(7); | ||
int taskNum = Integer.parseInt(taskStr) - 1; | ||
Task taskToRemove = taskList.get(taskNum); | ||
String removedTaskStr = taskToRemove.toString(); | ||
taskList.remove(taskNum); | ||
System.out.println("Noted. I've removed this task: \n " + removedTaskStr + | ||
"\nNow you have " + taskList.size() + " tasks in the list."); | ||
} | ||
else { | ||
try { | ||
if ((input.equals("todo")) || (input.equals("deadline")) || (input.equals("event"))) { | ||
throw new EmptyDescriptionException(); | ||
} else { | ||
throw new WrongCommandException(); | ||
} | ||
} catch (EmptyDescriptionException | WrongCommandException e){ | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
input = sc.nextLine(); | ||
} | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class EmptyDescriptionException extends Exception{ | ||
public EmptyDescriptionException() { | ||
super("☹ OOPS!!! The description of a task cannot be empty."); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Event extends Task{ | ||
String taskDescription; | ||
LocalDate startDate; | ||
LocalDate endDate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for this, regarding making class fields private |
||
|
||
public Event(String taskString, LocalDate startDateInput, LocalDate endDateInput) { | ||
super(taskString.substring(6, taskString.indexOf("/") - 1)); | ||
|
||
taskDescription = taskString.substring(6, taskString.indexOf("/") - 1); | ||
startDate = startDateInput; | ||
endDate = endDateInput; | ||
} | ||
|
||
@Override | ||
public String getTask() { | ||
return this.taskDescription; | ||
} | ||
|
||
public String getTimeline() { | ||
return this.startDate.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + " to " + this.endDate.format(DateTimeFormatter.ofPattern("MMM d yyyy")); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (" + this.getTimeline() + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public class Task { | ||
public String taskString; | ||
public boolean isCompleted; | ||
|
||
public Task(String taskString) { | ||
this.taskString = taskString; | ||
this.isCompleted = false; | ||
} | ||
|
||
public String currentTaskStatus() { | ||
if(this.isCompleted) { | ||
return "[X]"; | ||
} else { | ||
return "[ ]"; | ||
} | ||
} | ||
|
||
public void markTask() { | ||
this.isCompleted = true; | ||
} | ||
|
||
public void unmarkTask() { | ||
this.isCompleted = false; | ||
} | ||
|
||
public String getTaskType() { | ||
return this.toString().substring(1, 2); | ||
} | ||
|
||
public String getTask() { | ||
return " "; | ||
} | ||
|
||
public String toString() { | ||
return this.currentTaskStatus() + " " + this.taskString; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class ToDo extends Task { | ||
|
||
String taskDescription; | ||
|
||
public ToDo(String taskString) { | ||
super(taskString.substring(5)); | ||
|
||
taskDescription = taskString.substring(5); | ||
} | ||
|
||
@Override | ||
public String getTask() { | ||
return this.taskDescription; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class WrongCommandException extends Exception{ | ||
public WrongCommandException() { | ||
super("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello! I'm Happie | ||
What can I do for you? | ||
Got it. I've added this task: | ||
[T][ ] cook | ||
Now you have 1 tasks in the list. | ||
Got it. I've added this task: | ||
[E][ ] pay (from: 4pm to: 7pm) | ||
Now you have 2 tasks in the list. | ||
Got it. I've added this task: | ||
[D][ ] code (by: no idea :-p) | ||
Now you have 3 tasks in the list. | ||
Nice! I've marked this task as done: | ||
[T][X] cook | ||
Here are the tasks in your list: | ||
1.[T][X] cook | ||
2.[E][ ] pay (from: 4pm to: 7pm) | ||
3.[D][ ] code (by: no idea :-p) | ||
Ok, I've marked this task as not done yet: | ||
[T][ ] cook | ||
Here are the tasks in your list: | ||
1.[T][ ] cook | ||
2.[E][ ] pay (from: 4pm to: 7pm) | ||
3.[D][ ] code (by: no idea :-p) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
todo cook | ||
event pay /from 4am /to 7pm | ||
deadline code /by no idea :-p | ||
mark 1 | ||
list | ||
unmark 1 | ||
list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these class fields have corresponding getters and setters, it might be better to make them private to prevent any unintended modifications.