Skip to content

Commit

Permalink
Level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
de-yi committed Feb 28, 2023
1 parent 11e0fdd commit 67d4f0e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class Deadline extends Task {
public String toString() {
String returnStr = "[D]";
if (getIsDone()) {
returnStr = returnStr.concat("[O]");
returnStr = returnStr.concat("[O] ");
} else {
returnStr = returnStr.concat("[ ]");
returnStr = returnStr.concat("[ ] ");
}

return returnStr + getContents() + "(by: " + by + ")";
return returnStr + getContents() + "/by: " + by;
}
}
3 changes: 2 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Duke {
public static final String COMMAND_UNMARK = "unmark";
public static final String COMMAND_DELETE = "delete";

private static TaskList TASKLIST = new TaskList();
private static TaskList TASKLIST;

public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -19,6 +19,7 @@ public static void main(String[] args) {
System.out.println("Hello from\n" + logo);

greetUser();
TASKLIST = new TaskList();

while(true){
executeCommand(getUserCommand());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class Event extends Task {
public String toString() {
String returnStr = "[E]";
if (getIsDone()) {
returnStr = returnStr.concat("[O]");
returnStr = returnStr.concat("[O] ");
} else {
returnStr = returnStr.concat("[ ]");
returnStr = returnStr.concat("[ ] ");
}

return returnStr + getContents() + "(from: " + from + " | to: " + to + ")";
return returnStr + getContents() + "/from: " + from + "/to: " + to;
}
}
52 changes: 44 additions & 8 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.IOException;

public class TaskList {
public final static String FILEPATH = "duke.txt";
public final static String FILEPATH = "./duke.txt";
private ArrayList<Task> taskArray = new ArrayList<>();
private int totalTaskNum = 0;

public TaskList(){
try {
File file = new File(FILEPATH);
if (file.createNewFile()) {
System.out.println("I created the file " + file.getName());
System.out.println("From now on, I will record your tasks here.");
} else{
Scanner scanner = new Scanner(file);
ArrayList<String[]> existingTasks = new ArrayList<>();
while(scanner.hasNext()){
String task = scanner.nextLine().substring(8);
String[] taskInfo = task.split("/");
existingTasks.add(taskInfo);
}
loadData(existingTasks);
}
} catch(IOException e){
System.out.println("☹ OOPS!!! I cannot create new file.");
System.out.println(e.getMessage());
}
}

public boolean addTask(String userInput){
String[] userInputSplited = userInput.split("/");
String[] userCommand = userInputSplited[0].split(" ");
Expand Down Expand Up @@ -40,7 +63,7 @@ public boolean addTodo(String[] userInputSplited){
public boolean addDeadline(String[] userInputSplited){
try{
String contents = userInputSplited[0].replace("deadline ", "");
String end = userInputSplited[1].replace("/by ", "");
String end = userInputSplited[1].replace("by: ", "");
Deadline newDeadline = new Deadline (contents, end);
taskArray.add(newDeadline);
totalTaskNum++;
Expand All @@ -55,8 +78,8 @@ public boolean addDeadline(String[] userInputSplited){
public boolean addEvent(String[] userInputSplited){
try{
String contents = userInputSplited[0].replace("event ", "");
String start = userInputSplited[1].replace("/from ", "");
String end = userInputSplited[2].replace("/to ", "");
String start = userInputSplited[1].replace("from: ", "");
String end = userInputSplited[2].replace("to: ", "");
Event newEvent = new Event(contents, start, end);
taskArray.add(newEvent);
totalTaskNum++;
Expand All @@ -82,10 +105,7 @@ public boolean unmarkTask(int taskNumInt){

public static void writeToFile(String filePath, String textAdded){
try {
File file = new File(filePath);
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
}
File file = new File(FILEPATH);
FileWriter fw = new FileWriter(file);
fw.write(textAdded);
fw.close();
Expand All @@ -107,6 +127,22 @@ public boolean delete(int taskNumInt){
}
}

public void loadData(ArrayList<String[]> existingTasks){
for(String[] taskInfo : existingTasks){
switch(taskInfo.length){
case 1:
addTodo(taskInfo);
break;
case 2:
addDeadline(taskInfo);
break;
case 3:
addEvent(taskInfo);
break;
}
}
}


@Override
public String toString(){
Expand Down

0 comments on commit 67d4f0e

Please sign in to comment.