Skip to content

Commit

Permalink
Added Level 7. Save
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang-Moyan committed Sep 4, 2023
1 parent 5bb12d4 commit 3df8678
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 10 deletions.
170 changes: 162 additions & 8 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import HelperClass.Task;

public class Duke {
Expand All @@ -20,7 +28,7 @@ public static void Exit() {

}

private static String getUserTaskName() {
private static String GetUserTaskName() {
Scanner getUserInput = new Scanner(System.in);
String taskName = getUserInput.nextLine();
if (taskName.isEmpty()) {
Expand All @@ -30,6 +38,144 @@ private static String getUserTaskName() {
return taskName;
}

}

private static void BackgroundSetUp() {
String directoryName = "data";
String fileName = "list.txt";

File dir = new File(directoryName);
if (!(dir.exists())) {
if (dir.mkdir()) {
System.out.println("Directory '" + directoryName + "' created.");
} else {
System.err.println("Failed to create directory '" + directoryName + "'.");
return;
}
}


File file = new File(dir, fileName);

if (!(file.exists())) {
try {
if (file.createNewFile()) {
System.out.println("File '" + fileName + "' created in directory '" + directoryName + "'.");
} else {
System.err.println("Failed to create file '" + fileName + "' in directory '" + directoryName + "'.");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}


private static List<String> ReadLine(String line) {
List<String> formattedLine = new ArrayList<>();
Scanner lineScanner = new Scanner(line);
while (lineScanner.hasNext()) {

String token = lineScanner.next();
formattedLine.add(token);

}
lineScanner.close();

return formattedLine;
}



private static Task[] LoadList() {
Task[] userList = new Task[100];
int positionPointer = 0;

String fileName = "data/list.txt";
Path path = Paths.get(fileName);
try {
Scanner fileScanner = new Scanner(path);
while(fileScanner.hasNextLine()){

// Record format: "Type | Status | Name | Time"
// example: "D | 0 | return book | June 6th"
// "0" for not done and "1" for done

String line = fileScanner.nextLine();

List<String> formattedLine = ReadLine(line);



List<String> attributes = new ArrayList<>();
String attributeName = "";

for (Object element : formattedLine) {
if (element.equals("|")) {
attributes.add(attributeName);
attributeName = "";
} else {
attributeName = attributeName + element + " ";

}
}

attributes.add(attributeName);
boolean isDone = attributes.get(1).equals("1");

switch (attributes.get(0)) {
case "T": {
Task task = new Task(attributes.get(2), 1, "Null", isDone);
userList[positionPointer] = task;

break;
}
case "D": {
Task task = new Task(attributes.get(2), 2, attributes.get(3), isDone);
userList[positionPointer] = task;

break;
}
case "E": {
Task task = new Task(attributes.get(2), 3, attributes.get(3), isDone);
userList[positionPointer] = task;

break;


}
default:
throw new IllegalStateException("Unexpected value: " + attributes.get(0));
}

positionPointer++;


}
fileScanner.close();

} catch (IOException e) {
throw new RuntimeException(e);
}


return userList;
}

private static void SaveList(Task[] userList, int numberOfElements) {
String fileName = "data/list.txt";
try (FileWriter writer = new FileWriter(fileName)) {
for (int i = 0; i < numberOfElements; i++) {
writer.write(userList[i].ForRecordingInTextFile());
writer.write("\n");
}

} catch (IOException e) {
throw new RuntimeException(e);
}



}


Expand All @@ -41,13 +187,15 @@ public static void main(String[] args) {
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

BackgroundSetUp();


Greet();

boolean wantToExit = false;
Scanner getUserInput = new Scanner(System.in);
Scanner getUserIndex = new Scanner(System.in);
Task[] userList = new Task[100];
Task[] userList = LoadList();

int listPointer = 0;

Expand All @@ -59,7 +207,10 @@ public static void main(String[] args) {

case "bye":
wantToExit = true;
getUserInput.close();
getUserIndex.close();
Exit();

break;

case "list":
Expand Down Expand Up @@ -105,9 +256,9 @@ public static void main(String[] args) {

case "todo":
System.out.println("Enter task name:");
String taskName = getUserTaskName();
String taskName = GetUserTaskName();
if (!(taskName.isEmpty())) {
userList[listPointer] = new Task(taskName, 1, "");
userList[listPointer] = new Task(taskName, 1, "Null", false);

System.out.println("Got it. I've added this task:");

Expand All @@ -122,11 +273,11 @@ public static void main(String[] args) {

case "deadline":
System.out.println("Enter task name:");
String taskN = getUserTaskName();
String taskN = GetUserTaskName();
if (!(taskN.isEmpty())) {
System.out.println("Enter deadline:");
String timePeriod = getUserInput.nextLine();
userList[listPointer] = new Task(taskN, 2, "by:" + timePeriod);
userList[listPointer] = new Task(taskN, 2, "by:" + timePeriod, false);

System.out.println("Got it. I've added this task:");

Expand All @@ -140,13 +291,14 @@ public static void main(String[] args) {

case "event":
System.out.println("Enter task name:");
String tN = getUserTaskName();
String tN = GetUserTaskName();
if (!(tN.isEmpty())) {
System.out.println("Enter start time:");
String startTime = getUserInput.nextLine();
System.out.println("Enter end time:");
String endTime = getUserInput.nextLine();
userList[listPointer] = new Task(tN, 3, "from: " + startTime + " to: " + endTime);
String timePeriod = "from: " + startTime + " to: " + endTime;
userList[listPointer] = new Task(tN, 3, timePeriod, false);

System.out.println("Got it. I've added this task:");

Expand Down Expand Up @@ -202,6 +354,8 @@ public static void main(String[] args) {
}
printOneLine();

SaveList(userList, listPointer);

}


Expand Down
36 changes: 34 additions & 2 deletions src/main/java/HelperClass/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class Task {
private int type;
private String timePeriod;
private String taskName;
public Task(String taskName, int type, String timePeriod) {
this.isDone = false;
public Task(String taskName, int type, String timePeriod, boolean isDone) {
this.isDone = isDone;
this.type = type;
this.timePeriod = timePeriod;
this.taskName = taskName;
Expand Down Expand Up @@ -50,6 +50,38 @@ public String display() {



return description;
}

public String ForRecordingInTextFile() {
// Record format: "Type | Status | Name | Time"
// example: "D | 0 | return book | June 6th"
// "0" for not done and "1" for done

String description = "";
switch (this.type) {
case 1:
description = description + "T | ";
break;
case 2:
description = description + "D | ";
break;
case 3:
description = description + "E | ";
break;
}
if (isDone) {
description = description + "1 | " + taskName;
} else {
description = description + "0 | " + taskName;
}

if (!(this.type == 1)) {
description = description + " | " + timePeriod;
}



return description;
}
}
Expand Down

0 comments on commit 3df8678

Please sign in to comment.