-
Notifications
You must be signed in to change notification settings - Fork 193
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
[Xue Yushan] iP #197
base: master
Are you sure you want to change the base?
[Xue Yushan] iP #197
Changes from 18 commits
85d1982
cac3d8e
1fd8497
6466eb7
6141721
babe161
d198914
1a6a0ac
9f131cc
7cac7da
6be3ec0
5aa495e
ecee40d
1b59f78
8cfb82a
e060cf4
0cf473f
b8d0935
d69ddd3
97a99f5
3516ed4
0d92829
6c1b940
27ea764
3a00f4e
329a47e
de1b451
22393ec
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Deadline extends Task { | ||
public String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + "(by: " + this.by | ||
+ ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,289 @@ | ||
import java.util.Scanner; | ||
import java.lang.String; | ||
import java.util.ArrayList; | ||
import java.io.*; | ||
|
||
public class Duke { | ||
|
||
public static final String line = "____________________________________________________________\n"; | ||
public static final int MAX_NUM_OF_TASKS = 100; | ||
public static final int TODO_POS = 5; | ||
public static final int BY_POS = 4; | ||
public static final int DEADLINE_POS = 9; | ||
public static final int AT_POS = 4; | ||
public static final int EVENT_POS = 6; | ||
public static final int TYPE_POS = 4; | ||
public static final int TASK_POS = 10; | ||
|
||
public static void main(String[] args) { | ||
|
||
greeting(); | ||
ArrayList<Task> tasks = getDatafromFile("tasks"); | ||
|
||
if (tasks.size() == 0) { | ||
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. You can also do something like:
|
||
System.out.println("No tasks can be loaded."); | ||
} else { | ||
System.out.println("Tasks have been loaded."); | ||
} | ||
|
||
|
||
Scanner input = new Scanner(System.in); | ||
while (true) { | ||
String inputString = input.nextLine(); | ||
if (inputString.equals("bye")) { | ||
saveDataToFile("tasks", tasks); | ||
exit(); | ||
break; | ||
} else if (inputString.equals("list")) { | ||
try { | ||
printList(tasks); | ||
} catch (DukeException d) { | ||
System.out.println(line | ||
+ d.getMessage() + "\n" + line); | ||
} | ||
} else if (inputString.contains("done") == true) { | ||
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. There is no need to explicitly check if it is equal to true because the contains method returns a boolean value. You can change this to:
|
||
try { | ||
setDone(inputString, tasks); | ||
} catch (IndexOutOfBoundsException i) { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The index is out of range.\n" | ||
+ line); | ||
} catch (NumberFormatException n) { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The index of done indstruction should be a number.\n" | ||
+ line); | ||
} catch (NullPointerException p) { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The index of done indstruction cannot be empty.\n" | ||
+ line); | ||
} | ||
} else if (inputString.contains("delete") == true) { | ||
try { | ||
deleteTask(inputString, tasks); | ||
} catch (IndexOutOfBoundsException i) { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The index is out of range.\n" | ||
+ line); | ||
} catch (NumberFormatException n) { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The index of delete indstruction should be a number.\n" | ||
+ line); | ||
} | ||
} else if (inputString.contains("clear") == true) { | ||
clearList(tasks); | ||
} else { | ||
try { | ||
addTask(inputString, tasks); | ||
} | ||
catch(IndexOutOfBoundsException i) { | ||
String type = ""; | ||
if (inputString.contains("todo")) { | ||
type = "todo"; | ||
} else if (inputString.contains("deadline")) { | ||
type = "deadline"; | ||
} else if (inputString.contains("event")) { | ||
type = "event"; | ||
} | ||
if (type == "") { | ||
System.out.println(line | ||
+ "☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n" | ||
+ line); | ||
} else { | ||
System.out.println(line | ||
+ "☹ OOPS!!! The description of a " + type + " cannot be empty.\n" | ||
+ line); | ||
} | ||
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. Avoid using so many nested try catch blocks. Please abstract out the logic into different functions |
||
} | ||
} | ||
} | ||
} | ||
|
||
public static final void greeting() { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
System.out.println(line | ||
+ "Hello! I'm Duke\n" | ||
+ "What can I do for you?\n" | ||
+ line); | ||
} | ||
|
||
public static final void exit() { | ||
System.out.println(line | ||
+ "Bye. Hope to see you again soon!\n" | ||
+ line); | ||
} | ||
|
||
public static final void printList(ArrayList<Task> tasks) throws DukeException{ | ||
if (tasks.size() == 0) { | ||
throw new DukeException("The list is empty!"); | ||
} else { | ||
System.out.println(line | ||
+ "Here are the tasks in your list: "); | ||
|
||
for (int index = 0; index < tasks.size(); index++) { | ||
System.out.println((index + 1) + ". " | ||
+ tasks.get(index).toString()); | ||
} | ||
System.out.println(line); | ||
} | ||
} | ||
|
||
public static final void setDone(String input, ArrayList<Task> tasks) { | ||
String num = ""; | ||
for (int i = 0; i < input.length(); i++) { | ||
char c = input.charAt(i); | ||
if (Character.isDigit(c) == true) { | ||
num += c; | ||
} | ||
} | ||
|
||
int index = Integer.parseInt(num); | ||
tasks.get(index - 1).markedAsDone(); | ||
System.out.println(line | ||
+ "Nice! I've marked this task as done: \n" | ||
+ tasks.get(index - 1) + "\n" | ||
+ line); | ||
} | ||
|
||
public static final void addTask(String input, ArrayList<Task> tasks) throws NullPointerException { | ||
if (input.contains("todo") == true) { | ||
tasks.add(new Todo(input.substring(TODO_POS))); | ||
} else if (input.contains("deadline") == true) { | ||
String by = input.substring(input.indexOf("/") + BY_POS); | ||
tasks.add(new Deadline(input.substring(DEADLINE_POS, | ||
input.indexOf("/")), by)); | ||
} else if (input.contains("event") == true) { | ||
String at = input.substring(input.indexOf("/") + AT_POS); | ||
tasks.add(new Event(input.substring(EVENT_POS, | ||
input.indexOf("/")), at)); | ||
} else { | ||
System.out.print(tasks.get(tasks.size() + 1).toString()); | ||
} | ||
|
||
System.out.println(line | ||
+ "Got it. I've added this task: \n" | ||
+ tasks.get(tasks.size() - 1) + "\n" | ||
+ "Now you have " + tasks.size() | ||
+ " tasks in the list\n" | ||
+ line); | ||
} | ||
|
||
public static final void deleteTask(String input, ArrayList<Task> tasks) { | ||
String num = ""; | ||
for (int i = 0; i < input.length(); i++) { | ||
char c = input.charAt(i); | ||
if (Character.isDigit(c) == true) { | ||
num += c; | ||
} | ||
} | ||
|
||
int index = Integer.parseInt(num); | ||
Task removedTask = tasks.get(index - 1); | ||
tasks.remove(index - 1); | ||
System.out.println(line | ||
+ "Noted. I've removed this task: \n" | ||
+ removedTask.toString() | ||
+ "\nNow you have " + tasks.size() | ||
+ " tasks in the list.\n" + line); | ||
} | ||
|
||
public static final void clearList(ArrayList<Task> tasks) { | ||
while (tasks.size() > 0) { | ||
tasks.remove(0); | ||
} | ||
|
||
System.out.println(line | ||
+ "The list has been cleared. Now the list is empty.\n" | ||
+ line); | ||
} | ||
|
||
public static final ArrayList<Task> getDatafromFile(String fileName) { | ||
ArrayList<Task> tasks = new ArrayList<Task>(); | ||
String path = "C:\\Users\\XUEY0013\\Documents\\ip\\src\\main\\" + fileName+ ".txt"; | ||
File file = new File(path); | ||
if (!file.exists()) { | ||
return new ArrayList<Task> (); | ||
} | ||
BufferedReader reader = null; | ||
try { | ||
FileInputStream fileInputStream = new FileInputStream(path); | ||
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); | ||
reader = new BufferedReader(inputStreamReader); | ||
String tempString = ""; | ||
Task task = new Task("null"); | ||
|
||
while ((tempString = reader.readLine()) != null) { | ||
if (tempString.charAt(TYPE_POS) == 'T') { | ||
task = (new Todo(tempString.substring(TASK_POS))); | ||
} else if (tempString.charAt(TYPE_POS) == 'E') { | ||
String event = tempString.substring(TASK_POS, | ||
tempString.indexOf("(") - 1); | ||
String at = tempString.substring(tempString.indexOf(":") + 2, | ||
tempString.indexOf(")")); | ||
task = (new Event(event, at)); | ||
} else if (tempString.charAt(TYPE_POS) == 'D') { | ||
String deadline = tempString.substring(TASK_POS, | ||
tempString.indexOf("(") - 1); | ||
String by = tempString.substring(tempString.indexOf(":") + 2, | ||
tempString.indexOf(")")); | ||
task = (new Deadline(deadline, by)); | ||
} | ||
|
||
if (tempString.charAt(7) == 'X') { | ||
task.markedAsDone(); | ||
} | ||
|
||
tasks.add(task); | ||
} | ||
reader.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return tasks; | ||
} | ||
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. Having such long functions can be hard to read. Please consider abstracting out repeated code in line with the DRY (Don't Repeat Yourself) principle. Also add comments to improve the readability of your code |
||
|
||
public static final void saveDataToFile(String fileName,ArrayList<Task> tasks) { | ||
BufferedWriter writer = null; | ||
File file = new File("C:\\Users\\XUEY0013\\Documents\\ip\\src\\main\\"+ fileName + ".txt"); | ||
if(!file.exists()) { | ||
try { | ||
file.createNewFile(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
try { | ||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8")); | ||
for (Task task : tasks) { | ||
int i = 0; | ||
writer.write(i + 1 + ". " + task.toString()); | ||
i++; | ||
writer.write("\n"); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if(writer != null){ | ||
writer.close(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
System.out.println("The list of tasks have been saved!"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class DukeException extends Exception { | ||
|
||
public DukeException() { | ||
super(); | ||
} | ||
|
||
public DukeException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Event extends Task { | ||
public String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + "(at: " + this.at | ||
+ ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
public class List { | ||
private static String[] currentList = new String[100]; | ||
public static int listSize = 0; | ||
private static int[] isDone = new int[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. The value 100 is a magic number, consider extracting it as a constant variable 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. Agreed! Also make sure the constants are named using proper naming conventions. |
||
|
||
public static void addToList(String input) { | ||
currentList[listSize] = input; | ||
listSize++; | ||
System.out.println("____________________________________________________________\n" | ||
+ "Added: " + input + "\n" | ||
+ "____________________________________________________________\n"); | ||
} | ||
|
||
public static void printItems() { | ||
if (listSize == 0) { | ||
System.out.println("The list is empty."); | ||
} else { | ||
for (int i = 1; i <= listSize; i++) { | ||
System.out.print(i + ". " ); | ||
System.out.print("["); | ||
if (isDone[i-1] == 1) { | ||
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. Please maintain spacing between the operators and the operands |
||
System.out.print("X"); | ||
} else { | ||
System.out.print(" "); | ||
} | ||
System.out.print("] " + currentList[i-1] + "\n"); | ||
} | ||
} | ||
} | ||
|
||
public static void mark(int number) { | ||
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 renaming this method to make it more clear on what it does. For eg setTaskAsCompleted |
||
isDone[number-1] = 1; | ||
} | ||
|
||
public static void markedItems() { | ||
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. name of this function "markedItems()" sounds like a list. maybe should make it sound like a verb instead |
||
for (int i = 0; i < isDone.length; i++) { | ||
if (isDone[i] == 1) { | ||
System.out.println("[X] " + currentList[i]); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public class Task { | ||
protected String description; | ||
protected String isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = " "; | ||
} | ||
|
||
public final void markedAsDone() { | ||
this.isDone = "X"; | ||
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. As mentioned earlier, avoid using magic strings - please extract them out as constants. |
||
} | ||
|
||
@Override | ||
public String toString() { | ||
String status; | ||
return "[" + isDone + "] " + this.description; | ||
} | ||
} |
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.
The main method exceeds 30 lines, consider refactoring the console input and output into separate methods