Skip to content

Commit 7d0c233

Browse files
Make following changes to the source code
Fix the save functions to allow Jar File to automatically save all tasks when program ends Added JavaDocs to the necessary public functions and classes
1 parent cec7717 commit 7d0c233

File tree

19 files changed

+341
-58
lines changed

19 files changed

+341
-58
lines changed

data/list.txt

Whitespace-only changes.

src/main/java/chingu/Chingu.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package chingu;
22

33
import chingu.command.Command;
4+
import chingu.exception.ChinguException;
45
import chingu.exception.NoCommandException;
56
import chingu.task.TaskList;
67

78
import java.io.IOException;
8-
import java.io.InputStream;
99

1010
/**
1111
* Class Duke that is the main class that helps to run the program
1212
*/
1313
public class Chingu {
1414

15+
private static final String FILEPATH = "store/list.txt";
16+
1517
private Storage storage;
1618
private TaskList tasks;
1719
private Ui ui;
1820

19-
private InputStream filePath = Main.class.getResourceAsStream("/data/list.txt") ;
20-
2121

2222
public Chingu() {
2323
ui = new Ui();
24-
storage = new Storage(filePath);
24+
storage = new Storage(FILEPATH);
2525
try {
2626
tasks = new TaskList(storage.load());
2727
} catch (IOException e) {
28-
ui.showLoadingError();
29-
tasks = new TaskList();
28+
throw new RuntimeException(e);
3029
}
3130
}
3231

@@ -45,18 +44,28 @@ public void run() {
4544
isExit = c.isExit();
4645
} catch (NoCommandException e) {
4746
ui.showError(e.getMessage());
47+
} catch (ChinguException e) {
48+
throw new RuntimeException(e);
4849
} finally {
4950
ui.showLine();
5051
}
5152
}
5253
}
5354

55+
/**
56+
* Collect Command from the user each time and respond to user's command to Chingu
57+
*
58+
* @param userInput which consist of command
59+
* @return string Response to the command of the user
60+
*/
5461
public String getResponse(String userInput) {
5562
String Response = "";
5663
try {
5764
Command newCommand = Parser.parse(userInput);
5865
Response = newCommand.execute(tasks, ui, storage);
5966
} catch (NoCommandException e) {
67+
return ui.showError(e.getMessage());
68+
} catch (ChinguException e) {
6069
throw new RuntimeException(e);
6170
}
6271
return Response;

src/main/java/chingu/Parser.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import chingu.exception.NoCommandException;
1313
import chingu.exception.ToDosException;
1414
import chingu.exception.EventException;
15-
import chingu.task.TaskList;
1615
import chingu.task.Task;
1716
import chingu.task.Event;
1817
import chingu.task.Deadline;
@@ -41,6 +40,13 @@ public static Command parse(String fullCommand) throws NoCommandException {
4140
}
4241
}
4342

43+
/**
44+
* Sorts the 1-word Command from the user
45+
*
46+
* @param fullCommand from the user
47+
* @return Command that is constructed based on User's command
48+
* @throws NoCommandException if no command is present
49+
*/
4450
public static Command parseOneWordCommand(String fullCommand) throws NoCommandException {
4551
if (fullCommand.equals("list")) {
4652
return new ListCommand();
@@ -72,6 +78,13 @@ public static Command parseLongCommand(String firstWord, String furtherDetails)
7278
}
7379
}
7480

81+
/**
82+
* Parse Command that adds ToDo
83+
*
84+
* @param description about ToDo
85+
* @return ToDo Task that is created
86+
* @throws ToDosException if necessary details are missing from the ToDo
87+
*/
7588
public static Task parseToDos(String description) throws ToDosException {
7689
if (description.isEmpty()) {
7790
throw new ToDosException("What todos do you need to record?");
@@ -86,6 +99,14 @@ public static Task parseToDos(String description) throws ToDosException {
8699
return new ToDo(description, priority);
87100
}
88101

102+
/**
103+
* Parse Command that adds Deadline
104+
*
105+
* @param description about Deadline
106+
* @return Deadline that is created
107+
* @throws DeadlineException if necessary details are missing from the Deadline
108+
* @throws DateException if format of the date given is wrong
109+
*/
89110
public static Task parseDeadline(String description) throws DeadlineException, DateException{
90111
if (description.isEmpty()) {
91112
throw new DeadlineException("What deadline do you need to record?");
@@ -107,6 +128,13 @@ public static Task parseDeadline(String description) throws DeadlineException, D
107128
return new Deadline(D, by, priority);
108129
}
109130

131+
/**
132+
* Parse Command taht adds Event
133+
*
134+
* @param description about Event
135+
* @return Event that is created
136+
* @throws EventException if necessary details are missing from the Event
137+
*/
110138
public static Task parseEvent(String description) throws EventException {
111139
if (description.isEmpty()) {
112140
throw new EventException("What event do you need to record?");
@@ -127,6 +155,12 @@ public static Task parseEvent(String description) throws EventException {
127155
return new Event(D, from, to, priority);
128156
}
129157

158+
/**
159+
* Check if the description is splitted correctly
160+
*
161+
* @param length number of splitted descriptions
162+
* @throws DeadlineException if necessary details are missing from the Deadline
163+
*/
130164
public static void checkDeadlineSplit(int length) throws DeadlineException {
131165
if (length < 2) {
132166
throw new DeadlineException("Please insert the priority of the task - /priority High/Mid/low");
@@ -139,6 +173,12 @@ public static void checkEventSplit(int length) throws EventException{
139173
}
140174
}
141175

176+
/**
177+
* Validate if the format of the event dates are correctly added by the user
178+
*
179+
* @param from - start date of the event
180+
* @param to - end date of the event
181+
*/
142182
public static void validateDates(String from, String to) {
143183
boolean validateFromDate = from.matches("[0-9]{4}/[0-9]{2}/[0-9]{2}\\[0-9]{4}");
144184
assert !validateFromDate : "format of the event start date is WRONG!";

0 commit comments

Comments
 (0)