Skip to content

Commit

Permalink
Merge branch-A-JavaDoc to master
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpola committed Jan 28, 2024
2 parents db32dbf + 00778f5 commit 220128e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 11 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

mainClassName = 'Nicole'

repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
Expand All @@ -23,6 +25,7 @@ application {
}

shadowJar {
archiveFileName = "nicole.jar"
archiveBaseName = "nicole"
archiveClassifier = null
dependsOn("distZip", "distTar")
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/Nicole.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import java.io.IOException;

public class Nicole {

/**
* Initialises the chatbot and triggers user interactions
*
*/
public Nicole() {
try {
System.out.println(new Ui().talkToUser());
System.out.println(new Ui());
} catch (IOException e) {
System.out.println(e);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/Task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import NicoleExceptions.NicoleException;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

public class Deadline extends Task {
private LocalDate deadlineDateLocalDate;
private String deadlineDateReformattedString;

/**
* Initialises a Deadline
*
* @param name the user request.
* @throws NicoleException if the description is not as deadline [task] by YYYY-MM-DD.
*/
public Deadline(String name) throws NicoleException {
super();
System.out.println(name);
Expand All @@ -17,7 +25,7 @@ public Deadline(String name) throws NicoleException {
this.parseDate(name);
super.setName(name);
}
public void parseDate(String name) throws NicoleException {
private void parseDate(String name) throws NicoleException {
String[] whiteSpaceSeparatedDate = name.split(" ");
String date = whiteSpaceSeparatedDate[whiteSpaceSeparatedDate.length - 1];
try {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public class Event extends Task {
private String deadlineFromTimeReformattedString;
private String deadlineToDateReformattedString;
private String deadlineToTimeReformattedString;

/**
* Initialises a Event
*
* @param name the user request.
* @throws NicoleException if the description is not
* as event [name] from YYYY-MM-DD at HH-MM-SS
* to YYYY-MM-DD at HH-MM-SS.
*/
public Event(String name) throws NicoleException {
super();
System.out.println(name);
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/Task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import NicoleExceptions.NicoleException;

import java.time.LocalDate;
import java.util.Objects;

public class Task {
private boolean completed;
private String name;

/**
* Initialises a Task. The default state of a Task is incomplete.
*
*/
public Task() {
this.completed = false;
}
Expand All @@ -16,6 +21,14 @@ public void setName(String name) {
this.name = name;
}

/**
* Factory method for Tasks.
*
* @param name the user request.
* @param taskType 'T' for Todo, 'D' for Deadline, 'E' for Event.
* @throws NicoleException if any of Task, Deadline, or Event throws it due to
* invalid request.
*/
public static Task taskFactory(String name, char taskType) throws NicoleException {
if (taskType == 'T') {
return new Todo(name);
Expand All @@ -26,6 +39,11 @@ public static Task taskFactory(String name, char taskType) throws NicoleExceptio
}
}

/**
* Marks a task object as completed.
*
* @throws NicoleException if the task is already marked completed.
*/
public String markDone() throws NicoleException {
if (this.completed) {
throw new NicoleException("That is already marked complete -_-");
Expand All @@ -35,6 +53,11 @@ public String markDone() throws NicoleException {
}
}

/**
* Marks a task object as incomplete.
*
* @throws NicoleException if the task is already marked incomplete.
*/
public String markUndone() throws NicoleException {
if (!this.completed) {
throw new NicoleException("That is already marked incomplete -_-");
Expand All @@ -44,6 +67,11 @@ public String markUndone() throws NicoleException {
}
}

/**
* Retrieves the date associated with the Task object.
*
* @return the max LocalDate by default.
*/
public LocalDate getDate() {
return LocalDate.parse("+999999999-12-31");
}
Expand All @@ -52,8 +80,4 @@ public LocalDate getDate() {
public String toString() {
return this.completed ? "[C] " + this.name : "[I] " + this.name;
}

public boolean equals(Task task) {
return this.completed == task.completed;
}
}
7 changes: 7 additions & 0 deletions src/main/java/Task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import NicoleExceptions.NicoleException;

public class Todo extends Task {

/**
* Initialises a Todo
*
* @param name the user request.
* @throws NicoleException if the description is not in todo [task]
*/
public Todo(String name) throws NicoleException {
super();
if (name == null) {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/TaskStorage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
public class Storage {
File tasksFile;
FileWriter taskFileWriter;

/**
* Initialises Storage by creating a directory "./data" and a file "tasks.txt" inside.
*
* @throws NicoleException if there are issues with creating a writer to the task file.
*/
public Storage() throws NicoleException {
new File("./data").mkdirs();
this.tasksFile = new File("./data/tasks.txt");
Expand All @@ -21,6 +27,12 @@ public Storage() throws NicoleException {
}
this.loadTasksFromFile();
}

/**
* Saves user tasks to "./data/tasks.txt"
*
* @throws NicoleException if there are write issues to tasks.txt
*/
public void saveTasksToFile() throws NicoleException {
try {
for (int i = 0; i < TaskList.taskList.size(); i++) {
Expand All @@ -31,15 +43,21 @@ public void saveTasksToFile() throws NicoleException {
throw new NicoleException("I couldn't save the task >< try again plss");
}
}

/**
* Loads user tasks from "./data/tasks.txt" and recreates a list of tasks.
*
* @throws NicoleException if there are read issues from tasks.txt
*/
public void loadTasksFromFile() throws NicoleException {
File tasksFile = new File("./data/tasks.txt");
try {
Scanner userTaskFileReader = new Scanner(tasksFile);
int numTasksInFile = 0;
BufferedReader reader = new BufferedReader(new FileReader(tasksFile));
while (reader.readLine() != null) {
numTasksInFile++;
}
Scanner userTaskFileReader = new Scanner(tasksFile);
int i = 1;
while (userTaskFileReader.hasNextLine()) {
String task = userTaskFileReader.nextLine();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/TaskStorage/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,67 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class TaskList {
public static final List<Task> taskList = new ArrayList<>();
private final Storage storage;

/**
* Initialises a TaskList and creates Storage for tasks.
*
* @throws NicoleException if Storage throws it due to IO issues with the
* tasks file.
*/
public TaskList() throws NicoleException {
storage = new Storage();
}

private void crudChecker(int taskNumber) throws NicoleException {
if (taskNumber <= 0 || taskNumber > TaskList.taskList.size()) {
throw new NicoleException("Huh? That's not a valid item number :')");
}
}

/**
* Unmarks a task in the list.
*
* @throws NicoleException if the unmark index is out of bounds of the list.
*/
public void unmarkTask(int taskNumber) throws NicoleException {
this.crudChecker(taskNumber);
System.out.println(TaskList.taskList.get(taskNumber - 1).markUndone());
this.storage.saveTasksToFile();
}

/**
* Marks a task in the list.
*
* @throws NicoleException if the mark index is out of bounds of the list.
*/
public void markTask(int taskNumber) throws NicoleException {
this.crudChecker(taskNumber);
System.out.println(TaskList.taskList.get(taskNumber - 1).markDone());
this.storage.saveTasksToFile();
}

/**
* Deletes a task in the list.
*
* @throws NicoleException if the delete index is out of bounds of the list.
*/
public void deleteTask(int taskNumber) throws NicoleException {
this.crudChecker(taskNumber);
TaskList.taskList.remove(taskNumber - 1);
System.out.println("Nicole: Phew...deleted :>");
this.storage.saveTasksToFile();
}

/**
* Adds a task to the list.
*
* @throws NicoleException if there are write issues to tasks.txt
*/
public void addTask(Task newTask) throws NicoleException {
TaskList.taskList.add(newTask);
try {
Expand All @@ -53,6 +83,12 @@ public void addTask(Task newTask) throws NicoleException {
}
System.out.println("Nicole: Oki I added " + "\"" + newTask.toString().substring(7) + "\"");
}

/**
* Lists the present tasks. Sorts the list by task date if the user requests it
* before printing the tasks.
*
*/
public void listTasks() {
if (TaskList.taskList.size() == 0) {
System.out.println("Nicole: No tasks yet. Let's make some moves BD");
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/UserRequests/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
public class Parser {
public Parser() {
}

/**
* Checks the validity of the user request.
*
* @param name the user request.
* @return the appropriate task for the user request.
* @throws NicoleException if the request is unknown or in the wrong format.
*/
public static Task parseRequest(String name) throws NicoleException {
Pattern todoPattern = Pattern.compile("^todo(?: (.*?))?$");
Pattern deadlinePattern = Pattern.compile("^deadline(?: (.*?))?(?: by (\\d{4}-\\d{2}-\\d{2}))?$");
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/UserRequests/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
public class Request {
public static boolean priorityTasking = false;
private Task newTask;

/**
* Initialises a Request
*
* @param name the user request.
* @throws NicoleException if the Parser throws this exception due to issues
* with the request.
* @throws IOException if the TaskList throws this exception due to issues with
* posting and retrieving data from hard drive.
*/
public Request(String name) throws NicoleException, IOException {
if (name.equals("list") ||
name.contains("mark") ||
Expand All @@ -24,7 +34,7 @@ public Request(String name) throws NicoleException, IOException {
this.handleRequest(name);
}

private void handleRequest(String name) throws NicoleException, IOException {
private void handleRequest(String name) throws NicoleException {
TaskList taskList = new TaskList();
if (name.contains("unmark")) {
int taskNumber = Integer.parseInt(name.substring(7));
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/UserRequests/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@

public class Ui {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Ui() {

/**
* Initialises Ui and executes the user interaction loop.
*
* @throws IOException if Request throws this exception due to issues with
* posting and retrieving data from hard drive.
*/
public Ui() throws IOException {
System.out.println(this.greet());
this.talkToUser();
}
public String talkToUser() throws IOException {

private void talkToUser() throws IOException {
String request = br.readLine();
while (request != null && !request.equals("bye")) {
try {
Expand All @@ -24,7 +33,6 @@ public String talkToUser() throws IOException {
}
request = br.readLine();
}
return this.exit();
}
private String greet() {
DateTimeFormatter digitalTime = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Expand Down

0 comments on commit 220128e

Please sign in to comment.