Skip to content

Commit

Permalink
Add increment level 6 and 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
hshiah committed Sep 20, 2023
1 parent 71a2724 commit 69f1c6c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ public class Deadline extends Task{

public Deadline(String description, String by) throws DukeException{
super(description);
this.by = by;
if(!by.contains("by ")){
throw new DukeException("☹ OOPS!!! The description of a deadline must contain \"by\" time.");
}else if(by.replace("by ", "").isBlank()){
throw new DukeException("☹ OOPS!!! The time of a deadline cannot be empty.");
}
this.by = by.replace("by ", "");
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}

@Override
public String toFile(){
return "D | " + super.toFile() + " | " + this.by;
}
}
97 changes: 87 additions & 10 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import java.io.FileReader;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
public class Duke {
protected final static String LINE = "_______________________________________________";
protected final static String GREET = "Hello! I'm Elwin\n" + "What can I do for you?";
Expand All @@ -14,9 +18,55 @@ private static void print(String message){
System.out.println(message);
System.out.println(LINE);
}
private static void writeToFile(List<Task> list){
try{
FileWriter fileWriter = new FileWriter("./data/duke.txt");
for(Task task : list){
fileWriter.write(task.toFile()+"\n");
}
fileWriter.flush();
fileWriter.close();
}catch(IOException e){
System.out.println("Something went wrong: " + e.getMessage());
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Task> todoList = new ArrayList<>();
List<Task> todoList = new ArrayList<Task>();
try{
FileReader fileReader = new FileReader("./data/duke.txt");
Scanner fileScanner = new Scanner(fileReader);
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
if(line.equals("\n")){
break;
}
String[] task = line.split(" \\| ");
if(task[0].equals("T")){
todoList.add(new Todo (task[2]));
}else if(task[0].equals("D")){
todoList.add(new Deadline (task[2], "by "+task[3]));
}else if(task[0].equals("E")){
todoList.add(new Event (task[2], "from "+task[3], "to "+task[4]));
}
if(task[1].equals("1")){
todoList.get(todoList.size()-1).markAsDone();
}
}
fileScanner.close();
fileReader.close();
}catch(IOException e){
try{
File file = new File("./data/duke.txt");
file.getParentFile().mkdirs();
file.createNewFile();
}catch(IOException ex){
System.out.println("☹ OOPS!!! The file has existed.");
}
}catch(DukeException e){
System.out.println("☹ OOPS!!! Wrong format in file.");
}
print(GREET);
while(true){
String input = scanner.nextLine();
Expand All @@ -32,11 +82,12 @@ public static void main(String[] args) {
}else if(input.matches("^mark \\d+$")) {
int index = Integer.parseInt(input.substring(5));
try{
todoList.get(index-1).unmarkAsDone();
todoList.get(index-1).markAsDone();
}catch(IndexOutOfBoundsException e){
print("☹ OOPS!!! The task number is not in the list.");
continue;
}
writeToFile(todoList);
print(MARK+"\n "+todoList.get(index-1));
}else if(input.matches("^unmark \\d+$")) {
int index = Integer.parseInt(input.substring(7));
Expand All @@ -46,37 +97,63 @@ public static void main(String[] args) {
print("☹ OOPS!!! The task number is not in the list.");
continue;
}
writeToFile(todoList);
print(UNMARK+"\n "+todoList.get(index-1));
}else if(input.split(" ")[0].equals("todo")){
Todo todo;
try{
todo = new Todo(input.substring(4, input.length()));
todo = new Todo(input.substring(5, input.length()));
}catch(DukeException e){
continue;
}catch(StringIndexOutOfBoundsException e){
print("☹ OOPS!!! The description of a todo cannot be empty.");
continue;
}
todoList.add(todo);
writeToFile(todoList);
print("Got it. I've added this task:\n "+todo+"\nNow you have " + todoList.size() + " tasks in the list.");
}else if(input.split(" ")[0].equals("deadline")){
Deadline deadline;
try {
deadline = new Deadline(input.split("/")[0].substring(9, input.split("/")[0].length() - 1), input.split("/")[1].replace("by ", ""));
deadline = new Deadline(input.split(" /")[0].substring(9), input.split(" /")[1]);
}catch (DukeException e){
continue;
}catch (ArrayIndexOutOfBoundsException e){
print("☹ OOPS!!! The description of a deadline should use / mark time.");
continue;
}catch(StringIndexOutOfBoundsException e){
print("☹ OOPS!!! The description of a deadline cannot be empty.");
continue;
}
todoList.add(deadline);
writeToFile(todoList);
print("Got it. I've added this task:\n "+deadline+"\nNow you have " + todoList.size() + " tasks in the list.");
}else if(input.split(" ")[0].equals("event")){
}else if(input.split(" ")[0].equals("event")) {
Event event;
try{
event = new Event(input.split("/")[0].substring(5, input.split("/")[0].length()-1), input.split("/")[1], input.split("/")[2]);
}catch(DukeException e){
try {
event = new Event(input.split(" /")[0].substring(6), input.split(" /")[1], input.split(" /")[2]);
}catch(DukeException e) {
continue;
}catch(ArrayIndexOutOfBoundsException e) {
print("☹ OOPS!!! The description of a event should use / mark start time and end time.");
continue;
}catch(StringIndexOutOfBoundsException e){
print("☹ OOPS!!! The description of a event should use \\ mark start time and end time.");
print("☹ OOPS!!! The description of a event cannot be empty.");
continue;
}
writeToFile(todoList);
todoList.add(event);
print("Got it. I've added this task:\n "+event+"\nNow you have " + todoList.size() + " tasks in the list.");
print("Got it. I've added this task:\n " + event + "\nNow you have " + todoList.size() + " tasks in the list.");
}else if(input.split(" ")[0].equals("delete")){
int index = Integer.parseInt(input.substring(7));
try{
Task task = todoList.get(index-1);
todoList.remove(index-1);
writeToFile(todoList);
print("Noted. I've removed this task:\n "+task+"\nNow you have " + todoList.size() + " tasks in the list.");
}catch(IndexOutOfBoundsException e){
print("☹ OOPS!!! The task number is not in the list.");
}
}else{
print("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public class DukeException extends Exception{
protected String line = "_______________________________________________";
protected String LINE = "_______________________________________________";
public DukeException(String message){
super(message);
System.out.println(line);
System.out.println(LINE);
System.out.println(message);
System.out.println(line);
System.out.println(LINE);
}
}
14 changes: 12 additions & 2 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ public Event(String description, String from, String by) throws DukeException{
throw new DukeException("☹ OOPS!!! The description of a event must contain \"from\" time.");
}else if(!by.contains("to ")){
throw new DukeException("☹ OOPS!!! The description of a event must contain \"to\" time.");
}else if(from.replace("from ", "").isBlank()){
throw new DukeException("☹ OOPS!!! The start time of a event cannot be empty.");
}else if(by.replace("to ", "").isBlank()){
throw new DukeException("☹ OOPS!!! The end time of a event cannot be empty.");
}
this.from = from;
this.by = by;
this.from = from.replace("from ", "");
this.by = by.replace("to ", "");
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.by + ")";
}


@Override
public String toFile(){
return "E | " + super.toFile() + " | " + this.from + " | " + this.by;
}
}
8 changes: 6 additions & 2 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Task(String description) throws DukeException {
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
return (isDone ? "X" : " ");
}

public void markAsDone() {
Expand All @@ -24,6 +24,10 @@ public void unmarkAsDone(){

@Override
public String toString() {
return "[" + getStatusIcon() + "]" + this.description;
return "[" + getStatusIcon() + "] " + this.description;
}

public String toFile(){
return (isDone ? "1" : "0") + " | " + this.description;
}
}
5 changes: 5 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public Todo(String description) throws DukeException{
public String toString() {
return "[T]" + super.toString();
}

@Override
public String toFile(){
return "T | " + super.toFile();
}
}

0 comments on commit 69f1c6c

Please sign in to comment.