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

[Gerald Koh Kheng Guan] iP #192

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
de8ed4d
Achieved Level 0
geraldkoh4 Jan 24, 2023
5d5cc71
Minor Change in spelling
geraldkoh4 Jan 25, 2023
bc93fd2
Finished Level 1
geraldkoh4 Jan 25, 2023
7d21da9
Finished Level-2
geraldkoh4 Jan 25, 2023
6d10d3f
Finished Level-3
geraldkoh4 Jan 25, 2023
444b5c0
Finished level-4 and edited level 0, 1, 2 and 3
geraldkoh4 Jan 31, 2023
f7d165c
Minor format changes
geraldkoh4 Jan 31, 2023
25dffa2
Finished Level 5
geraldkoh4 Feb 7, 2023
609c4a9
Merge branch 'branch-Level-5'
geraldkoh4 Feb 12, 2023
544ba64
Minor changes to main, added DukeSession and fixed minor bugs
geraldkoh4 Feb 12, 2023
8902eae
Level-6 done, with some minor changes to arraylist and actioncounter …
geraldkoh4 Feb 12, 2023
69d4b63
Merge branch 'branch-Level-6'
geraldkoh4 Feb 12, 2023
959c5a4
Finished Level-7, fixed some bugs for Level-6 as well
geraldkoh4 Feb 12, 2023
13f7505
Merge branch 'branch-Level-7'
geraldkoh4 Feb 12, 2023
8a06c04
No idea what this does. Im trying to keep everything up to date.
geraldkoh4 Feb 12, 2023
e0c6dc9
Finished Level-9
geraldkoh4 Mar 1, 2023
391f7bf
Merge branch 'branch-Level-9'
geraldkoh4 Mar 1, 2023
77ef37e
Implemented JavaDoc and other Ui debugging and formatted strings.
geraldkoh4 Mar 2, 2023
6ba8f86
Merge pull request #1 from geraldkoh4/branch-A-JavaDoc
geraldkoh4 Mar 2, 2023
f423633
Update README.md
geraldkoh4 Mar 2, 2023
bf20902
Merge pull request #2 from geraldkoh4/A-UserGuide
geraldkoh4 Mar 2, 2023
cbc2097
Final and Minor formatting changes. Hope everything is working well!
geraldkoh4 Mar 3, 2023
c9f98f4
Update README.md
geraldkoh4 Mar 3, 2023
7831692
Minor Bug fixes
geraldkoh4 Mar 3, 2023
6df56ee
Update README.md
geraldkoh4 Mar 3, 2023
f4792f0
Update README.md
geraldkoh4 Mar 3, 2023
a7d571e
Update README.md
geraldkoh4 Mar 3, 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
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
data/savedList.txt
13 changes: 7 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import utility.Methods;
import java.util.Scanner;
public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
DukeSession dukeSession = new DukeSession();
Methods.printGreetings();
dukeSession.setUpArrayList();
dukeSession.execute();
}
}

92 changes: 92 additions & 0 deletions src/main/java/DukeSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import file.methods.FileHandler;
import utility.Methods;
import utility.commandChecker;

import tasks.Deadline;
import tasks.Event;
import tasks.Task;
import tasks.Todo;

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;


public class DukeSession {
private ArrayList<Task> actions = new ArrayList<>();
public void setUpArrayList() {
FileHandler.setUpFile(actions);
}
public void execute() {

Choose a reason for hiding this comment

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

This method is quite long (>30 lines). Consider refactoring the code to a new method OR see how you can combine common things from the case statements together. See here

String line;
int taskNumber;
String[] decisions;
Task toBeAdded;
String[] dates;
Scanner in = new Scanner(System.in);
do {
line = in.nextLine();
decisions = line.split(" ");
dates = line.split("/");
commandChecker currentLoop = new commandChecker(decisions, dates, actions.size());
if (currentLoop.hasErrors()) {
decisions[0] = "Invalidate";
}
switch (decisions[0]) {
case "echo":
System.out.println(Methods.findTaskDetails(line));
break;

case "todo":
toBeAdded = new Todo(Methods.findTaskDetails(line));
actions.add(toBeAdded);
Methods.printAcknowledgement(toBeAdded, actions.size());
break;

case "event":
toBeAdded = new Event(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]), Methods.findTaskDetails(dates[2]));
actions.add(toBeAdded);
Methods.printAcknowledgement(toBeAdded, actions.size());
break;

case "deadline":
toBeAdded = new Deadline(Methods.findTaskDetails(dates[0]), Methods.findTaskDetails(dates[1]));
actions.add(toBeAdded);
Methods.printAcknowledgement(toBeAdded, actions.size());
break;

case "mark":
taskNumber = Integer.parseInt(decisions[1]) - 1;
actions.get(taskNumber).mark();
Methods.printDoneMarkingTasks(actions.get(taskNumber));
break;

case "unmark":
taskNumber = Integer.parseInt(decisions[1]) - 1;
actions.get(taskNumber).unmark();
Methods.printDoneMarkingTasks(actions.get(taskNumber));
break;

case "list":
for (int iterator = 0; iterator < actions.size(); iterator++) {
Methods.printListElement(iterator, actions.get(iterator));
}
break;
case "delete":
taskNumber = Integer.parseInt(decisions[1]) - 1;
Methods.printDeleteAcknowledgement(actions.get(taskNumber), actions.size() - 1);
actions.remove(taskNumber);
break;

case "bye":
break;

default:
Methods.printCurrentSupportedActions();
}
} while (!decisions[0].equals("bye"));
FileHandler.saveFile(actions);
System.out.println("That's all from me! Goodbye!");
}

}
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Duke

83 changes: 83 additions & 0 deletions src/main/java/file/methods/FileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package file.methods;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


import tasks.Task;
import tasks.Todo;
import tasks.Event;
import tasks.Deadline;

public class FileHandler {

public static void setUpFile(ArrayList<Task> actions) {

Choose a reason for hiding this comment

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

This function is also quite long, maybe see how you can refactor some things inside it.

try {
File f = new File("data/savedList.txt");
File directory = new File("data");

Choose a reason for hiding this comment

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

Perhaps avoid the usage of magic literals. You can store the string literals as constants and use them (final static vars). See here

if (!directory.exists()) {
boolean success = directory.mkdir();
if (!success) {

Choose a reason for hiding this comment

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

Avoid deep nesting to improve readability. If needed, you can refactor the inner nests to a new method. See here

System.out.println("Met an error creating the directory");
}
}
if (!f.exists()) {
f.createNewFile();
return;
}
String line;
String[] decisions;
Scanner in = new Scanner(f);
Task toBeAdded;
while (in.hasNext()) {
line = in.nextLine();
decisions = line.split("/");
switch(decisions[0]) {
case "t":
toBeAdded = new Todo(decisions[2]);
if (decisions[1].equals("X")) {
toBeAdded.mark();
}
actions.add(toBeAdded);
break;

case "d":
toBeAdded = new Deadline(decisions[2],decisions[3]);
if (decisions[1].equals("X")) {
toBeAdded.mark();
}
actions.add(toBeAdded);
break;

case "e":
toBeAdded = new Event(decisions[2],decisions[3],decisions[4]);
if (decisions[1].equals("X")) {
toBeAdded.mark();
}
actions.add(toBeAdded);
break;

default:
System.out.println("File has been corrupted");
}
}
} catch (IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
}
}
public static void saveFile(ArrayList<Task> actions) {
try {
FileWriter fw = new FileWriter("data/savedList.txt");
for (Task i : actions) {
fw.write(i.getCommand() + System.lineSeparator());
}
fw.close();
} catch (IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tasks;

public class Deadline extends Task {

private String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

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


public String getCommand() {
return "d/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.by;
}
}
23 changes: 23 additions & 0 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tasks;

public class Event extends Task {

private String from;
private String to;

public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from + "to: " + to + ")";

}

public String getCommand() {
return "e/" + this.getStatusIcon() + "/" + this.getDescription() + "/" + this.from + "/" + this.to ;
}
}
36 changes: 36 additions & 0 deletions src/main/java/tasks/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tasks;

public class Task {
private String description;
private boolean isDone;

private String command;
public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

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

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

public String getDescription() {
return (this.description);
}

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

public String getCommand() {
return this.command;
}
}
20 changes: 20 additions & 0 deletions src/main/java/tasks/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tasks;

public class Todo extends Task {

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


public String getCommand() {
return "t/" + this.getStatusIcon() + "/"+ this.getDescription();
}

}


21 changes: 21 additions & 0 deletions src/main/java/utility/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utility;

public class DukeException extends Exception {
public String description;

public DukeException() {
this.description = "No Description";
}

public DukeException(String description) {
this.description = description;
}

public void setDescription(String description) {
this.description = description;
}

public String getDescription() {
return this.description;
}
}
49 changes: 49 additions & 0 deletions src/main/java/utility/Methods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package utility;

import tasks.Task;

public class Methods {

Choose a reason for hiding this comment

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

The name of this class does not describe what it is used for. Maybe consider naming itUi instead since it seems to handle the functions for console output operations. See here

public static void printGreetings() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n"
+ "__________________________\n";
System.out.println("Hello from\n" + logo);
System.out.println("Hello! Do you need anything from me?\n"
+ "I have only been trained to greet, echo and list you so far.\n"
+ "Once my owner is more proficient in what he does, he will give me more functions!\n"
+ " 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n"
+ "When you wish to exit, do tell me by typing : bye");
}

public static void print(String input) {
System.out.println(input);
}

public static void printListElement(int iterator, Task action) {
Methods.print(iterator + 1
+ ")"
+ action.toString());
}

public static void printDoneMarkingTasks(Task action) {
Methods.print("Done!\n" + action.toString());
}

public static String findTaskDetails(String line) {
return line.substring(line.indexOf(" ") + 1);
}

public static void printAcknowledgement(Task action, int actionCounter) {
System.out.println("Got it. I've added this task:\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list.");
}

public static void printCurrentSupportedActions() {
Methods.print("I am currently only able to do: \n 1)echo \n 2)todo\n 3)mark\n 4)unmark\n 5)deadline\n 6)event\n 7)delete\n");
}
public static void printDeleteAcknowledgement(Task action, int actionCounter) {
print("Got it. I've removed this task\n" + action.toString() + System.lineSeparator() + "Now you have " + actionCounter + " tasks in the list.");
}
}
Loading