Skip to content
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

[shinichi04] iP #841

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 0 | txt
D | 0 | txt | 2
E | 0 | txt | 2 | 4
175 changes: 175 additions & 0 deletions src/main/java/Axel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

public class Axel {
private static final String FILE_PATH = "data/tasks.txt";
public static void displayList(List<Task> anyList) {
for (int i = 0; i < anyList.size(); i++) {
System.out.println((i + 1) + "." + anyList.get(i).toString());
}
}

public static void saveTasks(List<Task> taskList) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (Task task : taskList) {
writer.write(task.saveFormat());
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error saving tasks: " + e.getMessage());
}
}

public static List<Task> loadTasks() {
List<Task> taskList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
while ((line = reader.readLine()) != null) {
String[] partition = line.split(" \\| ");
String taskType = partition[0];
boolean isDone = partition[1].equals("1");
String taskName = partition[2];
switch (taskType) {
case "T":
ToDo todo = new ToDo(taskName);
if (isDone) todo.mark();
taskList.add(todo);
break;
case "D":
Deadline deadline = new Deadline(taskName, partition[3]);
if (isDone) deadline.mark();
taskList.add(deadline);
break;
case "E":
Event event = new Event(taskName, partition[3], partition[4]);
if (isDone) event.mark();
taskList.add(event);
break;
}
}
} catch (FileNotFoundException e) {
System.out.println("No previous task hehe");
} catch (IOException e) {
System.out.println("Ooops! there is an error in loading tasks: " + e.getMessage());
}
return taskList;
}

public static void main(String[] args) {
String welcomeMsg = " /\\_/\\\n" +
"( ^.^ ) Hii! I am Axel, nice to meet you\n" +
"( >**< )";
Scanner sc = new Scanner(System.in);
List<Task> taskList = loadTasks();
String exitMsg = "Bye, see u again!";

System.out.println(welcomeMsg);
while (sc.hasNext()) {
String prompt = sc.nextLine();
if (prompt.equals("bye")) {
System.out.println(exitMsg);
break;
} else if (prompt.equals("list")) {
System.out.println("Here are the tasks in your list:");
displayList(taskList);
} else if (prompt.startsWith("mark")) {
try {
String idxString = prompt.substring(5);
Integer idxInt = Integer.parseInt(idxString);
taskList.get(idxInt - 1).mark();
System.out.println("Very nice! I have marked this task as done:\n"
+ taskList.get(idxInt - 1).toString());
saveTasks(taskList);
} catch (NumberFormatException e) {
System.out.println("Please input a valid number hehe");
} catch (IndexOutOfBoundsException e) {
System.out.println("Ooops! There is no task to be marked hehe");
}
} else if (prompt.startsWith("unmark")) {
try {
String idxString = prompt.substring(7);
Integer idxInt = Integer.parseInt(idxString);
taskList.get(idxInt - 1).unmark();
System.out.println("OK! I have marked this task as not done yet:\n"
+ taskList.get(idxInt - 1).toString());
saveTasks(taskList);
} catch (NumberFormatException e) {
System.out.println("Please input a valid number hehe");
} catch (IndexOutOfBoundsException e) {
System.out.println("Ooops! There is no task to be unmarked hehe");
}
} else if (prompt.startsWith("delete")) {
try {
String idxString = prompt.substring(7);
Integer idxInt = Integer.parseInt(idxString);
Task task = taskList.remove(idxInt - 1);
System.out.println("Alright! I have deleted this task:\n"
+ task.toString() + "\nNow you have " + taskList.size() + " tasks in the list");
saveTasks(taskList);
} catch (NumberFormatException e) {
System.out.println("Please input a valid number hehe");
} catch (IndexOutOfBoundsException e) {
System.out.println("Ooops! There is no task to be deleted hehe");
}
} else if (prompt.startsWith("todo")) {
try {
String taskName = prompt.substring(4).trim();
if (taskName.isEmpty()) {
throw new IllegalArgumentException("Ooops! Wrong format for todo hehe");
}
ToDo task = new ToDo(taskName);
taskList.add(task);
System.out.println("Got it. I've added this task:\n"
+ task.toString() + "\nNow you have " + taskList.size() + " tasks in the list");
saveTasks(taskList);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
} else if (prompt.startsWith("deadline")) {
try {
String imm1 = prompt.substring(8).trim();
String[] partition = imm1.split("/by");
if (partition.length < 2 || partition[0].trim().isEmpty() || partition[1].trim().isEmpty()) {
throw new IllegalArgumentException("Ooops! Wrong format for deadline hehe");
}
String taskName = partition[0].trim();
String dueBy = partition[1].trim();
Deadline task = new Deadline(taskName, dueBy);
taskList.add(task);
System.out.println("Got it. I've added this task:\n"
+ task.toString() + "\nNow you have " + taskList.size() + " tasks in the list");
saveTasks(taskList);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
} else if (prompt.startsWith("event")) {
try {
String imm1 = prompt.substring(5).trim();
String[] partition1 = imm1.split("/from");
if (partition1.length < 2 || partition1[0].trim().isEmpty() || partition1[1].trim().isEmpty()) {
throw new IllegalArgumentException("Ooops! Wrong format for event hehe");
}
String taskName = partition1[0].trim();
String imm2 = partition1[1];
String[] partition2 = imm2.split("/to");
if (partition2.length < 2 || partition2[0].trim().isEmpty() || partition2[1].trim().isEmpty()) {
throw new IllegalArgumentException("Ooops! Wrong format for event hehe");
}
String from = partition2[0].trim();
String to = partition2[1].trim();
Event task = new Event(taskName, from, to);
taskList.add(task);
System.out.println("Got it. I've added this task:\n"
+ task.toString() + "\nNow you have " + taskList.size() + " tasks in the list");
saveTasks(taskList);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Ooops! I don't understand your command hehe");
}
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Deadline extends Task {
protected String by;

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

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}

public String saveFormat() {
return "D | " + getStatus() + " | " + this.taskName + " | " + this.by;
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import com.sun.source.util.TaskListener;

public class Event extends Task {
protected String from;
protected String to;

public Event(String name, String from, String to) {
super(name);
this.from = from;
this.to = to;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")";
}

public String saveFormat() {
return "E | " + getStatus() + " | " + this.taskName + " | " + this.from + " | " + this.to;
}
}
30 changes: 30 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Task {
protected boolean isDone;
protected String taskName;

public Task(String name) {
this.taskName = name;
this.isDone = false;
}

public void mark() {
this.isDone = true;
}

public void unmark() {
this.isDone = false;
}

public String getStatus() {
return (isDone ? "1" : "0");
}

public String saveFormat() {
return "";
}

@Override
public String toString() {
return "[" + (isDone ? "X" : " ") + "] " + taskName;
}
}
13 changes: 13 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class ToDo extends Task {
public ToDo (String name) {
super(name);
}

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

@Override
public String saveFormat() {
return "T | " + getStatus() + " | " + this.taskName;
}
}
29 changes: 22 additions & 7 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

/\_/\
( ^.^ ) Hii! I am Axel, nice to meet you
( >**< )
Got it. I've added this task:
[T][ ] read book
Now you have 1 tasks in the list
Got it. I've added this task:
[D][ ] assignment 1 (by: Sunday)
Now you have 2 tasks in the list
Got it. I've added this task:
[E][ ] sleeping (from: 9 to: 6)
Now you have 3 tasks in the list
Here are the tasks in your list:
1.[T][ ] read book
2.[D][ ] assignment 1 (by: Sunday)
3.[E][ ] sleeping (from: 9 to: 6)
Very nice! I have marked this task as done:
[D][X] assignment 1 (by: Sunday)
Here are the tasks in your list:
1.[T][ ] read book
2.[D][X] assignment 1 (by: Sunday)
3.[E][ ] sleeping (from: 9 to: 6)
6 changes: 6 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
todo read book
deadline assignment 1 /by Sunday
event sleeping /from 9 /to 6
list
mark 2
list
2 changes: 1 addition & 1 deletion text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 (
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath ..\bin Axel < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT