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

[Rena] iP #458

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
55f9f9f
docs/README.md: Tweak document template
Jan 7, 2024
f837ddb
Add Gradle support
May 24, 2020
a6f7324
Bump gradle and lib version
Eclipse-Dominator Aug 5, 2023
e038c80
Level 0-1
Chiarena Jan 30, 2024
ec99ab5
Level 1-2
Chiarena Jan 30, 2024
8bf1856
level 2 done
Chiarena Jan 30, 2024
0b4044d
Added level 3
Chiarena Jan 31, 2024
bfe14f0
Added level 4 - deadline,todo,event
Chiarena Jan 31, 2024
beff8a4
added functions
Chiarena Feb 1, 2024
3103d89
Automated testing done
Chiarena Feb 5, 2024
52770ed
Exceptions class added
Chiarena Feb 5, 2024
e65d1c3
Delete function added
Chiarena Feb 5, 2024
c9ede7f
Added save feature
Chiarena Feb 12, 2024
7c7eb0f
date time added
Chiarena Feb 18, 2024
a45e142
oop done
Chiarena Feb 23, 2024
cecd03d
Classes arranged into packages
Chiarena Feb 23, 2024
396cf5c
Merge remote-tracking branch 'origin/add-gradle-support'
Chiarena Feb 25, 2024
11e9506
isExit corrected
Chiarena Feb 25, 2024
f829900
corrected bye command to terminate program
Chiarena Feb 25, 2024
89331f7
JUnit tests added
Chiarena Feb 25, 2024
4459683
java docs added
Chiarena Feb 25, 2024
61137a6
removed redundant code
Chiarena Feb 25, 2024
eb8604d
Find feature added
Chiarena Feb 25, 2024
c1110ed
Coding standard improved
Chiarena Feb 25, 2024
e785e0e
Merge branch 'branch-A-JavaDoc'
Chiarena Feb 26, 2024
fca0a8a
Merge branch 'branch-A-CodingStandard'
Chiarena Feb 26, 2024
b21992a
Merge branch 'branch-Level-9'
Chiarena Feb 26, 2024
1f9dfe6
further corrected code after merging
Chiarena Feb 26, 2024
96ec174
Add critical assertions across to the parser class
Chiarena Feb 26, 2024
d8808e1
The code for each class is edited and sectioned to improve code quality
Chiarena Feb 26, 2024
a7c37b3
Merge pull request #3 from Chiarena/branch-A-Assertions
Chiarena Feb 26, 2024
ed78efc
Merge branch 'master' into branch-CodeQuality
Chiarena Feb 26, 2024
1fa1a03
Merge pull request #2 from Chiarena/branch-CodeQuality
Chiarena Feb 26, 2024
222176b
update feature added
Chiarena Feb 26, 2024
f075d9c
Update README.md
Chiarena Feb 26, 2024
14682ee
README edited
Chiarena Feb 26, 2024
0daaeec
Added user guide
Chiarena Feb 29, 2024
a8872c7
JavaFX
Chiarena Feb 29, 2024
774cdc7
Merge branch 'branch-Level-10'
Chiarena Feb 29, 2024
b3794f2
JavaFX edited
Chiarena Feb 29, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
*.class
4 changes: 4 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
T | 0 | read book |
E | 0 | read | mon - thurs
D | 0 | homework | tomorrow
T | 0 | read book |
95 changes: 95 additions & 0 deletions src/main/java/DataManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class DataManager {
private static String path;

public DataManager(String path) {
this.path = path;
}

private static String convertToFile(Task task) {
String symbol = " ";
String time = " ";
String status = " ";
if(task instanceof Todo) {
symbol = "T";
}else if(task instanceof Deadline) {

Choose a reason for hiding this comment

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

could insert a space between } and else if

symbol = "D";
time = ((Deadline) task).getTime();
}else{
symbol = "E";
time = ((Event)task).getFrom() + " - " + ((Event)task).getTo();
}
if(task.getStatus()) {
status = "1";
}else{
status = "0";
}
return symbol + " | " + status + " | " + task.name + " | " + time;
}

public static void saveTasks(ArrayList<Task> tasks) {
try{
File dataFile = new File(path);
dataFile.getParentFile().mkdirs();
FileWriter writer = new FileWriter(dataFile, false);
for (Task task : tasks) {
writer.write(convertToFile(task) + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("Unable to save tasks.");
}
}

public ArrayList<Task> retrieveTasks () {
ArrayList<Task> tasks = new ArrayList<>();
try{
File file = new File(path);
if(file.exists()) {
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()) {
String data =scanner.nextLine();
Task task = getTask(data);
if (task != null) {
tasks.add(task);
}
}
scanner.close();
}
} catch (IOException e) {
System.out.println("Unable to load tasks.");
}
return tasks;
}

public static boolean getStatus(String num) {
return num.equals("1");
}
public static Task getTask(String message) {
Task task = null;
String parts[] = message.split(" \\| ");
String type = parts[0];
boolean status = getStatus(parts[1]);
String taskName = parts[2];
String time = parts[3];

if(type.equals("T")) {
task = new Todo(taskName, status);
}else if(type.equals("D")) {
task = new Deadline(taskName, status, time);
}else {
String timeParts[] = time.split(" - ");
String from = timeParts[0];
String to = timeParts[1];
task = new Event(taskName, status, from, to );
}
return task;
}

}
19 changes: 19 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Deadline extends Task {
protected String by;

public Deadline(String name, boolean isDone, String by) {
super(name, isDone);

this.by = by;
}

public String getTime() {
return this.by;
}

@Override
public String toString(){
return "[D] " + super.toString() + " (by:" + by + ")";
}

}
161 changes: 157 additions & 4 deletions src/main/java/Duke.java

Choose a reason for hiding this comment

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

Could probably store the lines as a constant value to make the code easier to change.

Original file line number Diff line number Diff line change
@@ -1,10 +1,163 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
private static DataManager file = new DataManager("./data/duke.txt");
public static String printIntro() {
return " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
+ "|____/ \\__,_|_|\\_\\___|\n" + "____________________________________________________________\n" +
" Hello! I'm Duke\n" +
" What can I do for you?\n" +
"____________________________________________________________\n";
}

public static String printOutro() {
return "____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
"____________________________________________________________\n";
}

public static String list(ArrayList<Task> a, int counter) {
String x = ("Here are the tasks in your list: " + "\n" +
"____________________________________________________________\n");
String result = "";
for (int i = 0; i < counter; i++) {
result += (i + 1) + ". " + a.get(i).toString() + "\n";
}
String y = ("____________________________________________________________\n");
return x + result + y;
}

public static String printMark(Task task) {
if (task.getStatus()) {
return "____________________________________________________________\n" +
"Nice! I've marked this task as done:" + "\n" + task.toString() + "\n" +
"____________________________________________________________\n";
} else {
return "____________________________________________________________\n" +
"OK, I've marked this task as not done yet-:" + "\n" + task.toString() + "\n" +
"____________________________________________________________\n";
}
}

public static String added(Task task, int count) {

Choose a reason for hiding this comment

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

Suggested change
public static String added(Task task, int count) {
public static String printAdded(Task task, int count) {

Methods should have verbs as names.

return "____________________________________________________________\n" +
"Got it.I've added this task:" + "\n" + task.toString() + "\n" +
"Now you have " + count + " tasks in the list." + "\n" +
"____________________________________________________________\n";
}

public static String getTask(String message) {
int start = message.indexOf(" ") + 1;
int end = message.indexOf("/");
if (end == -1) {
// "/" not found, handle the case, e.g., throw an exception or return the rest of the string
end = message.length();
}
String task = message.substring(start, end);
return task.trim();
}


public static String getTime(String message) {
int start = message.indexOf("/");
String time = message.substring(start + 1);
return time;
}

public static String deleteMessage(Task task, int count) {
return "____________________________________________________________\n" +
"Noted. I've removed this task:" + "\n" + task.toString() + "\n" +
"Now you have " + count + " tasks in the list." + "\n" +
"____________________________________________________________\n";
}


public static String replace(String message) {
String newString = message.replaceAll("/(\\w+)", "$1:");
return newString;
}

public static void main(String[] args) {
System.out.println(printIntro());
Scanner input = new Scanner(System.in);
System.out.println("Enter Message");

final int ArraySize = 100;

Choose a reason for hiding this comment

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

Constants should be in screaming_snake_case.

ArrayList<Task> tasks = file.retrieveTasks();
int counter = tasks.size();

while (true) {
String message = input.nextLine();

try {
if (message.equals("bye")) {
System.out.println(printOutro());
break;

} else if (message.equals("list")) {
System.out.println(list(tasks, tasks.size()));

} else if (message.startsWith("mark") || message.startsWith("unmark") || message.startsWith("delete")) {
String[] parts = message.split(" ");
int num = Integer.parseInt(parts[1]);
Task current = tasks.get(num - 1);

if (message.startsWith("mark")) {
current.markAsDone();
System.out.println(printMark(current));
file.saveTasks(tasks);
} else if (message.startsWith("unmark")){
current.unmark();
System.out.println(printMark(current));
file.saveTasks(tasks);
}else{

Choose a reason for hiding this comment

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

I think you may have forgotten to add a space before and after the 'else' here.

tasks.remove(num - 1);
System.out.println(deleteMessage(current, tasks.size()));
file.saveTasks(tasks);
}
} else if (message.equals("todo") || message.equals("deadline") || message.equals("event")){
throw new DukeExceptions("Don't forget the description !");
} else if(message.startsWith("todo") || message.startsWith("deadline") || message.startsWith("event")) {
String time = getTime(message);
if (message.startsWith("todo")) {
String[] parts = message.split(" ", 2);
String task = parts[1];
// tasks[counter] = new Todo(task);

Choose a reason for hiding this comment

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

consider removing these comments once code is finalised :)

tasks.add(new Todo(task, false));
counter++;
System.out.println(added(tasks.get(counter - 1), counter));
file.saveTasks(tasks);
} else if (message.startsWith("deadline")) {
String task = getTask(message);
String[] parts = time.split("by");
// tasks[counter] = new Deadline(task, parts[1]);
tasks.add(new Deadline(task, false, parts[1]) );
counter++;
System.out.println(added(tasks.get(counter - 1), counter));
file.saveTasks(tasks);
} else {
String task = getTask(message);
String[] parts = time.split("from");
String[] dateParts = parts[1].split("/to");
// tasks[counter] = new Event(task, dateParts[0], dateParts[1]);
tasks.add(new Event(task, false, dateParts[0], dateParts[1]));
counter++;
System.out.println(added(tasks.get(counter - 1), counter));
file.saveTasks(tasks);
}

} else {
throw new DukeExceptions("Sorry, I'm not sure what you mean");
}


} catch (DukeExceptions e) {
System.out.println(e.getMessage());
}
}
}
}

5 changes: 5 additions & 0 deletions src/main/java/DukeExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class DukeExceptions extends Exception{
public DukeExceptions(String message) {
super (message);
}
}
2 changes: 2 additions & 0 deletions src/main/java/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


23 changes: 23 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Event extends Task{
protected String from;
protected String to;

public Event(String name, boolean isDone, String from, String to){
super(name, isDone);
this.from = from;
this.to = to;
}

public String getTo(){
return this.to;
}

public String getFrom() {
return this.from;
}

@Override
public String toString() {
return "[E] " + super.toString() + " (from:" + from + " to:" + to + ")" ;
}
}
35 changes: 35 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import javax.xml.namespace.QName;


public class Task {
protected String name;
protected boolean isDone;

public Task(String name, boolean isDone) {
this.name = name;
this.isDone = isDone;
}

public String getStatusIcon(){
return(isDone? "X" : " ");
}

public Boolean getStatus() { return isDone; }

public void markAsDone() {
this.isDone = true;
}

public void unmark(){
this.isDone = false;
}

// public String getTask() {
// return this.name;
// }

public String toString() {
return "[" + this.getStatusIcon() + "] " + this.name ;
}

}
11 changes: 11 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Todo extends Task{

public Todo(String name, boolean isDone) {
super(name, isDone);
}

@Override
public String toString(){
return "[T] " + super.toString();
}
}
Loading