-
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
[Lyu Xingyu] iP #206
base: master
Are you sure you want to change the base?
[Lyu Xingyu] iP #206
Changes from 1 commit
9ab46e1
dc3f098
97423b2
63a191e
61029ce
06ae0dc
1fbda4e
203fea6
5f08aa5
0df796d
3199eec
64bf8a0
5db98b6
34bb510
c7547ef
8e92840
6d527d3
12d4b97
a6c842d
52047bb
30ba487
fc51ce6
acbca13
5b66aaa
bc9d59f
6e9ec36
a7fded6
7fcb84b
a2a863a
b1f0938
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 Deadline extends Task { | ||
|
||
protected String by; | ||
|
||
public static Deadline toDeadline(String instruction){ | ||
int contentIdx = instruction.indexOf("/by"); | ||
|
||
if (contentIdx == -1) return null; | ||
String deadlineContent = instruction.substring(0, contentIdx); | ||
String deadlineBy = instruction.substring(contentIdx + "/by ".length()); | ||
return new Deadline(deadlineContent, deadlineBy); | ||
} | ||
|
||
public Deadline(String description, String by) { | ||
super(description, 'D'); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString() + "(by: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void printError(String errMsg){ | ||
String printContent = " ____________________________________________________________\n" | ||
+ " " + "ERROR: " + errMsg + "!\n" | ||
+ " ____________________________________________________________\n"; | ||
System.out.println(printContent); | ||
} | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
|
@@ -25,11 +31,20 @@ public static void main(String[] args) { | |
System.out.println(exitPrompt); | ||
break; | ||
}else{ | ||
// parse input | ||
int funcIdx = line.indexOf(" "); | ||
if(funcIdx == -1){ | ||
funcIdx = line.length(); | ||
} | ||
String func = line.substring(0, funcIdx); | ||
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. perhaps you could give this variable a better name, as it is stored as a 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 agree with @lihka1202 . I'd suggest to give a variable name like commandIndex or inputIndex as it it a bit more clear for readers to understand what you are referring to. 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. Thank you for your good suggestions! I have made corrections. |
||
String instruction; | ||
if(funcIdx == line.length()){ | ||
instruction = ""; | ||
}else{ | ||
instruction = line.substring(funcIdx+1); | ||
} | ||
|
||
// do instructions | ||
if(line.equals("list")){ | ||
// show to-do list | ||
todoList.showList(); | ||
|
@@ -51,14 +66,31 @@ public static void main(String[] args) { | |
index = -1; | ||
} | ||
todoList.markItem(index, false); | ||
}else if(func.equals("todo")){ | ||
Todo todo = Todo.toTodo(instruction); | ||
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. Perhaps you could name object |
||
if(todo == null){ | ||
printError("Wrong todo format"); | ||
continue; | ||
} | ||
todoList.addItem(todo); | ||
}else if(func.equals("deadline")){ | ||
Deadline deadline = Deadline.toDeadline(instruction); | ||
if(deadline == null){ | ||
printError("Wrong deadline format"); | ||
continue; | ||
} | ||
todoList.addItem(deadline); | ||
}else if(func.equals("event")){ | ||
Event event = Event.toEvent(instruction); | ||
if(event == null){ | ||
printError("Wrong event format"); | ||
continue; | ||
} | ||
todoList.addItem(Event.toEvent(instruction)); | ||
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. 'switch' statement would be better since there are many cases you want to separate. 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 deep nesting in if-else statement while every if statements inside are conducting similar calculation. There would be other way to better construct this code like declaring special function for those calculation. |
||
}else{ | ||
// add item to to-do list | ||
todoList.addItem(line); | ||
// }else{ | ||
// String repeatInput = " ____________________________________________________________\n" | ||
// + " " + line + "\n" | ||
// + " ____________________________________________________________\n"; | ||
// System.out.println(repeatInput); | ||
Task todo = new Todo(line); | ||
todoList.addItem(todo); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Event extends Task { | ||
protected String from; | ||
protected String to; | ||
|
||
public static Event toEvent(String instruction){ | ||
int contentIdx = instruction.indexOf("/from"); | ||
int fromIdx = instruction.indexOf("/to"); | ||
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. Perhaps u could use Index instead of Idx |
||
|
||
if(contentIdx == -1 || fromIdx == -1) return null; | ||
|
||
String eventContent = instruction.substring(0, contentIdx); | ||
String eventFrom = instruction.substring(contentIdx + "/from ".length(), fromIdx); | ||
String eventTo = instruction.substring(fromIdx + "/to ".length()); | ||
return new Event(eventContent, eventFrom, eventTo); | ||
} | ||
|
||
public Event(String description, String from, String to){ | ||
super(description, 'E'); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString() + "(from: " + from + "to: " + to + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class Todo extends Task { | ||
|
||
public static Todo toTodo(String instruction){ | ||
return new Todo(instruction); | ||
} | ||
|
||
public Todo(String description){ | ||
super(description, 'T'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,51 +12,49 @@ public int getListnum(){ | |
return listnum; | ||
} | ||
|
||
public int addItem(String line){ | ||
public int addItem(Task task){ | ||
if(listnum < MAXLISTNUM){ | ||
// todoList[listnum++] = line; | ||
tasks[listnum++] = new Task(line); | ||
tasks[listnum++] = task; | ||
System.out.println(SPLITTER); | ||
System.out.println(" added: " + line); | ||
System.out.println(" " + "Got it. I've added this task:"); | ||
System.out.println(" " + task); | ||
System.out.println(" " + "Now you have " + listnum + " task" + ((listnum>1) ? "s" : "") + " in the list."); | ||
System.out.println(SPLITTER); | ||
System.out.println(); | ||
return 1; | ||
}else{ | ||
System.out.println(" ERROR: List overflow!"); | ||
System.out.println(); | ||
Duke.printError("List overflow"); | ||
return 0; | ||
} | ||
} | ||
|
||
public int markItem(int num, boolean mark){ | ||
System.out.println(SPLITTER); | ||
if(num < listnum && num >= 0){ | ||
// marks[num] = mark; | ||
tasks[num].mark(mark); | ||
if(mark){ | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
System.out.println(SPLITTER); | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
System.out.print(" [X] "); | ||
}else{ | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
System.out.println(SPLITTER); | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
System.out.print(" [ ] "); | ||
} | ||
System.out.println(tasks[num].getDescription()); | ||
System.out.println(SPLITTER); | ||
System.out.println(); | ||
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. So many printings, I think in if-else statement. How about just making additional function for those printing stacks? |
||
return 1; | ||
}else{ | ||
System.out.println(" ERROR: Index wrong!"); | ||
System.out.println(SPLITTER); | ||
System.out.println(); | ||
Duke.printError("Wrong index"); | ||
return 0; | ||
} | ||
} | ||
|
||
public void showList(){ | ||
System.out.println(SPLITTER); | ||
for(int i = 0; i < listnum; i++){ | ||
System.out.println(" " + (i + 1) + ". [" | ||
+ tasks[i].getStatusIcon() + "] " + tasks[i].getDescription()); | ||
System.out.println(" " + (i + 1) + ". " + tasks[i]); | ||
} | ||
System.out.println(SPLITTER); | ||
System.out.println(); | ||
|
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.