-
Notifications
You must be signed in to change notification settings - Fork 76
[kevinz420] iP #62
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?
[kevinz420] iP #62
Changes from 5 commits
dc728ac
c78f17c
a0623db
da4a93a
a1fa50e
dd65e0a
5c53fad
572ea6b
616f3ec
102ab56
8019e0a
4f13039
953157e
9f1c37f
b2585d9
86eb664
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,119 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| private static Task[] tasks = new Task[100]; | ||
| private static int numTasks = 0; | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| printGreeting(); | ||
|
|
||
| Scanner in = new Scanner(System.in); | ||
| String line; | ||
| while (in.hasNextLine()) { | ||
|
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 can check "bye" condition here |
||
| line = in.nextLine(); | ||
| if (line.equals("bye")) { | ||
| break; | ||
| } | ||
|
|
||
| if (line.equals("list")) { | ||
| printTasks(); | ||
| } else { | ||
| handleCommand(line); | ||
| } | ||
|
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. What do you think about refactoring this into an "if - else if - else" statement instead of having a separate "if" statement and another "if - else" statement? |
||
| } | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
|
|
||
| private static void handleCommand(String line) { | ||
|
kvn-zhang marked this conversation as resolved.
Outdated
|
||
| int divider = line.indexOf(" "); | ||
|
|
||
| if (line.contains("mark")) { | ||
| int idx = Integer.parseInt(line.substring(divider + 1)) - 1; | ||
| if (idx < 0 || tasks[idx] == null) { | ||
| System.out.println("Sorry! That's not a valid task"); | ||
| return; | ||
| } | ||
|
|
||
| markTask(idx, line.startsWith("mark")); | ||
| return; | ||
| } | ||
| if (line.startsWith("todo")){ | ||
| addTask(new Todo(line.substring(divider + 1))); | ||
| return; | ||
| } | ||
|
|
||
| HashMap<String, String> parameters = parseParameters(line); | ||
| String description = parameters.get("description"); | ||
| if (description == null) { | ||
| System.out.println("Sorry! Please provide a valid description"); | ||
| return; | ||
| } | ||
| if (line.startsWith("deadline")) { | ||
| String by = parameters.get("by"); | ||
| if (by == null) { | ||
|
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 should consider using if-else statements instead of all if statements. Another alternative if using switch-case statements |
||
| System.out.println("Sorry! Please provide a valid `by`"); | ||
| return; | ||
| } | ||
| addTask(new Deadline(description, parameters.get("by"))); | ||
| } else if (line.startsWith("event")) { | ||
| String from = parameters.get("from"); | ||
| String to = parameters.get("to"); | ||
| if (from == null || to == null) { | ||
| System.out.println("Sorry! Please provide a valid `from` and/or `to`"); | ||
| return; | ||
| } | ||
| addTask(new Event(description, from, to)); | ||
| } | ||
| } | ||
|
|
||
| private static HashMap<String, String> parseParameters(String line) { | ||
| HashMap<String, String> fieldToValue = new HashMap<>(); | ||
|
|
||
| int startDescription = line.indexOf(" "); | ||
|
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. for consistency, can rename to startOfDescription |
||
| int endOfDescription = line.indexOf(" /"); | ||
| fieldToValue.put("description", line.substring(startDescription + 1, endOfDescription)); | ||
|
|
||
| String[] splitParams = line.split(" /"); | ||
| for (int i = 1; i < splitParams.length; i++) { | ||
| String rawParam = splitParams[i]; | ||
| int divider = rawParam.indexOf(" "); | ||
|
|
||
| String field = rawParam.substring(0, divider); | ||
| String value = rawParam.substring(divider + 1); | ||
| fieldToValue.put(field, value); | ||
|
kvn-zhang marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return fieldToValue; | ||
| } | ||
|
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 love that you factored this out into a separate method! |
||
|
|
||
| public static void markTask(int index, boolean isDone) { | ||
| tasks[index].setStatus(isDone); | ||
| if (isDone) { | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| } else { | ||
| System.out.println("OK, I've marked this task as not done yet:"); | ||
| } | ||
| System.out.println(tasks[index].getFormattedTask()); | ||
| } | ||
|
|
||
| private static void printGreeting() { | ||
| System.out.println("Hello! I'm KevBot"); | ||
| System.out.println("What can I do for you?"); | ||
| } | ||
|
|
||
| public static void printTasks() { | ||
| for (int i = 0; i < tasks.length && tasks[i] != null; i++) { | ||
| System.out.println((i + 1) + ". " + tasks[i].getFormattedTask()); | ||
| } | ||
| } | ||
|
|
||
| public static void addTask(Task task) { | ||
| if (numTasks == tasks.length) { | ||
| System.out.println("Sorry. The task list is all full"); | ||
| } | ||
|
|
||
| tasks[numTasks] = task; | ||
| numTasks++; | ||
| System.out.println("Got it. I've added this task:\n" + task.getFormattedTask() + "\nNow you have " + numTasks + " tasks in the list."); | ||
|
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. That line seems a bit long (although in the GitHub Web Interface I unfortunately can't see its exact length); what do you think about breaking it into two lines?
kvn-zhang marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.