-
Notifications
You must be signed in to change notification settings - Fork 484
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
[Tang-Moyan] iP #542
base: master
Are you sure you want to change the base?
[Tang-Moyan] iP #542
Changes from 13 commits
28ad2b8
ed6d4d2
f88b6b3
fedfbde
30a8d0f
76e279d
05bfd79
95571af
6b91d86
5bb12d4
3df8678
e5419c6
9cd2af3
61f2b5f
e55e84a
748abf7
4f477ac
20f6c35
0b1dd0d
0e65df1
9eecffa
5d8b7a3
8a215e5
bf66d36
9f04997
44c08ab
944be72
3d841fc
525c359
4a8adbc
80c4176
a853e6a
fc087dd
d513068
271c84f
c3d3018
a2aa77d
fcd88d2
3b1f871
2e43bf1
60d851f
fc3b8e1
62eb998
1442d20
996dee8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,373 @@ | ||
import java.nio.file.Paths; | ||
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 { | ||
private static void printOneLine() { | ||
System.out.println("---------------------------"); | ||
} | ||
|
||
private static final String MyName = "Rio"; | ||
public static void Greet() { | ||
printOneLine(); | ||
System.out.println("Hello! I'm " + MyName); | ||
System.out.println("What can I do for you?"); | ||
printOneLine(); | ||
} | ||
|
||
public static void Exit() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double spacing can change to single between public and static |
||
|
||
System.out.println(" Bye. Hope to see you again soon!"); | ||
|
||
} | ||
|
||
private static String GetUserTaskName() { | ||
Scanner getUserInput = new Scanner(System.in); | ||
String taskName = getUserInput.nextLine(); | ||
if (taskName.isEmpty()) { | ||
System.out.println("OOPS!!! The name of a task cannot be empty."); | ||
return ""; | ||
} else { | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant return statement |
||
} | ||
} | ||
|
||
|
||
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 | StartTime(optional) | EndTime(optional)" | ||
// example: "D | 0 | return book | 2023-09-04" | ||
// "0" for not done and "1" for done | ||
|
||
String line = fileScanner.nextLine(); | ||
|
||
List<String> formattedLine = ReadLine(line); | ||
|
||
|
||
|
||
List<String> attributes = new ArrayList<>(); | ||
StringBuilder attributeName = new StringBuilder(); | ||
|
||
for (Object element : formattedLine) { | ||
if (element.equals("|")) { | ||
attributes.add(attributeName.toString()); | ||
attributeName = new StringBuilder(); | ||
} else { | ||
if (attributeName.length() == 0) { | ||
attributeName.append(element); | ||
} else { | ||
attributeName.append(" ").append(element); | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
attributes.add(attributeName.toString()); | ||
boolean isDone = attributes.get(1).equals("1"); | ||
|
||
switch (attributes.get(0)) { | ||
case "T": { | ||
Task task = new Task(attributes.get(2), 1, "Null", "Null", isDone); | ||
userList[positionPointer] = task; | ||
|
||
break; | ||
} | ||
case "D": { | ||
Task task = new Task(attributes.get(2), 2, "Null", attributes.get(3), isDone); | ||
userList[positionPointer] = task; | ||
|
||
break; | ||
} | ||
case "E": { | ||
Task task = new Task(attributes.get(2), 3, attributes.get(3), attributes.get(4), 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); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\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 = LoadList(); | ||
|
||
int listPointer = 0; | ||
|
||
while (!(wantToExit)) { | ||
String userInput = getUserInput.nextLine(); | ||
|
||
printOneLine(); | ||
switch (userInput) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code is kindda big so maybe can encapsulate out |
||
case "bye": | ||
wantToExit = true; | ||
getUserInput.close(); | ||
getUserIndex.close(); | ||
Exit(); | ||
|
||
break; | ||
|
||
case "list": | ||
|
||
if (listPointer < 1) { | ||
System.out.println("No items in the list yet"); | ||
} else { | ||
for (int i = 0; i < listPointer; i++) { | ||
int num = i + 1; | ||
System.out.println(num + userList[i].display()); | ||
|
||
|
||
} | ||
} | ||
|
||
break; | ||
|
||
case "mark": | ||
System.out.println("Enter index:"); | ||
int index = getUserIndex.nextInt() - 1; | ||
if (index < 0 || index >= listPointer) { | ||
System.out.println("Invalid index."); | ||
} else { | ||
|
||
userList[index].markDone(); | ||
|
||
} | ||
|
||
break; | ||
|
||
case "unmark": | ||
System.out.println("Enter index:"); | ||
int i = getUserIndex.nextInt() - 1; | ||
if (i < 0 || i >= listPointer) { | ||
System.out.println("Invalid index."); | ||
} else { | ||
|
||
userList[i].unmarkDone(); | ||
|
||
} | ||
|
||
break; | ||
|
||
case "todo": | ||
System.out.println("Enter task name:"); | ||
String taskName = GetUserTaskName(); | ||
if (!(taskName.isEmpty())) { | ||
userList[listPointer] = new Task(taskName, 1, "Null", "Null", false); | ||
|
||
System.out.println("Got it. I've added this task:"); | ||
|
||
System.out.println(userList[listPointer].display()); | ||
|
||
listPointer = listPointer + 1; | ||
|
||
System.out.println("Now you have " + listPointer + " tasks in the list."); | ||
} | ||
|
||
break; | ||
|
||
case "deadline": | ||
System.out.println("Enter task name:"); | ||
String taskN = GetUserTaskName(); | ||
if (!(taskN.isEmpty())) { | ||
System.out.println("Enter deadline:"); | ||
String timePeriod = getUserInput.nextLine(); | ||
userList[listPointer] = new Task(taskN, 2, "Null", timePeriod, false); | ||
|
||
System.out.println("Got it. I've added this task:"); | ||
|
||
System.out.println(userList[listPointer].display()); | ||
|
||
listPointer = listPointer + 1; | ||
|
||
System.out.println("Now you have " + listPointer + " tasks in the list."); | ||
} | ||
break; | ||
|
||
case "event": | ||
System.out.println("Enter task name:"); | ||
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, startTime, endTime, false); | ||
|
||
System.out.println("Got it. I've added this task:"); | ||
|
||
System.out.println(userList[listPointer].display()); | ||
|
||
listPointer = listPointer + 1; | ||
|
||
System.out.println("Now you have " + listPointer + " tasks in the list."); | ||
} | ||
break; | ||
|
||
case "delete": | ||
System.out.println("Enter index:"); | ||
int ind = getUserIndex.nextInt() - 1; | ||
if (ind < 0 || ind >= listPointer) { | ||
System.out.println("Invalid index."); | ||
} else { | ||
|
||
System.out.println("Noted. I've removed this task:"); | ||
|
||
System.out.println(userList[listPointer].display()); | ||
|
||
Task[] newUserList = new Task[100]; | ||
|
||
for (int a = 0, k = 0; a < listPointer; a++) { | ||
|
||
// if the index is | ||
// the removal element index | ||
if (a == ind) { | ||
continue; | ||
} | ||
|
||
// if the index is not | ||
// the removal element index | ||
newUserList[k++] = userList[a]; | ||
} | ||
|
||
listPointer = listPointer - 1; | ||
|
||
userList = newUserList; | ||
|
||
System.out.println("Now you have " + listPointer + " tasks in the list."); | ||
|
||
} | ||
|
||
break; | ||
|
||
|
||
default: | ||
System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
|
||
|
||
} | ||
printOneLine(); | ||
|
||
SaveList(userList, listPointer); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, not sure which level you are at but you could consider more encapsulation of code 😸
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your review! I am a bit falling behind in catching up with the levels. I will try to get them done sooner.