-
Notifications
You must be signed in to change notification settings - Fork 197
[LimHongYao] iP #201
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
base: master
Are you sure you want to change the base?
[LimHongYao] iP #201
Changes from 8 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class Deadline extends Task { | ||
| protected String dueDate; | ||
|
|
||
| public Deadline(String description, String dueDate) { | ||
| super(description); | ||
| this.dueDate = dueDate; | ||
| } | ||
| @Override | ||
| public String getTypeIcon() { | ||
| return "D"; | ||
| } | ||
|
|
||
| public String getDueDate() { | ||
| return dueDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String getTask() { | ||
| return taskTypeIcon() + isDoneIcon() + getDescription() | ||
| + " (by: " + getDueDate() + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,143 @@ | ||
| import java.lang.reflect.Array; | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| static final int COMMAND_INDEX = 0; | ||
| static final int MAX_COMMAND_LENGTH = 1; | ||
| static final int DESCRIPTION_INDEX = 1; | ||
| static final int STARTDATE_INDEX = 0; | ||
| static final int ENDDATE_INDEX = 1; | ||
|
|
||
| public static void exitMessage() { | ||
| System.out.println("Go away Anna"); | ||
| System.out.println("O-kay bye......"); | ||
| } | ||
|
|
||
| public static String getItemDescription(String userInput) { | ||
| Scanner in = new Scanner(System.in); | ||
| String description; | ||
| try { | ||
| description = userInput.split(" ", 2)[DESCRIPTION_INDEX]; | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| System.out.println("What are you referring to?"); | ||
| description = in.nextLine().trim(); | ||
| } | ||
| return description; | ||
| } | ||
|
|
||
| public static String getDueDate(String userInput) { | ||
| Scanner in = new Scanner(System.in); | ||
| String dueDate; | ||
| if (userInput.contains("/by")) { | ||
| dueDate = userInput.substring(userInput.indexOf("/by")); | ||
| } else { | ||
| System.out.println("When is this due by?"); | ||
| dueDate = in.nextLine().trim(); | ||
| } | ||
| return dueDate; | ||
| } | ||
|
|
||
| public static String[] getStartEndDates(String userInput) { | ||
| Scanner in = new Scanner(System.in); | ||
| String[] StartEndDates = new String[2]; | ||
|
|
||
| if (userInput.contains("/from")) { | ||
| StartEndDates[STARTDATE_INDEX] = userInput.substring(userInput.indexOf("/from"),userInput.indexOf("/to")).trim(); | ||
| } else { | ||
| System.out.println("When does this event start?"); | ||
| StartEndDates[STARTDATE_INDEX] = in.nextLine().trim(); | ||
| } | ||
|
|
||
| if (userInput.contains("/to")) { | ||
| StartEndDates[ENDDATE_INDEX] = userInput.substring(userInput.indexOf("/to")).trim(); | ||
| } else { | ||
| System.out.println("When does this event end?"); | ||
| StartEndDates[ENDDATE_INDEX] = in.nextLine().trim(); | ||
| } | ||
| return StartEndDates; | ||
| } | ||
| 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); | ||
|
|
||
| while (true) { | ||
| String userInput = in.nextLine().trim(); | ||
| ArrayList <String> input = new ArrayList<>(); | ||
| input.add(COMMAND_INDEX, userInput.split(" ", 2)[COMMAND_INDEX]); | ||
| String inputCommand = input.get(COMMAND_INDEX); | ||
|
|
||
| switch (inputCommand) { | ||
| case "bye": | ||
| exitMessage(); | ||
| return; | ||
| case "list": | ||
| if (TaskList.getNumItems() == 0) { | ||
| System.out.println("We are free! Let's go play!"); | ||
| } else { | ||
| System.out.println("Here's what we've gotta do:"); | ||
| TaskList.viewList(); | ||
| } | ||
| break; | ||
|
|
||
|
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 |
||
| case "mark": { | ||
| String itemNum = getItemDescription(userInput); | ||
|
|
||
| TaskList.markDone(Integer.parseInt(itemNum) - 1); | ||
| System.out.println("Okay I've marked item " + itemNum + " as done:"); | ||
| TaskList.printItem(Integer.parseInt(itemNum) - 1); | ||
| break; | ||
| } | ||
|
|
||
| case "unmark": { | ||
| String itemNum = getItemDescription(userInput); | ||
|
|
||
| TaskList.markNotDone(Integer.parseInt(itemNum) - 1); | ||
| System.out.println("Oh no! Are we not done with " + itemNum + " after all?"); | ||
| TaskList.printItem(Integer.parseInt(itemNum) - 1); | ||
| break; | ||
| } | ||
|
|
||
| case "add": { | ||
| String itemDescription = getItemDescription(userInput); | ||
| Task newTask = new Task(itemDescription); | ||
| TaskList.addItem(newTask); | ||
| break; | ||
| } | ||
|
|
||
| case "todo": { | ||
| String itemDescription = getItemDescription(userInput); | ||
| Todo newTask = new Todo(itemDescription); | ||
| TaskList.addItem(newTask); | ||
| break; | ||
| } | ||
|
|
||
| case "deadline": { | ||
| String itemDescription = getItemDescription(userInput); | ||
| String dueDate = getDueDate(userInput); | ||
| Deadline newTask = new Deadline(itemDescription,dueDate); | ||
| TaskList.addItem(newTask); | ||
| break; | ||
| } | ||
|
|
||
| case "event": | ||
| String itemDescription = getItemDescription(userInput); | ||
| String[] StartEndDates = getStartEndDates(userInput); | ||
| String startDate = StartEndDates[STARTDATE_INDEX]; | ||
| String endDate = StartEndDates[ENDDATE_INDEX]; | ||
| Event newTask = new Event(itemDescription,startDate,endDate); | ||
| TaskList.addItem(newTask); | ||
| break; | ||
|
|
||
| default: | ||
| System.out.println("I didn't get that!"); | ||
| break; | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| public class Event extends Task{ | ||
| String startDate, endDate; | ||
| public Event(String description, String startDate, String endDate) { | ||
| super(description); | ||
| this.startDate = startDate; | ||
| this.endDate = endDate; | ||
| } | ||
| @Override | ||
| public String getTypeIcon() { | ||
| return "E"; | ||
| } | ||
|
|
||
| public String getStartDate() { | ||
| return startDate; | ||
| } | ||
|
|
||
| public String getEndDate() { | ||
| return endDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String getTask() { | ||
| return taskTypeIcon() + isDoneIcon() + getDescription() + System.lineSeparator() | ||
| + "Start: " + getStartDate() + System.lineSeparator() | ||
| + "End: " + getEndDate(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
|
|
||
| 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 String getTypeIcon() { | ||
| return "NULL"; | ||
| } | ||
| public String taskTypeIcon() { return "[" + getTypeIcon() + "]";} | ||
|
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. Even though these are simple 1 liners, please do still follow the proper style (aka Egyptian Style). |
||
| public void setDescription(String description) { | ||
| this.description = description; | ||
| } | ||
|
|
||
| public void markDone() { | ||
| isDone = true; | ||
| } | ||
|
|
||
| public void markNotDone() { | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String isDoneIcon() { return "[" + getStatusIcon() + "]";} | ||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public String getTask() { | ||
| return taskTypeIcon() + isDoneIcon() + " " + getDescription(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class TaskList { | ||
| 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. ArrayList is a type of dynamic array and there is no need to limit your |
||
| private static int NumTasks = 0; | ||
| public static void addItem (Task newTask) { | ||
| TaskList.add(newTask); | ||
| NumTasks += 1; | ||
| System.out.println("Okay! I've added: [" + newTask.getTypeIcon() +"] " + newTask.getDescription()); | ||
| } | ||
| 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) { | ||
|
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 should not be a space between |
||
| if (TaskList.get(index).getStatusIcon().equals(" ")) { | ||
| 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()); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class Todo extends Task { | ||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String getTypeIcon() { | ||
| return "T"; | ||
| } | ||
| } |
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.
Good job using constants for your numbers! You could extend that to magic strings as well!