Skip to content
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

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
28ad2b8
Add Gradle support
May 24, 2020
ed6d4d2
Bump gradle and lib version
Eclipse-Dominator Aug 5, 2023
f88b6b3
Level 0 Rename, Greet, Exit
Tang-Moyan Aug 24, 2023
fedfbde
Added Level 1. Echo
Tang-Moyan Aug 30, 2023
30a8d0f
Added Level 2. Add, List
Tang-Moyan Aug 30, 2023
76e279d
Added Level 3. Mark as Done
Tang-Moyan Aug 30, 2023
05bfd79
Added Level 4. ToDos, Events, Deadlines
Tang-Moyan Aug 30, 2023
95571af
Added UI Testing
Tang-Moyan Sep 1, 2023
6b91d86
Level 5. Handle Errors
Tang-Moyan Sep 1, 2023
5bb12d4
Level 6. Delete
Tang-Moyan Sep 1, 2023
3df8678
Added Level 7. Save
Tang-Moyan Sep 4, 2023
e5419c6
Small changes
Tang-Moyan Sep 4, 2023
9cd2af3
Merge branch 'branch-Level-7'
Tang-Moyan Sep 4, 2023
61f2b5f
Added Leve 8. Dates and Time
Tang-Moyan Sep 4, 2023
e55e84a
Merge branch 'branch-Level-8'
Tang-Moyan Sep 4, 2023
748abf7
partial OOP
Tang-Moyan Sep 6, 2023
4f477ac
Added A-MoreOOP
Tang-Moyan Sep 6, 2023
20f6c35
Added A-Packages (already done in previous commit)
Tang-Moyan Sep 6, 2023
0b1dd0d
Merge remote-tracking branch 'origin/add-gradle-support' into A-branches
Tang-Moyan Sep 6, 2023
0e65df1
Fixed the bug that cannot get user command correctly
Tang-Moyan Sep 6, 2023
9eecffa
Added A-JUnit
Tang-Moyan Sep 7, 2023
5d8b7a3
Created A-Jar
Tang-Moyan Sep 7, 2023
8a215e5
Added JavaDoc
Tang-Moyan Sep 7, 2023
bf66d36
Modified Coding Standard
Tang-Moyan Sep 7, 2023
9f04997
Added Level 9. Find
Tang-Moyan Sep 7, 2023
44c08ab
Merge branch 'branch-A-JavaDoc' into branch-A-CodingStandard
Tang-Moyan Sep 7, 2023
944be72
Resolved conflict
Tang-Moyan Sep 7, 2023
3d841fc
small modification
Tang-Moyan Sep 8, 2023
525c359
Level 10. GUI
Tang-Moyan Sep 15, 2023
4a8adbc
Make getResponse() return correct String
Tang-Moyan Sep 22, 2023
80c4176
Add showTaskStatics() function
Tang-Moyan Sep 24, 2023
a853e6a
Add A-Assertions
Tang-Moyan Sep 24, 2023
fc087dd
Improved code quality
Tang-Moyan Sep 24, 2023
d513068
Improved code quality further
Tang-Moyan Sep 24, 2023
271c84f
Merge pull request #2 from Tang-Moyan/branch-A-Assertions
Tang-Moyan Sep 24, 2023
c3d3018
Merge branch 'branch-A-CodeQuality'
Tang-Moyan Sep 24, 2023
a2aa77d
Add sort function
Tang-Moyan Sep 24, 2023
fcd88d2
Add User Guide
Tang-Moyan Sep 25, 2023
3b1f871
Add Ui.png
Tang-Moyan Sep 25, 2023
2e43bf1
More improvement to the code
Tang-Moyan Sep 25, 2023
60d851f
More improvement to the code
Tang-Moyan Sep 25, 2023
fc3b8e1
More improvement to the code
Tang-Moyan Sep 25, 2023
62eb998
Apply SLAP to shorten methods
Tang-Moyan Oct 23, 2023
1442d20
Limit task names to be non-empty
Tang-Moyan Oct 23, 2023
996dee8
Command classes: extract command functions into classes
Tang-Moyan Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 363 additions & 0 deletions src/main/java/Duke.java
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 {

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 😸

Copy link
Author

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.

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() {

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The 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);

}




}




}
Loading