-
Notifications
You must be signed in to change notification settings - Fork 200
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
[Wang Silang] iP #214
base: master
Are you sure you want to change the base?
[Wang Silang] iP #214
Changes from 5 commits
6169785
d047c4e
b1f3481
e45b36b
fbf03b0
9ac72cf
90cbc52
4eee682
21815d8
21238c7
d5393f2
55e06c9
d98e800
a9eebd0
7688011
c240a47
fde76d5
da82fc2
d9e6240
e202337
806f874
4b7e1b8
d703de1
399c136
d914533
b8552f4
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,23 @@ | ||
public class Deadlines extends Task { | ||
private String endTime; | ||
private String taskLabel = "[D]"; | ||
public Deadlines (String input){ | ||
super(input.substring(9,input.indexOf('/') - 1)); // Sanitize input by removing "deadline" at the start | ||
super.setTaskLabel(taskLabel); | ||
endTime = "(" + getEndTime(input) + ")"; | ||
} | ||
// Method of StringBuffer operation taken from | ||
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 like the way you use |
||
// https://www.geeksforgeeks.org/insert-a-string-into-another-string-in-java/ | ||
@Override | ||
public String getEndTime(String input){ | ||
String deadline = input.substring(input.indexOf('/') + 1); // endTime of task is the string after '/' | ||
StringBuffer deadlineCorrectFormat = new StringBuffer(deadline); // convert to StringBuffer for inserting ':' | ||
deadlineCorrectFormat.insert(2,":"); | ||
return deadlineCorrectFormat.toString(); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return this.taskLabel + this.mark + " " + this.description + " " + this.endTime; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import jdk.jfr.Event; | ||
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. where/how is this used? |
||
|
||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
|
@@ -6,5 +11,87 @@ public static void main(String[] args) { | |
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
greetLine(); | ||
addList(); | ||
exitLine(); | ||
} | ||
public static void greetLine(){ | ||
System.out.println("How may I be of service?"); | ||
} | ||
public static void echo() | ||
{ | ||
String line; | ||
Scanner in = new Scanner(System.in); | ||
line = in.nextLine(); | ||
while(!line.equals("bye")) { | ||
System.out.println(line); | ||
line = in.nextLine(); | ||
} | ||
} | ||
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'd want to indent the { } properly, like: // your code |
||
public static void exitLine(){ | ||
System.out.println("Glad I could be of help!"); | ||
} | ||
|
||
public static void addList() | ||
{ | ||
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. Use K&R style brackets (as done in the exitLine method) |
||
Task[] list = new Task[100]; | ||
String line; | ||
Scanner in = new Scanner(System.in); | ||
line = in.nextLine(); | ||
int numOfItems = 0; | ||
while(!line.equals("bye")) { // condition to shut down program | ||
if(line.equals("list")) { // users wants to know all text so far | ||
printCurrentList(list, numOfItems); | ||
} | ||
else if(line.startsWith("mark")){ | ||
mark(line, list); | ||
printCurrentList(list, numOfItems); | ||
} | ||
else if(line.startsWith("unmark")){ | ||
unmark(line,list); | ||
printCurrentList(list, numOfItems); | ||
} | ||
else { // new tasks keyed in by user | ||
Task newTask = new Task(line); | ||
if(line.startsWith("todo")){ | ||
newTask = new ToDos(line); | ||
} | ||
else if(line.startsWith("deadline")){ | ||
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. change the form of the if/else statements to match the coding standard: if (condition) { 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. Yeah, you can refer to this link [https://se-education.org/guides/conventions/java/basic.html] for if-else statement coding standards. 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. Thanks guys! |
||
newTask = new Deadlines(line); | ||
} | ||
else{ | ||
newTask = new Events(line); | ||
} | ||
list[numOfItems] = newTask; | ||
++numOfItems; | ||
} | ||
line = in.nextLine(); // read in next line of text | ||
} | ||
} | ||
|
||
public static void mark(String task, Task[] list) | ||
{ | ||
String indexOfTask = task.substring(5); // get the number of task to be marked | ||
Task taskToBeMarked = list[Integer.parseInt(indexOfTask) - 1]; // convert from 1-based to 0-based | ||
taskToBeMarked.markTask(); | ||
System.out.println("Sir, your task has been marked as completed."); | ||
} | ||
|
||
public static void unmark(String task, Task[] list) | ||
{ | ||
String indexOfTask = task.substring(7); | ||
Task taskToBeMarked = list[Integer.parseInt(indexOfTask) - 1]; // convert from 1-based to 0-based | ||
taskToBeMarked.unmarkTask(); | ||
System.out.println("Sir, your task has been unmarked as requested."); | ||
} | ||
|
||
public static void printCurrentList(Task[] list, int numOfItems) | ||
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. nice job making clear method and variable names! |
||
{ | ||
Task[] subList = Arrays.copyOf(list, numOfItems); | ||
System.out.println("Your current list of items as requested, sir."); | ||
for(int i = 0; i < subList.length; ++i) | ||
{ | ||
System.out.println(Integer.toString(i+1) + "." + subList[i]); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
public class Events extends Task{ | ||
private String timeLine; | ||
private String taskLabel = "[E]"; | ||
public Events (String input){ | ||
super(input.substring(6,input.indexOf('/') - 1)); | ||
super.setTaskLabel(taskLabel); | ||
timeLine = "(" + getStartTime(input) + getEndTime(input) + ")"; | ||
} | ||
public String[] splitInput(String input){ | ||
String[] inputAfterSplit = input.split("/", 3); // split twice to generate three strings | ||
return inputAfterSplit; | ||
} | ||
@Override | ||
public String getStartTime(String input){ | ||
String[] inputAfterSplit = splitInput(input); | ||
String startTime = inputAfterSplit[1]; | ||
StringBuffer startTimeCorrectFormat = new StringBuffer(startTime); | ||
startTimeCorrectFormat.insert(4,":"); | ||
return startTimeCorrectFormat.toString(); | ||
} | ||
@Override | ||
public String getEndTime(String input){ | ||
String[] inputAfterSplit = splitInput(input); | ||
String startTime = inputAfterSplit[2]; | ||
StringBuffer endTimeCorrectFormat = new StringBuffer(startTime); // convert to StringBuffer for inserting ':' | ||
endTimeCorrectFormat.insert(2,":"); | ||
return endTimeCorrectFormat.toString(); | ||
} | ||
@Override | ||
public String toString(){ | ||
return this.taskLabel + this.mark + " " + this.description + " " + this.timeLine; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public class Task { | ||
protected String taskLabel; | ||
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 like how you have added another variable called taskLabel. This is actually clear while storing the data in it instead of explicitly typing [D] / [E] / [T] every time while printing the lists. |
||
protected String description; | ||
protected String mark; | ||
public Task(String input) { | ||
this.description = input; | ||
this.taskLabel = "To be replaced by labels"; | ||
this.mark = "[ ]"; | ||
} | ||
public String getTaskLabel() { | ||
return taskLabel; | ||
} | ||
public String getDescription() { | ||
return description; | ||
} | ||
public String getStartTime(String input){ | ||
return "To be overridden by subclass' methods"; | ||
} | ||
public String getEndTime(String input){ | ||
return "To be overridden by subclass' methods"; | ||
} | ||
public void setTaskLabel(String taskLabel) { | ||
this.taskLabel = taskLabel; | ||
} | ||
public void markTask(){ | ||
this.mark = "[X]"; | ||
} | ||
public void unmarkTask(){ | ||
this.mark = "[ ]"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class ToDos extends Task { | ||
private String taskLabel = "[T]"; | ||
public ToDos (String input){ | ||
super(input.substring(5)); // for ToDos tasks, description = input | ||
super.setTaskLabel(taskLabel); | ||
} | ||
@Override | ||
public String toString(){ | ||
return this.taskLabel + this.mark + " " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ Hello from | |
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
How may I be of service? | ||
Glad I could be of help! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
todo CS2113 assignment | ||
bye |
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.
Personally, maybe you can replace the number
9
with a constant to avoid "magic numbers"?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.
Agreed. Thanks for the idea!