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

CerIsaiah iP #81

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Deadline extends Task {

protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String getDescription() {
return "[D]" + super.getDescription() + " (by: " + by + ")";
}
}
125 changes: 119 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,123 @@
import java.util.ArrayList;
import java.util.Scanner;
//Refactoring methods is helpful for transforming
public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

showWelcome();

ArrayList<Task> tasks = new ArrayList<>(100);

Scanner scan = new Scanner( System.in );

boolean isEchoing = true;
while (isEchoing){
String input;
input = scan.nextLine();

if (input.equalsIgnoreCase("bye")){
isEchoing = false;
break;
}

if (input.equals("list")){
Copy link

Choose a reason for hiding this comment

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

Is there a reason why you don't use toLowerCase() for this specific command?

int numOfTasks = 0;

for (Task task: tasks){
numOfTasks++;
System.out.println(numOfTasks + ": " + task.getDescription());
}

} else if (input.toLowerCase().startsWith("mark ")){
String[] parts = input.split(" ");

int taskNum = Integer.parseInt(parts[1]);
try {
Task markedTask = tasks.get(taskNum - 1);

markedTask.isDone = true;
System.out.println("Congrats I marked this class as done : " + markedTask.getDescription());
} catch (Exception ArrayIndexOutOfBoundsException){
Copy link

Choose a reason for hiding this comment

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

should you catch the exception during the input phase? Check if 0 < user input index < num of items + 1

System.out.println("That task doesnt exist");
}

} else if (input.toLowerCase().startsWith("unmark ")) {
String[] parts = input.split(" ");

int taskNum = Integer.parseInt(parts[1]);
tasks.get(taskNum - 1).isDone = false;

try {
Task unmarkedTask = tasks.get(taskNum - 1);

unmarkedTask.isDone = false;
System.out.println("I unmarked this class as done: " + unmarkedTask.getDescription());
} catch (Exception ArrayIndexOutOfBoundsException){
System.out.println("That task doesnt exist");
}

} else if (input.toLowerCase().startsWith("deadline")){
try {
String[] toDoSplit = input.split("/");

Choose a reason for hiding this comment

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

Consider changing the variable name to a more suitable plural name.

//First part is task, and last is when by
String desc = toDoSplit[0].substring(9).trim(); // removes "deadline
Deadline deadline = new Deadline(desc, toDoSplit[1].trim());
System.out.println(deadline.getDescription());

tasks.add(deadline);

} catch (Exception ArrayIndexOutOfBoundsException){
System.out.println("Put a / after your task if you want to add a todo");
}



} else if (input.toLowerCase().startsWith("event")){
try {
String[] toDoSplit = input.split("/");
//First part is task, and last is when by
String desc = toDoSplit[0].substring(6).trim();
Event event = new Event(desc, toDoSplit[1].trim(), toDoSplit[2].trim());
System.out.println(event.getDescription());
tasks.add(event);

} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Put a / after your task if you want to add a todo");
} catch (NullPointerException e){
System.out.println("Please enter a input");
}


} else if (input.toLowerCase().startsWith("todo")) {
try {
String desc = input.substring(4).trim();
Todo todo = new Todo(desc);

System.out.println(todo.getDescription());
tasks.add(todo);

} catch (Exception e) {
Copy link

Choose a reason for hiding this comment

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

what types of errors are expected? can the exception catch be more specific?

System.out.println("An error occurred while adding the todo.");
}
} else {
// Add the task to the list
Task newTask = new Task(input);
tasks.add(newTask);
System.out.println("Added: " + input);
}






}
System.out.println("Fine. If you have no ideas imma head out");

}

private static void showWelcome() {
System.out.println("Hello my name is DUKE");
System.out.println("What can I do for you today?");
}
}
16 changes: 16 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Event extends Task{

protected String time;
String start;
String end;
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

Consider adding in access modifiers and not leave it to default.

public Event(String description, String start, String end) {
super(description);
this.start = start;
this.end = end;
}

@Override
public String getDescription() {
return "[E]" + super.getDescription() + " " + start + "-" + end ;
}
}
25 changes: 25 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Task {
String description;
protected boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}
public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

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


public String getDescription() {
return "[" + getStatusIcon() + "] " + description;
}




}
12 changes: 12 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Todo extends Task{
protected String by;
Copy link

Choose a reason for hiding this comment

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

should this variable be marked as private instead of protected? its the final class in the inheritance chain


public Todo(String description) {
super(description);
}

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