-
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
[LimHongYao] iP #201
base: master
Are you sure you want to change the base?
[LimHongYao] iP #201
Changes from 4 commits
a63c9f7
ef8d231
bdd7c9d
9d94709
a3a4347
c711aeb
28406be
c2a4627
6cf1e94
e3a2df5
e0bf091
e3091cd
896ad76
44155f7
95409e7
fd05d8e
6e7bbe3
bc51fbb
81192bc
0021e8a
4984f8c
61ec054
39fb97b
30d3513
86c9546
0200dab
7ba7c8e
ca442dc
3fff458
488a811
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 |
---|---|---|
@@ -1,10 +1,75 @@ | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void exitMessage() { | ||
System.out.println("Go away Anna"); | ||
System.out.println("O-kay bye......"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("Hi it's Anna!\nWhat do you need to do?"); | ||
Scanner in = new Scanner(System.in); | ||
|
||
boolean exit = false; | ||
while (!exit) { | ||
String input = (in.nextLine()).trim(); | ||
String inputCMD, inputItem; | ||
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. Can I check what does CMD mean? If it stands for command. I think it's better to just name it as inputCommand for readibilty. |
||
if (input.contains(" ")) { | ||
inputCMD = input.split(" ", 2)[0]; | ||
inputItem = input.split(" ", 2)[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. The numbers like 2, 0, 1 might be considered magic numbers that hinder readability. |
||
} else { | ||
inputCMD = input; | ||
inputItem = null; | ||
} | ||
|
||
switch (inputCMD) { | ||
case "bye": | ||
exitMessage(); | ||
exit = true; | ||
break; | ||
case "list": | ||
if (ToDoList.getNumItems() == 0) { | ||
System.out.println("We are free! Let's go play!"); | ||
} else { | ||
System.out.println("Here's what we've gotta do:"); | ||
ToDoList.viewList(); | ||
} | ||
break; | ||
case "mark": | ||
if (inputItem == null) { | ||
System.out.println("What should I mark?"); | ||
ToDoList.viewList(); | ||
inputItem = in.nextLine().trim(); | ||
} | ||
|
||
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. Do avoid leaving the space between each |
||
ToDoList.markDone(Integer.parseInt(inputItem)-1); | ||
System.out.println("Okay I've marked item " + inputItem + " as done:"); | ||
ToDoList.printItem(Integer.parseInt(inputItem)-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. I like the single level of abstraction applied here. Makes things easy to follow |
||
break; | ||
|
||
case "unmark": | ||
if (inputItem == null) { | ||
System.out.println("What should I unmark?"); | ||
//TODO: add in handling if input is "unmark 2" again | ||
ToDoList.viewList(); | ||
inputItem = in.nextLine().trim(); | ||
} | ||
|
||
ToDoList.markNotDone(Integer.parseInt(inputItem)-1); | ||
System.out.println("Oh no! Are we not done with " + inputItem + " after all?"); | ||
ToDoList.printItem(Integer.parseInt(inputItem)-1); | ||
break; | ||
case "add": | ||
ToDoList.addItem(inputItem); | ||
break; | ||
default: | ||
System.out.println("I didn't get that!"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { //ok to leave as public? | ||
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. Yes you need to leave it as public. But do remove such comments from your code as it goes against the java coding standard. 👍 |
||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public void markDone() { | ||
isDone = true; | ||
} | ||
|
||
public void markNotDone() { | ||
isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public String getTask() { | ||
String Task = "["; | ||
return Task.concat(getStatusIcon() + "] " + getDescription()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.ArrayList; | ||
|
||
public class ToDoList { | ||
private static final ArrayList<Task> TaskList = new ArrayList<>(10); | ||
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. Maybe V\variable names need to be in camelCase. 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. Maybe variable names need to be in camelCase. 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. Maybe the size of the ArrayList should be a magic number. |
||
private static int NumTasks = 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. Maybe variable names must be in camelCase, same problem. |
||
public static void addItem (String in) { | ||
Task task = new Task(in); | ||
|
||
TaskList.add(task); | ||
NumTasks += 1; | ||
System.out.println("added: " + in); | ||
} | ||
public static int getNumItems() { | ||
return NumTasks; | ||
} | ||
public static void viewList () { | ||
for (int i = 0; i < TaskList.size(); ++i) { | ||
System.out.print(i+1 + ". "); | ||
|
||
System.out.println(TaskList.get(i).getTask()); | ||
} | ||
} | ||
public static void markDone (int index) { | ||
if (TaskList.get(index).getStatusIcon().equals(" ")) { | ||
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. Is this if statement necessary? I think markDone would change the status icon to "X" independent of what is originally is |
||
TaskList.get(index).markDone(); | ||
} | ||
} | ||
public static void markNotDone (int index) { | ||
if (TaskList.get(index).getStatusIcon().equals("X")) { | ||
TaskList.get(index).markNotDone(); | ||
} | ||
} | ||
public static Task getItem (int index) { | ||
return TaskList.get(index); | ||
} | ||
public static void printItem (int index) { | ||
System.out.print(index+1 + ". "); | ||
System.out.println(TaskList.get(index).getTask()); | ||
} | ||
|
||
|
||
} |
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.
Preferably name boolean in such a way that implies it is boolean.
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.
Maybe use isExit to replace exit can imply a boolean