Skip to content

Commit

Permalink
Edit javadoc and improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
rexkoh425 committed Oct 6, 2024
1 parent 95a2761 commit a282b79
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 152 deletions.
6 changes: 0 additions & 6 deletions Data/TaskContents.txt

This file was deleted.

8 changes: 3 additions & 5 deletions src/main/java/TheThinker/Command/TheThinker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package TheThinker.Command;

import TheThinker.File.FileLoader;
import TheThinker.File.NewFile;
import TheThinker.Ui.CommandLine;
import TheThinker.Ui.UiControl;
import java.io.FileNotFoundException;
Expand All @@ -18,14 +17,13 @@ public class TheThinker{
public static void main(String[] args) {

try {
boolean isSaveTaskToFile = CommandLine.getSaveTaskInput();
NewFile data = new NewFile("");
boolean isSaveTaskToFile = FileLoader.getSaveTaskInput();
if(isSaveTaskToFile){
data = FileLoader.loadDefaultFileElseInputNewFile();
FileLoader.loadDefaultFileElseInputNewFile();
}

UiControl.printGreeting();
CommandLine.pollForUserInputTillBye(data , isSaveTaskToFile);
CommandLine.pollForUserInputTillBye(isSaveTaskToFile);

} catch (FileNotFoundException e) {
System.out.println("Please create file before proceeding");
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/TheThinker/File/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,46 @@
/**
* Handles file loading operations when starting the program
*/
public interface FileLoader {
public class FileLoader {

String FILE_DIR = "Data";
public static String FILE_DIR = "Data";
public static NewFile inputFile = new NewFile("TaskContents.txt");

/**
* Loads default file name and path and check if it exists.
* If file exists , load the contents of the file.
* If file does not exist , ask user for another file name.
* Loads default filepath and check if it exists.
* If file does not exist , create a new file based on user input
*
* @return NewFile object.
* @throws FileNotFoundException If the filepath is invalid.
* @throws FileNotFoundException If file creation failed.
*/
static NewFile loadDefaultFileElseInputNewFile() throws FileNotFoundException {
public static void loadDefaultFileElseInputNewFile() throws FileNotFoundException {
UiControl.printLoadingText();
createFolderIfNotExist();

NewFile data = new NewFile("TaskContents.txt");

if(!data.isFileExist()) {
if(!inputFile.isFileExist()) {
try {
data = inputFileNameAndCreateFile();
inputFileNameAndCreateFile();
} catch (IOException e) {
System.out.println("File not found. No data loaded. Please create the file under Data directory");
throw new FileNotFoundException();
}
}else{
data.loadFile();
inputFile.loadFile();
}
return data;
}

/**
* Gets user input for file name they want to load and load the file.
* Gets user input for file name and create the file.
*
* @return NewFile object.
* @throws FileNotFoundException If file name they input does not exist under /Data directory.
*/
private static NewFile inputFileNameAndCreateFile() throws IOException {
private static void inputFileNameAndCreateFile() throws IOException {
UiControl.printSeparation();
System.out.println("Input file name you want to save data to under the Data directory [filename] without .txt");
UiControl.printSeparation();
String filename = UserInputParser.getUserInput();
NewFile newFile = new NewFile(filename + ".txt");
inputFile = newFile;
createNewFile(newFile.file);
return newFile;
}

private static void createNewFile(File filename){
Expand Down Expand Up @@ -87,4 +82,10 @@ private static void createFolder(File directoryName){
System.out.println(directoryName + " directory already exists or failed to create.");
}
}

public static boolean getSaveTaskInput() {
System.out.println("Do you want to save your tasks to a file? yes / no");
String userInput = UserInputParser.getUserInput();
return userInput.equalsIgnoreCase("yes");
}
}
23 changes: 10 additions & 13 deletions src/main/java/TheThinker/File/NewFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.Scanner;
import java.io.IOException;

/**
* NewFile class handles file related operations
*/
public class NewFile {

public File file;
Expand All @@ -27,7 +30,7 @@ public boolean isFileExist() {
}

/**
* Reads the contents of the file and adds the task to current task list.
* Read content of a file and adds all task to current task list.
*
* @throws FileNotFoundException If the filepath is invalid.
*/
Expand All @@ -46,13 +49,11 @@ public void loadFile() throws FileNotFoundException {
}

/**
* Adds task without reprinting the task to the user.
* Adds task to task list without printing anything to the console.
* If task type is not T or D or E , the task will be ignored.
*
* @param parameters A string array containing parameters to initialise different task.
* Task : [task type , task description].
* Deadline : [task type , task description , deadline].
* Event : [task type , task description , start time , end time].
* @param parameters A string array containing values to initialise different task.
*
*/
private void addTaskAccordingToFileData(String[] parameters){
switch (parameters[0]) {
Expand Down Expand Up @@ -83,24 +84,20 @@ private void addTaskAccordingToFileData(String[] parameters){
}

/**
*
* Converts all tasks in task list to file format and writes to file.
*
* @throws IOException If the filepath is invalid.
*/
public void writeTaskToFile() throws IOException {
public void writeTaskListToFile() throws IOException {
FileWriter fw = new FileWriter(this.file);
String textToAdd = convertTaskListToString();
fw.write(textToAdd);
fw.close();
}

/**
* Returns a string of task list in file format with
* Each task member variable is seperated by "|" and each task seperated by "/n".
* Converts task list into a load file format
* If TaskList is empty , an empty string will be returned.
*
* @return A single string of task list in file format with "/n" included.
* @return A single string of task list in file format.
*/
private String convertTaskListToString(){
StringBuilder TaskListString = new StringBuilder();
Expand Down
74 changes: 27 additions & 47 deletions src/main/java/TheThinker/Parser/DateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,69 +59,49 @@ private static int isValidDay(String day) {
}

/**
* Returns boolean if day , month and year of both parameters are same , ignoring time.
* Compares date according to the deadline of each task types.
*
* @param task Task to compare
* @param specifiedDate date in format of dd/MM/yyyy.
* @param taskDate date in format of dd MMMM yyyy , h a.
* @return boolean
*
*/
private static boolean isMatchingDateOfResultDateTime(String specifiedDate , String taskDate) {
if(isResultDateTimeFormat(taskDate)) {
LocalDate parsedDate = LocalDate.parse(specifiedDate, dateFormatter);
LocalDateTime parsedDateTime = LocalDateTime.parse(taskDate, resultDateTimeFormatter);
return parsedDate.getMonthValue() == parsedDateTime.getMonthValue() &&
parsedDate.getDayOfMonth() == parsedDateTime.getDayOfMonth() &&
parsedDate.getYear() == parsedDateTime.getYear();
public static boolean isMatchingDateByType(Task task , String specifiedDate){

if((task.getTaskType() == 'D' || task.getTaskType() == 'E')){

return isMatchingDate(task.getTaskDate() , specifiedDate);
}
return false;
}

private static boolean isMatchingDateOfDateTime(String specifiedDate , String taskDate) {
if(isDateTimeFormat(taskDate)) {
LocalDate parsedDate = LocalDate.parse(specifiedDate, dateFormatter);
LocalDateTime parsedDateTime = LocalDateTime.parse(taskDate, dateTimeFormatter);
return parsedDate.getMonthValue() == parsedDateTime.getMonthValue() &&
parsedDate.getDayOfMonth() == parsedDateTime.getDayOfMonth() &&
parsedDate.getYear() == parsedDateTime.getYear();
private static DateTimeFormatter getDateFormat(String date){
if(isDateOnlyFormat(date)) {
return dateFormatter;
}
return false;
if(isDateTimeFormat(date)) {
return dateTimeFormatter;
}
if(isResultDateTimeFormat(date)) {
return resultDateTimeFormatter;
}
return null;
}

private static boolean isMatchingDateOnly(String specifiedDate , String taskDate) {
if(isDateOnlyFormat(taskDate)) {
/**
* @param taskDate Deadline corresponding to the task
* @param specifiedDate Queried date in any format
*/
private static boolean isMatchingDate(String taskDate , String specifiedDate) {
DateTimeFormatter formatOfTaskDate = getDateFormat(taskDate);
if(formatOfTaskDate != null) {
LocalDate parsedSpecifiedDate = LocalDate.parse(specifiedDate, dateFormatter);
LocalDate parsedTaskDate = LocalDate.parse(taskDate, dateFormatter);
LocalDate parsedTaskDate = LocalDate.parse(taskDate, formatOfTaskDate);
return parsedSpecifiedDate.getMonthValue() == parsedTaskDate.getMonthValue() &&
parsedSpecifiedDate.getDayOfMonth() == parsedTaskDate.getDayOfMonth() &&
parsedSpecifiedDate.getYear() == parsedTaskDate.getYear();
}
return false;
}

/**
* Returns true if day , month and year matches according to deadlines by task type.
* If task type is not event or deadline, return false.
*
* @param task Object of type task and subclasses.
* @param specifiedDate date in format of dd/MM/yyyy.
* @return boolean
*/
public static boolean isMatchingDateByType(Task task , String specifiedDate){

if((task.getTaskType() == 'D' || task.getTaskType() == 'E')){

return isMatchingAnyDateFormat(task , specifiedDate);
}
return false;
}

private static boolean isMatchingAnyDateFormat(Task task , String specifiedDate){
return isMatchingDateOfResultDateTime(specifiedDate ,task.getTaskDate()) ||
isMatchingDateOfDateTime(specifiedDate ,task.getTaskDate()) ||
isMatchingDateOnly(specifiedDate ,task.getTaskDate());
}

public static boolean isDateOnlyFormat(String dateStr){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

Expand Down Expand Up @@ -154,10 +134,10 @@ public static boolean isResultDateTimeFormat(String dateStr){
}

/**
* Parse date after get command
* Parse date after get command and checks if queried date is of dd/mm/yyyy format
*
* @return date in dd/MM/yyyy format
* @throws FormattingException If date is missing or in the wrong format
* @throws FormattingException If date is missing or in the wrong format(i.e. not dd/mm/yyyy)
*/
public static String parseDateAfterGet() throws FormattingException{
int LENGTH_OF_GET = 3;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/TheThinker/Parser/DeadlineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import TheThinker.Tasks.Deadline;

public class DeadlineParser extends UserInputParser{

public static final int LENGTH_OF_SLASH_BY = 3;
public static final int LENGTH_OF_DEADLINE = 8;

public static final String DEADLINE_FORMAT = "Please follow format : deadline [task] /by [time]";

/**
* Parse user input based on the format of Deadline and use the result to create Deadline object.
* Parse user input based on the format of Deadline and create Deadline object.
*
* @return Deadline Object.
* @throws FormattingException If /by , task description , deadline is missing from user input
*/
public static Deadline parseDeadline() throws FormattingException{
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/TheThinker/Parser/EventParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class EventParser extends UserInputParser{
/**
* Parse user input based on the format of Event and use the result to create Event object.
*
* @return Event Object.
* @throws FormattingException If /from , /to , task description , start time and end time
* is missing from user input
* is missing from user input. If /to is in front of /from.
*/
public static Event parseEvent() throws FormattingException{

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/TheThinker/Parser/TodoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public class TodoParser extends UserInputParser{
public static final String TODO_FORMAT = "Please follow format : todo [task]";

/**
* Parse user input based on the format of To-do and use the result to create To-do object
* Parse user input based on the format of To-do and create To-do object
*
* @return To-do Object
* @throws FormattingException if task description is not provided in user input
*/
public static Task parseTodo() throws FormattingException {
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/TheThinker/Parser/UserInputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import java.util.Scanner;

/**
* Handles the parsing of input other than to-do , deadline , event and date related parsing.
*/
public class UserInputParser {

public static String userInput;
public static final Scanner scanner = new Scanner(System.in);


public static final String GET_FORMAT = "Please follow format : get [dd/mm/yyyy] or [dd/MMMM/yyyy] or [dd/MM/yyyy , HH am/pm]";
public static final String GET_FORMAT = "Please follow format : get [dd/mm/yyyy]";
public static final String FIND_FORMAT = "Please follow format : find [keyword]";

public static String getUserInput(){
Expand All @@ -25,33 +28,48 @@ public static String parseUserAction(){
}

/**
* Parses the task number after unmark or mark command
* Used to obtain task number after inputting commands that require task number
*
* @return Task number
* @throws NumberFormatException If task number string provided is not a number.
* @throws FormattingException If no task or multiple task number is provided.
*/
public static int parseNumberAfterTask() throws NumberFormatException , FormattingException{
String[] parsedInputs = userInput.split(" ");
public static int parseNumberAfterTask(String task) throws NumberFormatException , FormattingException{

if(parsedInputs.length == 1){
int indexOfTask = userInput.toLowerCase().indexOf(task);
String taskNumber = userInput.substring(indexOfTask + task.length()).trim();
String[] parsedInputs = taskNumber.split(" ");

if(taskNumber.isEmpty()){
throw new FormattingException("Task number is not indicated. Please follow format : mark/unmark [number]");
}

if(parsedInputs.length > 2){
if(parsedInputs.length >= 2){
throw new FormattingException("Multiple tasks to mark is indicated in a single command. Please mark them one by one.");
}

String numberToMark = parsedInputs[1].trim();
return Integer.parseInt(numberToMark);
return Integer.parseInt(taskNumber);
}

/**
* @return keyword after removing space on the side
* @throws FormattingException If no keyword is provided or keyword is more than one word
*/
public static String parseKeywordAfterFind() throws FormattingException{
String[] parsedInputs = userInput.split(" ");
if(parsedInputs.length != 2){
throw new FormattingException("keyword is missing. " + FIND_FORMAT);

final int LENGTH_OF_FIND = 4;
int indexOfTask = userInput.toLowerCase().indexOf("find");
String keyword = userInput.substring(indexOfTask + LENGTH_OF_FIND).trim();
String[] parsedInputs = keyword.split(" ");

if(keyword.isEmpty()){
throw new FormattingException("Keyword is missing. " + FIND_FORMAT);
}

if(parsedInputs.length >= 2){
throw new FormattingException("More than one word is provided. " + FIND_FORMAT);
}

return parsedInputs[1].trim();
return keyword;
}
}
Loading

0 comments on commit a282b79

Please sign in to comment.