Skip to content

Commit c7cd15a

Browse files
committed
Level 7 implemented
1 parent d1eeead commit c7cd15a

File tree

8 files changed

+175
-68
lines changed

8 files changed

+175
-68
lines changed

database/duke.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deadline | return book | Sunday | true

src/main/java/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public enum Constants {
2+
TODO, DEADLINE, EVENT, LIST, MARK, UNMARK, DELETE, BYE
3+
4+
}

src/main/java/Deadline.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
public class Deadline extends Task {
22

3-
protected String by;
3+
protected String deadline;
44

5-
public Deadline(String description, String by) {
6-
super(description);
7-
this.by = by;
5+
public Deadline(String description) {
6+
super(description.substring(9, description.indexOf('/') - 1));
7+
this.deadline = description.substring(description.indexOf('/') + 3);
8+
}
9+
10+
@Override
11+
public String fileFormat() {
12+
return String.format("deadline | %s | %s | %b", super.description, deadline, super.isDone);
813
}
914

1015
@Override
1116
public String toString() {
12-
return "[D]" + super.toString() + " (by: " + by + ")";
17+
return "[D]" + super.toString() + " (by:" + deadline + ")";
1318
}
1419
}

src/main/java/Duke.java

Lines changed: 129 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
import java.io.File;
2+
import java.io.FileWriter;
3+
import java.io.IOException;
14
import java.util.Scanner;
25
import java.util.ArrayList;
36

47
public class Duke {
58

69
public Task[] tasks;
710

11+
private static final ArrayList<Task> collection = new ArrayList<>();
812

9-
public static void main(String[] args) throws DukeException {
13+
public static final Scanner sc = new Scanner(System.in);
14+
15+
16+
public static boolean isNotValid(String str) {
17+
String[] s = str.split(" ");
18+
return (s.length < 1);
19+
}
20+
21+
22+
23+
24+
public static void main(String[] args) throws DukeException, IOException, IllegalCommandException {
1025
String logo = " ____ _ \n"
1126
+ "| _ \\ _ _| | _____ \n"
1227
+ "| | | | | | | |/ / _ \\\n"
@@ -15,116 +30,172 @@ public static void main(String[] args) throws DukeException {
1530
System.out.println("Hello from\n" + logo);
1631
System.out.println("___________________________________");
1732
System.out.println("Hello! I'm Duke\n What can I do for you?");
18-
System.out.println("___________________________________");
33+
System.out.println("____________________________________");
1934

20-
Task[] tasks = new Task[100];
21-
int counter = 0;
22-
ArrayList<Task> collection = new ArrayList<>();
23-
24-
Scanner sc = new Scanner(System.in);
2535
String input = sc.nextLine();
2636

37+
File file = new File("database/duke.txt");
38+
if (file.createNewFile()) {
39+
System.out.println("File has been created: " + file.getName());
40+
} else {
41+
System.out.println("File already exists.");
42+
Scanner reader = new Scanner(file);
43+
while (reader.hasNext()) {
44+
reader.useDelimiter(" \\| ");
45+
Constants constant = Constants.valueOf(reader.next().toUpperCase());
46+
Task task;
47+
switch (constant) {
48+
case TODO:
49+
String tdDescription = reader.next();
50+
Todo todo = new Todo(tdDescription);
51+
collection.add(todo);
52+
break;
53+
case DEADLINE:
54+
String dlDescription = reader.next();
55+
Deadline deadline = new Deadline(dlDescription);
56+
collection.add(deadline);
57+
break;
58+
case EVENT:
59+
String evDescription = reader.next();
60+
Event event = new Event(evDescription);
61+
collection.add(event);
62+
break;
63+
64+
default:
65+
throw new IllegalCommandException("Illegal command given");
66+
}
67+
}
68+
}
69+
70+
2771

2872

2973
while (!input.equals("bye")) {
74+
3075
if (input.equals("list")) {
3176
System.out.println("___________________________________");
32-
for (int i = 0; i < counter; i++) {
77+
for (int i = 0; i < collection.size(); i++) {
3378
System.out.println( (i+1) + "."
3479
+ collection.get(i).toString());
3580
}
3681
System.out.println("___________________________________");
37-
input = sc.next();
82+
input = sc.nextLine();
3883

3984
}
4085

41-
else if (input.equals("mark")) {
42-
int number = sc.nextInt();
86+
if (input.startsWith("mark")) {
87+
int number = Integer.parseInt(input.substring(5));
4388
collection.get(number-1).mark();
4489
System.out.println("___________________________________");
4590
System.out.println("Nice! I've marked this task done: " + "\n"
4691
+ "[" + collection.get(number-1).getStatusIcon() + "] " +
4792
collection.get(number-1).description);
4893
System.out.println("___________________________________");
49-
input = sc.next();
94+
input = sc.nextLine();
5095
}
5196

52-
else if (input.equals("unmark")) {
53-
int number = sc.nextInt();
97+
if (input.equals("unmark")) {
98+
int number = Integer.parseInt(input.substring(7));
5499
collection.get(number-1).unmark();
55100
System.out.println("___________________________________");
56101
System.out.println("OK, I've marked this task as not done yet: " + "\n"
57102
+ "[" + collection.get(number-1).getStatusIcon() + "] " +
58103
collection.get(number-1).description);
59104
System.out.println("___________________________________");
60-
input = sc.next();
105+
input = sc.nextLine();
61106
}
62107

63-
else if (input.equals("deadline")) {
64-
String what = sc.nextLine();
65-
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
66-
String byWhen = sc.nextLine();
67-
collection.add(new Deadline(what, byWhen));
68-
System.out.println("___________________________________");
69-
System.out.println("Got it. I've added this task:" + "\n"
70-
+ " " + collection.get(counter).toString() + "\n"
71-
+ "Now you have " + (counter+1) + " tasks in the list.");
72-
System.out.println("___________________________________");
73-
counter++;
74-
input = sc.nextLine();
108+
if (input.startsWith("deadline")) {
109+
try {
110+
if (isNotValid(input))
111+
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
112+
Deadline deadline = new Deadline(input);
113+
collection.add(deadline);
114+
System.out.println("___________________________________");
115+
System.out.println("Got it. I've added this task:" + "\n"
116+
+ " " + deadline.toString() + "\n"
117+
+ "Now you have " + collection.size() + " tasks in the list.");
118+
System.out.println("___________________________________");
119+
} catch(DukeException e) {
120+
System.out.println(e.getMessage());
121+
122+
} finally {
123+
input = sc.nextLine();
124+
}
75125
}
76126

77-
else if (input.equals("todo")) {
78-
String what = sc.nextLine();
79-
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
80-
collection.add(new Todo(what));
81-
System.out.println("___________________________________");
82-
System.out.println("Got it. I've added this task:" + "\n"
83-
+ " " + collection.get(counter).toString() + "\n"
84-
+ "Now you have " + (counter+1) + " tasks in the list.");
85-
System.out.println("___________________________________");
86-
counter++;
87-
input = sc.nextLine();
127+
if (input.startsWith("todo")) {
128+
try {
129+
if (isNotValid(input))
130+
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
131+
Todo todo = new Todo(input);
132+
collection.add(todo);
133+
System.out.println("___________________________________");
134+
System.out.println("Got it. I've added this task:" + "\n"
135+
+ " " + todo.toString() + "\n"
136+
+ "Now you have " + collection.size() + " tasks in the list.");
137+
System.out.println("___________________________________");
138+
} catch(DukeException e) {
139+
System.out.println(e.getMessage());
140+
141+
} finally {
142+
input = sc.nextLine();
143+
}
88144
}
89145

90-
else if (input.equals("event")) {
91-
String what = sc.nextLine();
92-
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
93-
String atWhen = sc.nextLine();
94-
collection.add(new Event(what, atWhen));
95-
System.out.println("___________________________________");
96-
System.out.println("Got it. I've added this task:" + "\n"
97-
+ " " + collection.get(counter).toString() + "\n"
98-
+ "Now you have " + (counter+1) + " tasks in the list.");
99-
System.out.println("___________________________________");
100-
counter++;
101-
input = sc.nextLine();
146+
if (input.startsWith("event")) {
147+
try {
148+
if (isNotValid((input)))
149+
throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
150+
Event event = new Event(input);
151+
collection.add(event);
152+
System.out.println("___________________________________");
153+
System.out.println("Got it. I've added this task:" + "\n"
154+
+ " " + event.toString() + "\n"
155+
+ "Now you have " + collection.size() + " tasks in the list.");
156+
System.out.println("___________________________________");
157+
} catch(DukeException e) {
158+
System.out.println(e.getMessage());
159+
160+
} finally {
161+
input = sc.nextLine();
162+
}
102163
}
103164

104-
else if (input.equals("delete")) {
105-
int number = sc.nextInt();
165+
166+
if (input.startsWith("delete")) {
167+
int number = Integer.parseInt(input.substring(7));
106168
Task temp = collection.get(number-1);
107169
collection.remove(number-1);
108-
counter--;
109170
System.out.println("___________________________________");
110171
System.out.println("Noted. I've removed this task:" + "\n"
111172
+ " " + temp.toString() + "\n"
112-
+ "Now you have " + counter + " tasks in the list.");
173+
+ "Now you have " + collection.size() + " tasks in the list.");
113174
System.out.println("___________________________________");
114-
input = sc.next();
175+
input = sc.nextLine();
115176

116177
}
117178

118179
else {
119-
System.out.println("___________________________________");
120-
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
121-
180+
try {
181+
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(.");
182+
} catch (DukeException e) {
183+
System.out.println(e.getMessage());
184+
} finally {
185+
input = sc.nextLine();
186+
}
122187
}
123188

124189
}
190+
FileWriter fileWriter = new FileWriter(file);
191+
for (Task task : collection) {
192+
fileWriter.write(task.fileFormat() + System.lineSeparator());
193+
}
194+
fileWriter.close();
125195

126196
System.out.println("___________________________________");
127197
System.out.println("Bye. Hope to see you again soon!");
128198
System.out.println("___________________________________");
129199
}
200+
130201
}

src/main/java/Event.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import java.io.FileWriter;
2+
import java.io.IOException;
3+
14
public class Event extends Task{
25

36
protected String at;
47

5-
public Event(String description, String at) {
6-
super(description);
7-
this.at = at;
8+
public Event(String description) {
9+
super(description.substring(6, description.indexOf('/') - 1));
10+
this.at = description.substring(description.indexOf('/') + 3);
11+
}
12+
13+
@Override
14+
public String fileFormat() {
15+
return String.format("event | %s | %s | %b", super.description, at, super.isDone);
816
}
917

1018
@Override
1119
public String toString() {
12-
return "[E]" + super.toString() + " (at: " + at + ")";
20+
return "[E]" + super.toString() + " (at:" + at + ")";
1321
}
1422
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class IllegalCommandException extends Exception {
2+
3+
public IllegalCommandException(String message) {
4+
super(message);
5+
}
6+
}

src/main/java/Task.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.io.FileWriter;
2+
import java.io.IOException;
3+
14
public class Task {
25
protected String description;
36
protected boolean isDone;
@@ -19,6 +22,10 @@ public void unmark() {
1922
this.isDone = false;
2023
}
2124

25+
public String fileFormat() {
26+
return String.format("todo | %s | %b", description, isDone);
27+
}
28+
2229
@Override
2330
public String toString() {
2431
return "[" + this.getStatusIcon() + "] " + description;

src/main/java/Todo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
public class Todo extends Task {
22

33
public Todo(String description) {
4-
super(description);
4+
super(description.substring(5));
5+
}
6+
7+
@Override
8+
public String fileFormat() {
9+
return String.format("todo | %s | %b", super.description, super.isDone);
510
}
611

712
@Override

0 commit comments

Comments
 (0)