Skip to content

Commit

Permalink
improve2
Browse files Browse the repository at this point in the history
  • Loading branch information
WYing333 committed Oct 11, 2020
1 parent 885566c commit 7f0cfb8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 66 deletions.
14 changes: 6 additions & 8 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Example 1
Expected outcome:
```
Got it. I've added this task:
[E][] CS2113 tutorial (at: 2020-02-02T00:00)
[E][] CS2113 tutorial (at: 2020-02-02)
```
Example 2
Expand All @@ -85,7 +85,7 @@ Example 2
Expected outcome:
```
Got it. I've added this task:
[E][] CS2113 quiz (at: null)
[E][] CS2113 quiz (at: Monday)
```
### 3.3 Add Deadline: `deadline`
Expand All @@ -105,7 +105,7 @@ Example
Expected outcome:
```
Got it. I've added this task:
[D][] IP submission (by: 2019-03-02T00:00)
[D][] IP submission (by: 2019-03-02)
```
Example
Expand All @@ -114,7 +114,7 @@ Example
Expected outcome:
```
Got it. I've added this task:
[D][] go out to eat (by: null)
[D][] go out to eat (by: today)
```
### 3.4 List All Tasks: `list`
Expand All @@ -128,7 +128,7 @@ Example
Expected outcome:
```
1. [T][] return books
2. [E][] CS2113 tutorial (at: 2020-02-02T00:00)
2. [E][] CS2113 tutorial (at: 2020-02-02)
```
### 3.5 Mark as Done: `done`
Expand Down Expand Up @@ -177,7 +177,7 @@ Example
Expected outcome:
```
1. [E][] CS2113 tutorial (at: 2020-02-02T00:00)
1. [E][] CS2113 tutorial (at: 2020-02-02)
```
### 3.8 Exit the program: `bye`
Expand All @@ -204,5 +204,3 @@ to get the "✓" instead of the "?".
type "chcp 65001"in your cmd
then try run jar by: "java -Dfile.encoding=UTF-8 -jar ip.jar"
3) Due to the implementation of the DateParse, if the time provided by user is invalid time,
then the null will be returned instead of the invalid string.
8 changes: 5 additions & 3 deletions duke.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
D | 1 | ip submission | null
E | 0 | go out to play | null
E | 0 | | null
E | 1 | asd | 2020-02-02
E | 0 | afds | 2020-04-04
T | 0 | adsfgas
D | 0 | asdf | 2019-03-04
E | 0 | adgf | by asg
3 changes: 0 additions & 3 deletions src/main/java/duke.txt
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
T | 0 | go home
D | 0 | ip submission | 20190808
T | 1 | go out to play
7 changes: 4 additions & 3 deletions src/main/java/duke/DateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ public class DateParser {
* @param in String input to be transferred
* @return time in the form of LocalDateTime or null if no String in is found
*/
public static LocalDateTime parseDate(String in) {
public static String parseDate(String in) {

for(DateTimeFormatter df : dformaters) {
try {
return LocalDate.parse(in, df).atStartOfDay();
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return df2.format(LocalDate.parse(in, df));
} catch (DateTimeParseException e) {

}
}

return null;
return in;
}
}
73 changes: 36 additions & 37 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class Duke {
public static void main(String[] args) {

Scanner in = new Scanner(System.in);

UI.showWelcomeScreen();

try {
Expand All @@ -37,10 +36,12 @@ public static void main(String[] args) {
System.out.println("Unable to create file.");
}
}

while (!isTerminated) {
String input = in.nextLine();
parse(input);
}
writeFile("duke.txt");
}

/**
Expand All @@ -53,6 +54,7 @@ public static void openFile() throws FileNotFoundException {
while (s.hasNext()) {
parse(s.nextLine());
}

}

/**
Expand All @@ -62,15 +64,13 @@ public static void openFile() throws FileNotFoundException {
private static void writeFile(String filePath) {
clearFile();
try {
FileWriter fw = new FileWriter(filePath,false);//to not overwrite the file, set to true.
fw.write("\n");
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < tasks.size(); i++) {
fw.write(tasks.get(i).writeToFile()+"\n");
}
fw.close();
}catch (IOException e){
UI.messageSavingError();
System.out.println("\n\tTrouble with IO stream.\n");
}
}

Expand All @@ -95,41 +95,40 @@ public static void clearFile(){
* @param input the input string from the users and the file.
*/
public static void parse (String input) {
while (!input.equals("bye")) {
try {
if(input.equals("done") || input.equals("todo") || input.equals("event") || input.equals("deadline")){
throw new emptyException();
} else if (input.equals("list")) {
UI.list(numOfTasks, tasks);
} else if (input.contains("done")) {
UI.markDone(input, tasks);
} else if (input.contains("todo")||input.startsWith("T")) {
UI.addToDo(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.contains("deadline")||input.startsWith("D")) {
UI.addDeadline(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.contains("event")||input.startsWith("E")) {
UI.addEvent(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.contains("delete")) {
deleteTask(input);
} else if (input.contains("find ")) {
find(input);
} else {
throw new nonMatchException();
}
} catch (StringIndexOutOfBoundsException e) {
UI.messageStringIndexOutOfBoundsException();
} catch (nonMatchException e) {
UI.messageInvalidCommand();
}catch (emptyException ex) {
UI.messageSavingError();
try {
if(input.equals("done") || input.equals("todo") || input.equals("event") || input.equals("deadline")){
throw new emptyException();
}
if (input.equals("bye")) {
isTerminated = true;
UI.bye();
} else if (input.equals("list")) {
UI.list(numOfTasks, tasks);
} else if (input.contains("done")) {
UI.markDone(input, tasks);
} else if (input.startsWith("todo")||input.startsWith("T")) {
UI.addToDo(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.startsWith("deadline")||input.startsWith("D")) {
UI.addDeadline(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.startsWith("event")||input.startsWith("E")) {
UI.addEvent(input, tasks, numOfTasks);
numOfTasks++;
} else if (input.startsWith("delete")) {
deleteTask(input);
} else if (input.startsWith("find ")) {
find(input);
} else {
throw new nonMatchException();
}
} catch (StringIndexOutOfBoundsException e) {
UI.messageStringIndexOutOfBoundsException();
} catch (nonMatchException e) {
UI.messageInvalidCommand();
}catch (emptyException ex) {
UI.messageSavingError();
}
writeFile("duke.txt");
isTerminated = true;
UI.bye();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Deadline extends Task {

protected String by;
public LocalDateTime time;
public String time;

public Deadline(String description, String by) {
super(description);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Event extends Task {

protected String at;
public LocalDateTime time;
public String time;

public Event(String description, String at) {
super(description);
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/duke/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ public static void addDeadline(String in, ArrayList<Task> tasks, int numOfTasks)
tasks.add(new Deadline(sentences[2],sentences[3]));
}
else{
String descriptionOfDeadline;
String by;
descriptionOfDeadline = in.substring(9, (in.indexOf("/") - 1));
by = in.substring((in.indexOf("/") + 4));
tasks.add(new Deadline(descriptionOfDeadline, by));
String[] inputDeadline = in.split("/");
tasks.add(new Deadline(inputDeadline[0].replace("deadline", ""),
inputDeadline[1].replace("by", "")));

}

tasks.get(numOfTasks).setDone(isDone);
Expand All @@ -166,11 +165,10 @@ public static void addEvent(String in, ArrayList<Task> tasks, int numOfTasks) {
tasks.add(new Event(sentences[2],sentences[3]));
}
else {
String descriptionOfEvent;
String at;
descriptionOfEvent = in.substring(6, (in.indexOf("/") - 1));
at = in.substring((in.indexOf("/") + 4));
tasks.add(new Event(descriptionOfEvent, at));
String[] inputEvent = in.split("/");
tasks.add(new Event(inputEvent[0].replace("event", ""),
inputEvent[1].replace("at", "")));

}
tasks.get(numOfTasks).setDone(isDone);
printSuccessfullyAddedMessage(tasks, numOfTasks);
Expand Down Expand Up @@ -212,4 +210,5 @@ public static void bye() {
System.out.println("Bye. Hope to see you again soon!");
UI.printSeparator();
}

}

0 comments on commit 7f0cfb8

Please sign in to comment.