-
Notifications
You must be signed in to change notification settings - Fork 435
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
[Rena] iP #458
base: master
Are you sure you want to change the base?
[Rena] iP #458
Changes from 10 commits
55f9f9f
f837ddb
a6f7324
e038c80
ec99ab5
8bf1856
0b4044d
bfe14f0
beff8a4
3103d89
52770ed
e65d1c3
c9ede7f
7c7eb0f
a45e142
cecd03d
396cf5c
11e9506
f829900
89331f7
4459683
61137a6
eb8604d
c1110ed
e785e0e
fca0a8a
b21992a
1f9dfe6
96ec174
d8808e1
a7c37b3
ed78efc
1fa1a03
222176b
f075d9c
14682ee
0daaeec
a8872c7
774cdc7
b3794f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ bin/ | |
|
||
/text-ui-test/ACTUAL.TXT | ||
text-ui-test/EXPECTED-UNIX.TXT | ||
*.class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
T | 0 | read book | | ||
E | 0 | read | mon - thurs | ||
D | 0 | homework | tomorrow | ||
T | 0 | read book | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.FileWriter; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class DataManager { | ||
private static String path; | ||
|
||
public DataManager(String path) { | ||
this.path = path; | ||
} | ||
|
||
private static String convertToFile(Task task) { | ||
String symbol = " "; | ||
String time = " "; | ||
String status = " "; | ||
if(task instanceof Todo) { | ||
symbol = "T"; | ||
}else if(task instanceof Deadline) { | ||
symbol = "D"; | ||
time = ((Deadline) task).getTime(); | ||
}else{ | ||
symbol = "E"; | ||
time = ((Event)task).getFrom() + " - " + ((Event)task).getTo(); | ||
} | ||
if(task.getStatus()) { | ||
status = "1"; | ||
}else{ | ||
status = "0"; | ||
} | ||
return symbol + " | " + status + " | " + task.name + " | " + time; | ||
} | ||
|
||
public static void saveTasks(ArrayList<Task> tasks) { | ||
try{ | ||
File dataFile = new File(path); | ||
dataFile.getParentFile().mkdirs(); | ||
FileWriter writer = new FileWriter(dataFile, false); | ||
for (Task task : tasks) { | ||
writer.write(convertToFile(task) + "\n"); | ||
} | ||
writer.close(); | ||
} catch (IOException e) { | ||
System.out.println("Unable to save tasks."); | ||
} | ||
} | ||
|
||
public ArrayList<Task> retrieveTasks () { | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
try{ | ||
File file = new File(path); | ||
if(file.exists()) { | ||
Scanner scanner = new Scanner(file); | ||
while(scanner.hasNextLine()) { | ||
String data =scanner.nextLine(); | ||
Task task = getTask(data); | ||
if (task != null) { | ||
tasks.add(task); | ||
} | ||
} | ||
scanner.close(); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Unable to load tasks."); | ||
} | ||
return tasks; | ||
} | ||
|
||
public static boolean getStatus(String num) { | ||
return num.equals("1"); | ||
} | ||
public static Task getTask(String message) { | ||
Task task = null; | ||
String parts[] = message.split(" \\| "); | ||
String type = parts[0]; | ||
boolean status = getStatus(parts[1]); | ||
String taskName = parts[2]; | ||
String time = parts[3]; | ||
|
||
if(type.equals("T")) { | ||
task = new Todo(taskName, status); | ||
}else if(type.equals("D")) { | ||
task = new Deadline(taskName, status, time); | ||
}else { | ||
String timeParts[] = time.split(" - "); | ||
String from = timeParts[0]; | ||
String to = timeParts[1]; | ||
task = new Event(taskName, status, from, to ); | ||
} | ||
return task; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public class Deadline extends Task { | ||
protected String by; | ||
|
||
public Deadline(String name, boolean isDone, String by) { | ||
super(name, isDone); | ||
|
||
this.by = by; | ||
} | ||
|
||
public String getTime() { | ||
return this.by; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "[D] " + super.toString() + " (by:" + by + ")"; | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably store the lines as a constant value to make the code easier to change. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,163 @@ | ||||||
import java.util.ArrayList; | ||||||
import java.util.Scanner; | ||||||
public class Duke { | ||||||
public static void main(String[] args) { | ||||||
String logo = " ____ _ \n" | ||||||
private static DataManager file = new DataManager("./data/duke.txt"); | ||||||
public static String printIntro() { | ||||||
return " ____ _ \n" | ||||||
+ "| _ \\ _ _| | _____ \n" | ||||||
+ "| | | | | | | |/ / _ \\\n" | ||||||
+ "| |_| | |_| | < __/\n" | ||||||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||||||
System.out.println("Hello from\n" + logo); | ||||||
+ "|____/ \\__,_|_|\\_\\___|\n" + "____________________________________________________________\n" + | ||||||
" Hello! I'm Duke\n" + | ||||||
" What can I do for you?\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} | ||||||
|
||||||
public static String printOutro() { | ||||||
return "____________________________________________________________\n" + | ||||||
" Bye. Hope to see you again soon!\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} | ||||||
|
||||||
public static String list(ArrayList<Task> a, int counter) { | ||||||
String x = ("Here are the tasks in your list: " + "\n" + | ||||||
"____________________________________________________________\n"); | ||||||
String result = ""; | ||||||
for (int i = 0; i < counter; i++) { | ||||||
result += (i + 1) + ". " + a.get(i).toString() + "\n"; | ||||||
} | ||||||
String y = ("____________________________________________________________\n"); | ||||||
return x + result + y; | ||||||
} | ||||||
|
||||||
public static String printMark(Task task) { | ||||||
if (task.getStatus()) { | ||||||
return "____________________________________________________________\n" + | ||||||
"Nice! I've marked this task as done:" + "\n" + task.toString() + "\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} else { | ||||||
return "____________________________________________________________\n" + | ||||||
"OK, I've marked this task as not done yet-:" + "\n" + task.toString() + "\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} | ||||||
} | ||||||
|
||||||
public static String added(Task task, int count) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Methods should have verbs as names. |
||||||
return "____________________________________________________________\n" + | ||||||
"Got it.I've added this task:" + "\n" + task.toString() + "\n" + | ||||||
"Now you have " + count + " tasks in the list." + "\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} | ||||||
|
||||||
public static String getTask(String message) { | ||||||
int start = message.indexOf(" ") + 1; | ||||||
int end = message.indexOf("/"); | ||||||
if (end == -1) { | ||||||
// "/" not found, handle the case, e.g., throw an exception or return the rest of the string | ||||||
end = message.length(); | ||||||
} | ||||||
String task = message.substring(start, end); | ||||||
return task.trim(); | ||||||
} | ||||||
|
||||||
|
||||||
public static String getTime(String message) { | ||||||
int start = message.indexOf("/"); | ||||||
String time = message.substring(start + 1); | ||||||
return time; | ||||||
} | ||||||
|
||||||
public static String deleteMessage(Task task, int count) { | ||||||
return "____________________________________________________________\n" + | ||||||
"Noted. I've removed this task:" + "\n" + task.toString() + "\n" + | ||||||
"Now you have " + count + " tasks in the list." + "\n" + | ||||||
"____________________________________________________________\n"; | ||||||
} | ||||||
|
||||||
|
||||||
public static String replace(String message) { | ||||||
String newString = message.replaceAll("/(\\w+)", "$1:"); | ||||||
return newString; | ||||||
} | ||||||
|
||||||
public static void main(String[] args) { | ||||||
System.out.println(printIntro()); | ||||||
Scanner input = new Scanner(System.in); | ||||||
System.out.println("Enter Message"); | ||||||
|
||||||
final int ArraySize = 100; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constants should be in screaming_snake_case. |
||||||
ArrayList<Task> tasks = file.retrieveTasks(); | ||||||
int counter = tasks.size(); | ||||||
|
||||||
while (true) { | ||||||
String message = input.nextLine(); | ||||||
|
||||||
try { | ||||||
if (message.equals("bye")) { | ||||||
System.out.println(printOutro()); | ||||||
break; | ||||||
|
||||||
} else if (message.equals("list")) { | ||||||
System.out.println(list(tasks, tasks.size())); | ||||||
|
||||||
} else if (message.startsWith("mark") || message.startsWith("unmark") || message.startsWith("delete")) { | ||||||
String[] parts = message.split(" "); | ||||||
int num = Integer.parseInt(parts[1]); | ||||||
Task current = tasks.get(num - 1); | ||||||
|
||||||
if (message.startsWith("mark")) { | ||||||
current.markAsDone(); | ||||||
System.out.println(printMark(current)); | ||||||
file.saveTasks(tasks); | ||||||
} else if (message.startsWith("unmark")){ | ||||||
current.unmark(); | ||||||
System.out.println(printMark(current)); | ||||||
file.saveTasks(tasks); | ||||||
}else{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you may have forgotten to add a space before and after the 'else' here. |
||||||
tasks.remove(num - 1); | ||||||
System.out.println(deleteMessage(current, tasks.size())); | ||||||
file.saveTasks(tasks); | ||||||
} | ||||||
} else if (message.equals("todo") || message.equals("deadline") || message.equals("event")){ | ||||||
throw new DukeExceptions("Don't forget the description !"); | ||||||
} else if(message.startsWith("todo") || message.startsWith("deadline") || message.startsWith("event")) { | ||||||
String time = getTime(message); | ||||||
if (message.startsWith("todo")) { | ||||||
String[] parts = message.split(" ", 2); | ||||||
String task = parts[1]; | ||||||
// tasks[counter] = new Todo(task); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider removing these comments once code is finalised :) |
||||||
tasks.add(new Todo(task, false)); | ||||||
counter++; | ||||||
System.out.println(added(tasks.get(counter - 1), counter)); | ||||||
file.saveTasks(tasks); | ||||||
} else if (message.startsWith("deadline")) { | ||||||
String task = getTask(message); | ||||||
String[] parts = time.split("by"); | ||||||
// tasks[counter] = new Deadline(task, parts[1]); | ||||||
tasks.add(new Deadline(task, false, parts[1]) ); | ||||||
counter++; | ||||||
System.out.println(added(tasks.get(counter - 1), counter)); | ||||||
file.saveTasks(tasks); | ||||||
} else { | ||||||
String task = getTask(message); | ||||||
String[] parts = time.split("from"); | ||||||
String[] dateParts = parts[1].split("/to"); | ||||||
// tasks[counter] = new Event(task, dateParts[0], dateParts[1]); | ||||||
tasks.add(new Event(task, false, dateParts[0], dateParts[1])); | ||||||
counter++; | ||||||
System.out.println(added(tasks.get(counter - 1), counter)); | ||||||
file.saveTasks(tasks); | ||||||
} | ||||||
|
||||||
} else { | ||||||
throw new DukeExceptions("Sorry, I'm not sure what you mean"); | ||||||
} | ||||||
|
||||||
|
||||||
} catch (DukeExceptions e) { | ||||||
System.out.println(e.getMessage()); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class DukeExceptions extends Exception{ | ||
public DukeExceptions(String message) { | ||
super (message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class Event extends Task{ | ||
protected String from; | ||
protected String to; | ||
|
||
public Event(String name, boolean isDone, String from, String to){ | ||
super(name, isDone); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
public String getTo(){ | ||
return this.to; | ||
} | ||
|
||
public String getFrom() { | ||
return this.from; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E] " + super.toString() + " (from:" + from + " to:" + to + ")" ; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import javax.xml.namespace.QName; | ||
|
||
|
||
public class Task { | ||
protected String name; | ||
protected boolean isDone; | ||
|
||
public Task(String name, boolean isDone) { | ||
this.name = name; | ||
this.isDone = isDone; | ||
} | ||
|
||
public String getStatusIcon(){ | ||
return(isDone? "X" : " "); | ||
} | ||
|
||
public Boolean getStatus() { return isDone; } | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void unmark(){ | ||
this.isDone = false; | ||
} | ||
|
||
// public String getTask() { | ||
// return this.name; | ||
// } | ||
|
||
public String toString() { | ||
return "[" + this.getStatusIcon() + "] " + this.name ; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class Todo extends Task{ | ||
|
||
public Todo(String name, boolean isDone) { | ||
super(name, isDone); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "[T] " + super.toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could insert a space between } and else if