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

[kevinz420] iP #62

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

140 changes: 140 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package duke;

import duke.command.DukeException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.Todo;

import java.util.HashMap;
import java.util.Scanner;

kevinz420 marked this conversation as resolved.
Show resolved Hide resolved
public class Duke {
private static Task[] tasks = new Task[100];
private static int numTasks = 0;
public static void main(String[] args) {
printGreeting();

Scanner in = new Scanner(System.in);
String line;
while (in.hasNextLine()) {
line = in.nextLine();
if (line.equals("bye")) {
break;
}

if (line.equals("list")) {
printTasks();
} else {
try {
handleCommand(line);
} catch (DukeException e) {
System.out.println(e.getMessage());
}
}
}
System.out.println("Bye. Hope to see you again soon!");
}

private static void handleCommand(String line) throws DukeException {
int divider = line.indexOf(" ");
if (divider == -1) {
throw new DukeException("Sorry! Not sure what you mean");
}

if (line.contains("mark")) {
int idx = Integer.parseInt(line.substring(divider + 1)) - 1;
if (idx < 0 || tasks[idx] == null) {
throw new DukeException("Sorry! That's not a valid task");
}

markTask(idx, line.startsWith("mark"));
return;
}
if (line.startsWith("todo")){
String description = line.substring(divider + 1);

addTask(new Todo(description));
return;
}

HashMap<String, String> parameters = parseParameters(line);
String description = parameters.get("description");
if (description == null) {
throw new DukeException("Sorry! Please provide a valid description");
}
if (line.startsWith("deadline")) {
String by = parameters.get("by");
if (by == null) {
throw new DukeException("Sorry! Please provide a valid `by`");
}
addTask(new Deadline(description, by));
} else if (line.startsWith("event")) {
String from = parameters.get("from");
String to = parameters.get("to");
if (from == null || to == null) {
throw new DukeException("Sorry! Please provide a valid `from` and/or `to`");
}
addTask(new Event(description, from, to));
} else {
throw new DukeException("Sorry! Please enter a valid command");
}
}

private static HashMap<String, String> parseParameters(String line) throws DukeException {
HashMap<String, String> fieldToValue = new HashMap<>();

int startDescription = line.indexOf(" ");
int endOfDescription = line.indexOf(" /");
if (startDescription == -1 || endOfDescription == -1) {
throw new DukeException("Sorry! Not sure what you mean");
}
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(" ");
if (divider == -1) {
throw new DukeException("Sorry! Please enter valid inputs");
}

String field = rawParam.substring(0, divider);
String value = rawParam.substring(divider + 1);
fieldToValue.put(field, value);
}

return fieldToValue;
}

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) throws DukeException {
if (numTasks == tasks.length) {
throw new DukeException("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.");
}
}
8 changes: 8 additions & 0 deletions src/main/java/duke/command/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package duke.command;

public class DukeException extends Exception {

public DukeException(String message) {
super(message);
}
}
14 changes: 14 additions & 0 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package duke.task;

public class Deadline extends Task {
private String by;
public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String getFormattedTask() {
return "[D] " + super.getFormattedTask() + " (by: " + by + ")";
}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package duke.task;

public class Event extends Task {
private String from;
private String to;
public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}

@Override
public String getFormattedTask() {
return "[E] " + super.getFormattedTask() + " (from: " + from + " to: " + to + ")";
}
}
22 changes: 22 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package duke.task;

public class Task {
protected String description;
protected boolean isDone;

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

public String getFormattedTask() {
return "[" + getStatusIcon() + "] " + description;
}
public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public void setStatus(boolean isDone) {
this.isDone = isDone;
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke.task;

public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String getFormattedTask() {
return "[T] " + super.getFormattedTask();
}
}