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

[Lyu Xingyu] iP #206

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9ab46e1
Level-0: initial skeletal version of the Duke that simply greets the …
Zemdalk Jan 16, 2023
dc3f098
Level-1: Greet, Echo, Exit
Zemdalk Jan 26, 2023
97423b2
Level-2: Add, List
Zemdalk Jan 26, 2023
63a191e
Level-3: Mark as Done
Zemdalk Jan 26, 2023
61029ce
A-CodingStandard
Zemdalk Jan 26, 2023
06ae0dc
Level-4: ToDos, Events, Deadlines
Zemdalk Feb 2, 2023
1fbda4e
A-TextUiTesting
Zemdalk Feb 2, 2023
203fea6
A-TextUiTesting
Zemdalk Feb 2, 2023
5f08aa5
A-CodeQuality: adjust indent and delete some useless comments
Zemdalk Feb 2, 2023
0df796d
code: follow some PRs' suggestions
Zemdalk Feb 8, 2023
3199eec
code: follow some PRs' suggestions
Zemdalk Feb 8, 2023
64bf8a0
Level-5: add exception handlers
Zemdalk Feb 9, 2023
5db98b6
Level-5: remove leading and trailing zeros in exception handling
Zemdalk Feb 9, 2023
34bb510
Merge branch 'branch-Level-5'
Zemdalk Feb 9, 2023
c7547ef
A-Packages
Zemdalk Feb 9, 2023
8e92840
Level-6: Delete
Zemdalk Feb 15, 2023
6d527d3
Level-7: Save
Zemdalk Feb 16, 2023
12d4b97
Level-7: Save
Zemdalk Feb 16, 2023
a6c842d
A-Jar
Zemdalk Feb 16, 2023
52047bb
refactor: modify code according to TA's suggetions
Zemdalk Feb 22, 2023
30ba487
file: add in
Zemdalk Feb 23, 2023
fc51ce6
A-MoreOOP
Zemdalk Feb 23, 2023
acbca13
Level-8: Dates and Times
Zemdalk Feb 23, 2023
5b66aaa
Merge pull request #4 from Zemdalk/branch-Level-8
Zemdalk Feb 23, 2023
bc9d59f
Level-9: Find
Zemdalk Feb 23, 2023
6e9ec36
Merge pull request #5 from Zemdalk/branch-Level-9
Zemdalk Feb 23, 2023
a7fded6
A-JavaDoc
Zemdalk Mar 2, 2023
7fcb84b
A-UserGuide: add a user guide
Zemdalk Mar 2, 2023
a2a863a
A-UserGuide: add a user guide
Zemdalk Mar 2, 2023
b1f0938
A-UserGuide: add a user guide
Zemdalk Mar 2, 2023
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
23 changes: 23 additions & 0 deletions src/main/java/Deadline.java
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Deadline(String description, String by) {
public getDeadline(String description, String by) {

super(description, 'D');
this.by = by;
}

@Override
public String toString() {
return super.toString() + "(by: " + by + ")";
}
}
44 changes: 38 additions & 6 deletions src/main/java/Duke.java
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"
Expand All @@ -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);

Choose a reason for hiding this comment

The 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 String but is named func

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could name object todo better, as it has the same name as the class and it might get confusing!

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));
Copy link

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/Event.java
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");

Choose a reason for hiding this comment

The 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 + ")";
}
}
13 changes: 12 additions & 1 deletion src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
public class Task {
protected String description;
protected boolean isDone;
protected char type;

public Task(String description) {
public Task(String description, char type) {
this.description = description;
this.isDone = false;
this.type = type;
}

public void setDescription(String description){
Expand All @@ -27,7 +29,16 @@ public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public char getTypeIcon(){
return type;
}

public void mark(boolean done){
isDone = done;
}

@Override
public String toString(){
return ("[" + getTypeIcon() + "] " + "[" + getStatusIcon() + "] " + getDescription());
}
}
10 changes: 10 additions & 0 deletions src/main/java/Todo.java
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');
}
}
26 changes: 12 additions & 14 deletions src/main/java/TodoList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link

@0nandon 0nandon Feb 3, 2023

Choose a reason for hiding this comment

The 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();
Expand Down