Skip to content

Commit

Permalink
Level 7 implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavzzz committed Sep 5, 2022
1 parent d1eeead commit 1bc98d4
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 68 deletions.
1 change: 1 addition & 0 deletions database/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deadline | return book | Sunday | true
4 changes: 4 additions & 0 deletions src/main/java/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum Constants {
TODO, DEADLINE, EVENT, LIST, MARK, UNMARK, DELETE, BYE

}
19 changes: 14 additions & 5 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Deadline extends Task {

protected String by;
protected String deadline;

public Deadline(String description) {
super(description.substring(9, description.indexOf('/') - 1));
this.deadline = description.substring(description.indexOf('/') + 3);
}

public Deadline(String description, String by) {
super(description);
this.by = by;
@Override
public String fileFormat() {
return String.format("deadline | %s | %s | %b", super.description, deadline, super.isDone);
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
return "[D]" + super.toString() + " (by:" + deadline + ")";
}
}
187 changes: 129 additions & 58 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;

public class Duke {

public Task[] tasks;

private static final ArrayList<Task> collection = new ArrayList<>();

public static void main(String[] args) throws DukeException {
public static final Scanner sc = new Scanner(System.in);


public static boolean isNotValid(String str) {
String[] s = str.split(" ");
return (s.length < 1);
}




public static void main(String[] args) throws DukeException, IOException, IllegalCommandException {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
Expand All @@ -15,116 +30,172 @@ public static void main(String[] args) throws DukeException {
System.out.println("Hello from\n" + logo);
System.out.println("___________________________________");
System.out.println("Hello! I'm Duke\n What can I do for you?");
System.out.println("___________________________________");
System.out.println("____________________________________");

Task[] tasks = new Task[100];
int counter = 0;
ArrayList<Task> collection = new ArrayList<>();

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

File file = new File("database/duke.txt");
if (file.createNewFile()) {
System.out.println("File has been created: " + file.getName());
} else {
System.out.println("File already exists.");
Scanner reader = new Scanner(file);
while (reader.hasNext()) {
reader.useDelimiter(" \\| ");
Constants constant = Constants.valueOf(reader.next().toUpperCase());
Task task;
switch (constant) {
case TODO:
String tdDescription = reader.next();
Todo todo = new Todo(tdDescription);
collection.add(todo);
break;
case DEADLINE:
String dlDescription = reader.next();
Deadline deadline = new Deadline(dlDescription);
collection.add(deadline);
break;
case EVENT:
String evDescription = reader.next();
Event event = new Event(evDescription);
collection.add(event);
break;

default:
throw new IllegalCommandException("Illegal command given");
}
}
}




while (!input.equals("bye")) {

if (input.equals("list")) {
System.out.println("___________________________________");
for (int i = 0; i < counter; i++) {
for (int i = 0; i < collection.size(); i++) {
System.out.println( (i+1) + "."
+ collection.get(i).toString());
}
System.out.println("___________________________________");
input = sc.next();
input = sc.nextLine();

}

else if (input.equals("mark")) {
int number = sc.nextInt();
if (input.startsWith("mark")) {
int number = Integer.parseInt(input.substring(5));
collection.get(number-1).mark();
System.out.println("___________________________________");
System.out.println("Nice! I've marked this task done: " + "\n"
+ "[" + collection.get(number-1).getStatusIcon() + "] " +
collection.get(number-1).description);
System.out.println("___________________________________");
input = sc.next();
input = sc.nextLine();
}

else if (input.equals("unmark")) {
int number = sc.nextInt();
if (input.equals("unmark")) {
int number = Integer.parseInt(input.substring(7));
collection.get(number-1).unmark();
System.out.println("___________________________________");
System.out.println("OK, I've marked this task as not done yet: " + "\n"
+ "[" + collection.get(number-1).getStatusIcon() + "] " +
collection.get(number-1).description);
System.out.println("___________________________________");
input = sc.next();
input = sc.nextLine();
}

else if (input.equals("deadline")) {
String what = sc.nextLine();
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
String byWhen = sc.nextLine();
collection.add(new Deadline(what, byWhen));
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + collection.get(counter).toString() + "\n"
+ "Now you have " + (counter+1) + " tasks in the list.");
System.out.println("___________________________________");
counter++;
input = sc.nextLine();
if (input.startsWith("deadline")) {
try {
if (isNotValid(input))
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
Deadline deadline = new Deadline(input);
collection.add(deadline);
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + deadline.toString() + "\n"
+ "Now you have " + collection.size() + " tasks in the list.");
System.out.println("___________________________________");
} catch(DukeException e) {
System.out.println(e.getMessage());

} finally {
input = sc.nextLine();
}
}

else if (input.equals("todo")) {
String what = sc.nextLine();
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
collection.add(new Todo(what));
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + collection.get(counter).toString() + "\n"
+ "Now you have " + (counter+1) + " tasks in the list.");
System.out.println("___________________________________");
counter++;
input = sc.nextLine();
if (input.startsWith("todo")) {
try {
if (isNotValid(input))
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
Todo todo = new Todo(input);
collection.add(todo);
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + todo.toString() + "\n"
+ "Now you have " + collection.size() + " tasks in the list.");
System.out.println("___________________________________");
} catch(DukeException e) {
System.out.println(e.getMessage());

} finally {
input = sc.nextLine();
}
}

else if (input.equals("event")) {
String what = sc.nextLine();
if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
String atWhen = sc.nextLine();
collection.add(new Event(what, atWhen));
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + collection.get(counter).toString() + "\n"
+ "Now you have " + (counter+1) + " tasks in the list.");
System.out.println("___________________________________");
counter++;
input = sc.nextLine();
if (input.startsWith("event")) {
try {
if (isNotValid((input)))
throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
Event event = new Event(input);
collection.add(event);
System.out.println("___________________________________");
System.out.println("Got it. I've added this task:" + "\n"
+ " " + event.toString() + "\n"
+ "Now you have " + collection.size() + " tasks in the list.");
System.out.println("___________________________________");
} catch(DukeException e) {
System.out.println(e.getMessage());

} finally {
input = sc.nextLine();
}
}

else if (input.equals("delete")) {
int number = sc.nextInt();

if (input.startsWith("delete")) {
int number = Integer.parseInt(input.substring(7));
Task temp = collection.get(number-1);
collection.remove(number-1);
counter--;
System.out.println("___________________________________");
System.out.println("Noted. I've removed this task:" + "\n"
+ " " + temp.toString() + "\n"
+ "Now you have " + counter + " tasks in the list.");
+ "Now you have " + collection.size() + " tasks in the list.");
System.out.println("___________________________________");
input = sc.next();
input = sc.nextLine();

}

else {
System.out.println("___________________________________");
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");

try {
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(.");
} catch (DukeException e) {
System.out.println(e.getMessage());
} finally {
input = sc.nextLine();
}
}
}

FileWriter fileWriter = new FileWriter(file);
for (Task task : collection) {
fileWriter.write(task.fileFormat() + System.lineSeparator());
}
fileWriter.close();

System.out.println("___________________________________");
System.out.println("Bye. Hope to see you again soon!");
System.out.println("___________________________________");
}

}
19 changes: 15 additions & 4 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Event extends Task{

protected String at;

public Event(String description, String at) {
super(description);
this.at = at;
public Event(String description) {
super(description.substring(6, description.indexOf('/') - 1));
this.at = description.substring(description.indexOf('/') + 3);
}

@Override
public String fileFormat() {
return String.format("event | %s | %s | %b", super.description, at, super.isDone);
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
return "[E]" + super.toString() + " (at:" + at + ")";
}
}
6 changes: 6 additions & 0 deletions src/main/java/IllegalCommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class IllegalCommandException extends Exception {

public IllegalCommandException(String message) {
super(message);
}
}
7 changes: 7 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.FileWriter;
import java.io.IOException;

public class Task {
protected String description;
protected boolean isDone;
Expand All @@ -19,6 +22,10 @@ public void unmark() {
this.isDone = false;
}

public String fileFormat() {
return String.format("todo | %s | %b", description, isDone);
}

@Override
public String toString() {
return "[" + this.getStatusIcon() + "] " + description;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
public class Todo extends Task {

public Todo(String description) {
super(description);
super(description.substring(5));
}

@Override
public String fileFormat() {
return String.format("todo | %s | %b", super.description, super.isDone);
}

@Override
Expand Down

0 comments on commit 1bc98d4

Please sign in to comment.