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

[Tyrus Lye]iP #557

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
36 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
8664014
Level-0
TyrusLye Aug 23, 2023
892b284
Level-1
TyrusLye Aug 23, 2023
3f22749
Level-2
TyrusLye Aug 23, 2023
bfe5fea
Level-3
TyrusLye Aug 23, 2023
12b399e
Level-4
TyrusLye Aug 24, 2023
9a3f089
A-TextUiTesting
TyrusLye Aug 24, 2023
3f9bc73
Level-5
TyrusLye Aug 24, 2023
c8920e9
Level-6
TyrusLye Aug 24, 2023
937f61c
it doesnt work and i hate my life
TyrusLye Sep 6, 2023
f0cc53b
Level-7
TyrusLye Sep 6, 2023
5310a44
Level-8
TyrusLye Sep 6, 2023
1579d0c
Merge branch 'add-gradle-support'
TyrusLye Sep 10, 2023
6ff52fa
Add-gradle
TyrusLye Sep 10, 2023
22c309f
A-JavaDoc
TyrusLye Sep 22, 2023
bc05937
Level-9
TyrusLye Sep 22, 2023
918f5c8
Update README.md
TyrusLye Sep 22, 2023
47a247e
Update README.md
TyrusLye Sep 22, 2023
ccd2127
Just gonna commit this one before I start messing things up too much
TyrusLye Sep 23, 2023
724286a
Level-10
TyrusLye Sep 23, 2023
d9e7d80
Just bug fixes honestly
TyrusLye Sep 26, 2023
25866f1
Merge branch 'master' of https://github.com/TyrusLye/ip
TyrusLye Sep 26, 2023
904d88d
A-UI
TyrusLye Sep 27, 2023
56b0750
A-UserGuide
TyrusLye Sep 27, 2023
0b4d08c
A few small edits and corrections
TyrusLye Sep 27, 2023
7273363
Deadline and list bug fixes
TyrusLye Oct 1, 2023
2da97b5
Fixing formatting issues
TyrusLye Oct 1, 2023
4f32000
Add Ui.png
TyrusLye Oct 1, 2023
54290c6
Adding more OOP
TyrusLye Oct 23, 2023
c028425
Adding more OOP
TyrusLye Oct 23, 2023
e73f390
Merge pull request #3 from TyrusLye/A-MoreOOP
TyrusLye Oct 23, 2023
4d45b43
A-Storage
TyrusLye Oct 23, 2023
3c69f79
Merge pull request #4 from TyrusLye/branch-Level-9
TyrusLye Oct 23, 2023
0efd428
A-Assertions
TyrusLye Oct 23, 2023
70cbc5f
Merge pull request #5 from TyrusLye/A-Assertions
TyrusLye Oct 23, 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
272 changes: 267 additions & 5 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,272 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;

public class Duke {
private static final String FILE_PATH = "src/main/java/tasks.txt";
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
Scanner scanner = new Scanner(System.in);
ArrayList<Task> tasks = new ArrayList<>(100);
loadTasksFromFile(tasks);
String logo = "██╗░░░██╗██████╗░██████╗░░█████╗░██╗\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool!!

+ "██║░░░██║██╔══██╗██╔══██╗██╔══██╗██║\n"
+ "██║░░░██║██████╔╝██████╦╝██║░░██║██║\n"
+ "██║░░░██║██╔══██╗██╔══██╗██║░░██║██║\n"
+ "╚██████╔╝██║░░██║██████╦╝╚█████╔╝██║\n"
+ "░╚═════╝░╚═╝░░╚═╝╚═════╝░░╚════╝░╚═╝\n";
System.out.println("Hello from\n" + logo);
System.out.println("What can I do for you?");
System.out.println("____________________________________________________________");

while (true) {
String command = scanner.nextLine();
System.out.println("____________________________________________________________");
try{
if (command.equalsIgnoreCase("bye")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to use enumerates for the commands?

System.out.println("Bye. Hope to see you again soon!");
break;
} else if (command.equalsIgnoreCase("list")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it will be better if this part is separated into different functions like addToDoTask, etc. to improve readability, what do you think?

System.out.println("Here are the tasks in your list:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
} else if (command.startsWith("todo")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to package some of these if, else if else steps into a function that handles everything within the outer most if case? for eg. a function to handle creating todo here.

String description = command.substring(5).trim();
if (description.isEmpty()) {
throw new DukeException("The description of a todo cannot be empty.");
}
tasks.add(new Todo(description));
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1));
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
} else if (command.startsWith("deadline")) {
// Parse the date and time in the format d/M/yyyy HHmm
String[] parts = command.split(" /by ");
if (parts.length < 2) {
throw new DukeException("Deadline command must include a date.");
}
String description = parts[0].substring(9).trim();
LocalDateTime dateTime = LocalDateTime.parse(parts[1], DateTimeFormatter.ofPattern("d/M/yyyy HHmm"));

tasks.add(new Deadline(description, dateTime));
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1));
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
} else if (command.startsWith("event")) {
String description = command.substring(6, command.indexOf("/from")).trim();
String from = command.substring(command.indexOf("/from") + 6, command.indexOf("/to")).trim();
String to = command.substring(command.indexOf("/to") + 4).trim();
tasks.add(new Event(description, from, to));
System.out.println("Got it. I've added this task:\n " + tasks.get(tasks.size() - 1));
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
} else if (command.startsWith("mark")) {
int index = Integer.parseInt(command.split(" ")[1]) - 1;
if (index >= 0 && index < tasks.size()) {
tasks.get(index).markDone();
System.out.println("Nice! I've marked this task as done:\n " + tasks.get(index));
} else {
System.out.println("Invalid task index.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be useful if instead of printing invalid task index, the size of the tasklist is also returned in this else case to let user know that for example there is only 3 tasks in the tasklist thats why their input 4 is invalid. Something like "Invalid task index, the task list has " + tasklist.size() + "tasks. "

}
} else if (command.startsWith("unmark")) {
int index = Integer.parseInt(command.split(" ")[1]) - 1;
if (index >= 0 && index < tasks.size()) {
tasks.get(index).markNotDone();
System.out.println("OK, I've marked this task as not done yet:\n " + tasks.get(index));
} else {
System.out.println("Invalid task index.");
}
} else if (command.startsWith("delete")) {
int index = Integer.parseInt(command.split(" ")[1]) - 1;
if (index >= 0 && index < tasks.size()) {
Task removedTask = tasks.remove(index);
System.out.println("Noted. I've removed this task:\n " + removedTask);
System.out.println("Now you have " + tasks.size() + " tasks in the list.");
} else {
System.out.println("Invalid task index.");
}
} else {
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
saveTasksToFile(tasks);
}
catch (DukeException e) {
System.out.println("☹ OOPS!!! " + e.getMessage());
}

System.out.println("____________________________________________________________");
}


scanner.close();
}
private static String formatDate(LocalDate date) {
return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
}
private static void loadTasksFromFile(ArrayList<Task> tasks) {
try {
File file = new File(FILE_PATH);
if (file.exists()) {
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
Task task = createTaskFromLine(line);
if (task != null) {
tasks.add(task);
}
}
fileScanner.close();
}
} catch (FileNotFoundException e) {
// Handle file not found exception
System.out.println("File not found: " + FILE_PATH);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that the handling of this exception not only tells that user that the file cannot be found but also returns the filepath

}
}

private static Task createTaskFromLine(String line) {
String[] parts = line.split(" \\| ");
if (parts.length < 3) {
return null;
}

String type = parts[0];
String status = parts[1];
String description = parts[2];

Task task = null;
switch (type) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For switch statements, I think there shouldn't be indentation on the next line. On the mod website:
switch (condition) {
case ABC:
statements;
// Fallthrough
case DEF:
statements;
break;
case XYZ:
statements;
break;
default:
statements;
break;
}

case "T":
task = new Todo(description);
break;
case "D":
if (parts.length >= 4) {
String by = parts[3];
task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ofPattern("d/M/yyyy HHmm")));
}
break;
case "E":
if (parts.length >= 5) {
String from = parts[3];
String to = parts[4];
task = new Event(description, from, to);
}
break;
}

if (task != null) {
if (status.equals("1")) {
task.markDone();
} else {
task.markNotDone();
}
}

return task;
}


private static void saveTasksToFile(ArrayList<Task> tasks) {
try {
FileWriter fileWriter = new FileWriter(FILE_PATH);
for (Task task : tasks) {
fileWriter.write(task.toFileString() + System.lineSeparator());
}
fileWriter.close();
} catch (IOException e) {
// Handle IO exception
System.out.println("Error saving tasks to file: " + e.getMessage());
}
}
}

class Task {
protected String description;
protected boolean isDone;

protected LocalDateTime date;

public String toFileString() {
return "";
}
public String formatDate() {
return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
}
public Task(String description, LocalDateTime date) {
this.description = description;
this.isDone = false;
this.date = date;
}
public String getStatusIcon() {
return (isDone ? "[X]" : "[ ]"); // Return a tick or cross symbol cuz im lazy like that, or its easier. idk
}
public boolean isDone() {
return isDone;
}
public void markDone() {
isDone = true;
}
public void markNotDone() {
isDone = false;
}
@Override
public String toString() {
return "[" + (isDone ? "X" : " ") + "] " + description;
}
}

class Todo extends Task {
public Todo(String description) {
super(description,null);
}
@Override
public String toFileString() {
return "T | " + (isDone ? "1" : "0") + " | " + description;
}

@Override
public String toString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean and concise, good job!

return "[T]" + super.toString();
}
}

class Deadline extends Task {
protected String by;

public Deadline(String description, LocalDateTime date) {
super(description, date);
}
@Override
public String toFileString() {
return "D | " + (isDone ? "1" : "0") + " | " + description + " | " + date;
}
@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + date + ")";
}
}


class Event extends Task {
protected String from;
protected String to;

public Event(String description, String from, String to) {
super(description,null);
this.from = from;
this.to = to;
}
@Override
public String toFileString() {
return "E | " + (isDone ? "1" : "0") + " | " + description + " | " + from + " | " + to;
}
@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")";
}
}

class DukeException extends Exception {
public DukeException(String message) {
super(message);
}
}
3 changes: 3 additions & 0 deletions src/main/java/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 0 | thing
E | 0 | thing2 | now | later
D | 0 | return book | 2019-12-02T18:00
Binary file added tasks.txt
Binary file not shown.
12 changes: 5 additions & 7 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

____________________________________________________________
Got it. I've added this task:
[D][ ] do homework (by: no idea :-p)
Now you have 6 tasks in the list.
____________________________________________________________
1 change: 1 addition & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deadline do homework /by no idea :-p
8 changes: 6 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
@ECHO OFF

SET "JAVA_HOME=C:\Program Files\Java\jdk-11.0.16"
SET "PATH=%JAVA_HOME%\bin;%PATH%"

REM create bin directory if it doesn't exist
if not exist ..\bin mkdir ..\bin

REM delete output from previous run
if exist ACTUAL.TXT del ACTUAL.TXT
del ACTUAL.TXT

REM compile the code into the bin folder
javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java
javac -cp ..\src\main\java -encoding UTF-8 -Xlint:none -d ..\bin ..\src\main\java\*.java
IF ERRORLEVEL 1 (
echo ********** BUILD FAILURE **********
exit /b 1
Expand All @@ -19,3 +22,4 @@ java -classpath ..\bin Duke < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT

10 changes: 5 additions & 5 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi
# delete output from previous run
if [ -e "./ACTUAL.TXT" ]
then
rm ACTUAL.TXT
rm ACTUAL.TXT.TXT
fi

# compile the code into the bin folder, terminates if error occurred
Expand All @@ -19,15 +19,15 @@ then
exit 1
fi

# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ../bin Duke < input.txt > ACTUAL.TXT
# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT.TXT
java -classpath ../bin Duke < input.txt > ACTUAL.TXT.TXT

# convert to UNIX format
cp EXPECTED.TXT EXPECTED-UNIX.TXT
dos2unix ACTUAL.TXT EXPECTED-UNIX.TXT
dos2unix ACTUAL.TXT.TXT EXPECTED-UNIX.TXT

# compare the output to the expected output
diff ACTUAL.TXT EXPECTED-UNIX.TXT
diff ACTUAL.TXT.TXT EXPECTED-UNIX.TXT
if [ $? -eq 0 ]
then
echo "Test result: PASSED"
Expand Down