Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
chongjunwei committed Sep 8, 2021
1 parent f6aa489 commit 4a0dbbb
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 18 deletions.
6 changes: 4 additions & 2 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
T | 1 | read book
D | 0 | return book | June 6th
E | 0 | project meeting | Aug 6th 2-4pm
D | 1 | return book | 2013-03-03
E | 0 | project meeting | 2012-01-03
T | 1 | join sports club
D | 0 | return book | 2015-03-03
E | 1 | sleep | 2013-03-03
15 changes: 11 additions & 4 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Deadline extends Task {

protected String by;
protected LocalDate time;

public Deadline(String description, String by) {
public Deadline(String description, LocalDate time) {
super(description);
this.by = by;
this.time = time;
}

public String addToFile() {
return "D | 0 | " + this.description + " | " + this.time;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
return "[D]" + super.toString() + " (by: " + this.time.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")";
}
}
92 changes: 84 additions & 8 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.io.*;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
Expand Down Expand Up @@ -64,13 +64,13 @@ public static void main(String[] args) {
}
System.out.println(" " + (n) + "." + todo.toString());
} else if (type.equals("D")) {
Deadline deadline = new Deadline(description, parsed[3]);
Deadline deadline = new Deadline(description, LocalDate.parse(parsed[3]));
if (parsed[1].equals("1")) {
deadline.markAsDone();
}
System.out.println(" " + (n) + "." + deadline.toString());
} else if (type.equals("E")) {
Event event = new Event(description, parsed[3]);
Event event = new Event(description, LocalDate.parse(parsed[3]));
if (parsed[1].equals("1")) {
event.markAsDone();
}
Expand Down Expand Up @@ -130,15 +130,39 @@ public static void main(String[] args) {
"____________________________________________________________");
}
String task = input.split(" ")[1];
Todo todo = new Todo(task);
// arr.add(new Todo(task));
try {
Scanner reader = new Scanner(file);
List<String> lines = new ArrayList<>();
while (reader.hasNextLine()) {
String data = reader.nextLine();
lines.add(data);
}

file.delete();
try {
file.createNewFile();
PrintWriter writer = new PrintWriter(file);
for (String line : lines) {
writer.println(line);
}
writer.println(todo.addToFile());
writer.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
count++;
String s = "";
if (count > 1 && count != 0) {
s = "s";
}
String reply = "____________________________________________________________\n" +
"Got it. I've added this task:\n " +
arr.get(count-1) + "\n" +
todo.toString() + "\n" +
"Now you have " + count + " task" + s + " in the list.\n" +
"____________________________________________________________";
System.out.println(reply);
Expand All @@ -147,29 +171,81 @@ public static void main(String[] args) {
}
} else if (input.startsWith("deadline")) {
String[] n = input.split(" ", 2)[1].split(" /by ");
arr.add(new Deadline(n[0], n[1]));
// arr.add(new Deadline(n[0], n[1]));
String task = n[0];
LocalDate time = LocalDate.parse(n[1]);
Deadline deadline = new Deadline(task, time);
try {
Scanner reader = new Scanner(file);
List<String> lines = new ArrayList<>();
while (reader.hasNextLine()) {
String data = reader.nextLine();
lines.add(data);
}

file.delete();
try {
file.createNewFile();
PrintWriter writer = new PrintWriter(file);
for (String line : lines) {
writer.println(line);
}
writer.println(deadline.addToFile());
writer.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
count++;
String s = "";
if (count > 1 && count != 0) {
s = "s";
}
String reply = "____________________________________________________________\n" +
"Got it. I've added this task:\n " +
arr.get(count-1) + "\n" +
deadline.toString() + "\n" +
"Now you have " + count + " task" + s + " in the list.\n" +
"____________________________________________________________";
System.out.println(reply);
} else if (input.startsWith("event")) {
String[] n = input.split(" ", 2)[1].split(" /at ");
arr.add(new Event(n[0], n[1]));
// arr.add(new Event(n[0], n[1]));
String task = n[0];
LocalDate time = LocalDate.parse(n[1]);
Event event = new Event(task, time);
try {
Scanner reader = new Scanner(file);
List<String> lines = new ArrayList<>();
while (reader.hasNextLine()) {
String data = reader.nextLine();
lines.add(data);
}

file.delete();
try {
file.createNewFile();
PrintWriter writer = new PrintWriter(file);
for (String line : lines) {
writer.println(line);
}
writer.println(event.addToFile());
writer.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
count++;
String s = "";
if (count > 1 && count != 0) {
s = "s";
}
String reply = "____________________________________________________________\n" +
"Got it. I've added this task:\n " +
arr.get(count-1) + "\n" +
event.toString() + "\n" +
"Now you have " + count + " task" + s + " in the list.\n" +
"____________________________________________________________";
System.out.println(reply);
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Event extends Task {

protected String at;
protected LocalDate time;

public Event(String description, String at) {
public Event(String description, LocalDate time) {
super(description);
this.at = at;
this.time = time;
}

public String addToFile() {
return "E | 0 | " + this.description + " | " + this.time;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
return "[E]" + super.toString() + " (at: " + this.time.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")";
}
}
4 changes: 4 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public Todo(String description) {
super(description);
}

public String addToFile() {
return "T | 0 | " + this.description;
}

@Override
public String toString() {
return "[T]" + super.toString();
Expand Down

0 comments on commit 4a0dbbb

Please sign in to comment.