Skip to content

Commit

Permalink
Merge branch 'branch-A-CodingStandard'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/duke/Ui.java
  • Loading branch information
LimAiLin committed Sep 2, 2022
2 parents dee9760 + 7b9c33c commit f38c435
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 80 deletions.
36 changes: 23 additions & 13 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
import java.util.Objects;
import java.util.Scanner;

/**
* Main class of Duke.
*
* @author Lim Ai Lin
*/
public class Duke {

private final Storage storage;
private final Storage STORAGE;
private TaskList tasks;
private final Ui ui;
private final Parser parser;

private final Ui UI;
private final Parser PARSER;

/**
* Creates a Duke object which saves all tasks into the filePath.
*
* @param filePath The text file to save the tasks.
*/
public Duke(String filePath) {
ui = new Ui();
parser = new Parser();
storage = new Storage(filePath);
UI = new Ui();
PARSER = new Parser();
STORAGE = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
tasks = new TaskList(STORAGE.load());
} catch (IOException e) {
ui.showLoadingError();
UI.showLoadingError();
}
}

Expand All @@ -31,10 +41,10 @@ protected void run() throws DukeException {

while (!Objects.equals(command, "bye")) {
command = sc.nextLine();
ui.showLine();
Command c = parser.parse(command);
c.execute(tasks, ui, storage);
ui.newLine();
UI.showLine();
Command c = PARSER.parse(command);
c.execute(tasks, UI, STORAGE);
UI.newLine();
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package duke;

/**
* Thrown when Duke encounters an issue.
*
* @author Lim Ai Lin
*/
public class DukeException extends Exception {

/**
* Creates a DukeException.
*
* @param errorMessage The error message to be shown.
*/
public DukeException(String errorMessage) {
super(errorMessage);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import java.util.Locale;

/**
* Makes sense of the user commands.
*
* @author Lim Ai Lin
*/
public class Parser {
private enum Commands {
BYE,
Expand All @@ -17,6 +22,14 @@ private enum Commands {
FIND
}

/**
* Parses the input by the user.
*
* @param command The input String to be parsed.
* @return The Command the user wishes to execute.
* @throws DukeException
* Thrown if user inputs an unknown command.
*/
public Command parse(String command) throws DukeException {
String[] str = command.split("\\s", 2);

Expand Down
26 changes: 18 additions & 8 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import java.util.Locale;
import java.util.Objects;

/**
* Loads tasks and saves tasks in the specified file.
*
* @author Lim Ai Lin
*/
public class Storage {

private enum Commands {
Expand All @@ -18,21 +23,26 @@ private enum Commands {
EVENT
}

private final File file;
private final String filePath;
private final File FILE;
private final String FILE_PATH;

/**
* Creates a storage object to load and save all tasks.
*
* @param filePath The text file to load the tasks from and save the tasks to.
*/
public Storage(String filePath) {
this.filePath = filePath;
this.file = new File(filePath);
this.FILE_PATH = filePath;
this.FILE = new File(filePath);
}

protected ArrayList<Task> load() throws IOException {
ArrayList<Task> ls = new ArrayList<>(100);
try {
if (this.file.createNewFile()) {
System.out.println("Dino created a new file: " + file.getName() + "\n");
if (this.FILE.createNewFile()) {
System.out.println("Dino created a new file: " + FILE.getName() + "\n");
} else {
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedReader br = new BufferedReader(new FileReader(FILE));
try {
String current = br.readLine();
while (current != null) {
Expand Down Expand Up @@ -80,7 +90,7 @@ protected ArrayList<Task> load() throws IOException {

public void writeFile(TaskList tasks) {
try {
FileWriter myWriter = new FileWriter(this.filePath);
FileWriter myWriter = new FileWriter(this.FILE_PATH);
StringBuilder str = new StringBuilder();
for (int i = 0; i < tasks.size(); i++) {
Task myTask = tasks.get(i);
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,62 @@

import java.util.ArrayList;

/**
* Keeps track of the current tasks of the user.
*
* @author Lim Ai Lin
*/
public class TaskList {

private final ArrayList<Task> tasks;
private final ArrayList<Task> TASKS;
public TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
this.TASKS = tasks;
}

/**
* Prints out all tasks in the list.
*/
public void list() {
for (int i = 0; i < tasks.size(); i++) {
System.out.println("\t" + (i + 1) + ". " + tasks.get(i).toString());
for (int i = 0; i < TASKS.size(); i++) {
System.out.println("\t" + (i + 1) + ". " + TASKS.get(i).toString());
}
}

/**
* Adds a new task to the list.
*
* @param task The specified task to be added.
*/
public void add(Task task) {
tasks.add(task);
TASKS.add(task);
}

/**
* Gets the task at a specified index.
*
* @param i The index of the task to be returned.
* @return The task at index i.
*/
public Task get(int i) {
return tasks.get(i);
return TASKS.get(i);
}

/**
* Removes the task at the specified index.
*
* @param i The index of the task to be removed.
*/
public void remove(int i) {
tasks.remove(i);
TASKS.remove(i);
}

/**
* Gets the size of the list.
*
* @return The number of items in the list.
*/
public int size() {
return tasks.size();
return TASKS.size();
}

public ArrayList<Task> find(String match) {
Expand Down
71 changes: 68 additions & 3 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

import java.util.ArrayList;

/**
* Deals with interactions with the user.
*
* @author Lim Ai Lin
*/
public class Ui {

private static final String line = "Dino:\n";
private static final String LINE = "Dino:\n";

/**
* Creates a new Ui object.
*/
public Ui() {
System.out.println(" __\n" +
" / _)\n" +
Expand All @@ -16,47 +24,93 @@ public Ui() {
" __/ ( | ( |\n" +
"/__.-'|_|--|_|\n");

System.out.println(line
System.out.println(LINE
+ "\tHello! I'm Dino\n"
+ "\tWhat can I do for you?\n");
}

/**
* Prints a message when Duke faces a loading error.
*/
public void showLoadingError() {
System.out.println("RAWR! Error loading previous tasks.");
}

/**
* Prints a message showing bot and user interaction.
*/
public void showLine() {
System.out.print(line);
System.out.print(LINE);
}

/**
* Prints a message when the user wishes to exit.
*/
public void exit() {
System.out.println("\tBye bye. Hope to see you again soon!");
}

/**
* Throws an exception when the user does not enter a valid description.
*
* @throws DukeException
* Thrown when description is empty.
*/
public void emptyDescription() throws DukeException {
throw new DukeException("RAWR! Empty description.");
}

/**
* Throws an exception when the user does not enter a valid task.
*
* @throws DukeException
* Thrown when task is invalid.
*/
public void invalidTask() throws DukeException {
throw new DukeException("RAWR! Invalid task.");
}

/**
* Throws an exception when the user does not enter a date when needed.
*
* @throws DukeException
* Thrown when there is a missing date.
*/
public void missingDate() throws DukeException {
throw new DukeException("RAWR! Missing date.");
}

/**
* Prints a message when the user has marked a task.
*
* @param myTask The task that the user has marked.
*/
public void complete(Task myTask) {
System.out.println("\tHooray! You have completed this task:\n\t" + myTask);
}

/**
* Prints a message when the user has unmarked a task.
*
* @param myTask The task that the user has unmarked.
*/
public void incomplete(Task myTask) {
System.out.println("\tOh no! You have more things to complete:\n\t" + myTask);
}

/**
* Prints a new line.
*/
public void newLine() {
System.out.print("\n");
}

/**
* Prints a message when the user has added a new task to the list.
*
* @param taskList The tasklist the user has added to.
* @param task The task the user has added.
*/
public void add(TaskList taskList, Task task) {
System.out.println("\tadded: " + task);
System.out.println("\tYou have " + taskList.size() + " task" + (taskList.size() > 1 ? "s!" : "!"));
Expand All @@ -68,4 +122,15 @@ public void match(ArrayList<Task> matching) {
System.out.println("\t" + (i + 1) + ". " + matching.get(i).toString());
}
}

/**
* Prints a message when the user has removed a new task from the list.
*
* @param taskList The tasklist the user has removed from.
* @param task The task the user has removed.
*/
public void remove(TaskList taskList, Task task) {
System.out.println("\tRemoving " + task + "...");
System.out.println("\tYou have " + taskList.size() + " task" + (taskList.size() > 1 ? "s!" : "!"));
}
}
Loading

0 comments on commit f38c435

Please sign in to comment.