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

add DateTime functionality #329

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions src/main/java/DatedTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Optional;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.time.format.DateTimeFormatter;

public class DatedTask extends Task{
private Optional<LocalDate> date;
private String detail;

public DatedTask(String description, String date) {
super(description);
this.detail = date;
try {
CharSequence dateCharSeq = new StringBuffer(date.strip());
LocalDate localDate = LocalDate.parse(dateCharSeq);
this.date = Optional.of(localDate);
} catch (DateTimeParseException e) {
this.date = Optional.empty();
}
}

protected Optional<LocalDate> getBy() {
return date;
}

protected String getDateOrDetail() {
if (date.isEmpty()) {
return detail;
}
return date.get().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
}

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


public Deadline(String description, String deadline) {
super(description, deadline);

}

@Override
public String toString() {
return String.format("[D]%s (at: %s)", super.toString(),
super.getDateOrDetail());
}
}
42 changes: 36 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import java.util.Scanner;


public class Duke {
/**
* Main method which handles initial and closing interactions.
* @param args
*/
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

Output.introduceDuke();
Output.greetUser();

run();

Output.byeUser();
}


/**
* Method containing the continuous while loop that parses user inputs and handles interactions accordingly
* Also initialises the task list. Attempts to retrieve a stored task list, if any.
*/
private static void run() {

Scanner scan = new Scanner(System.in);

TaskManager manager = RetrieveList.retrieveList();

while(manager.parseInput(scan.nextLine())) {
Output.printBreak();
Output.printEmptyLine();
}

scan.close();
}



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


public Event(String description, String event) {
super(description, event);
}

@Override
public String toString() {
return String.format("[E]%s (at: %s)", super.toString(),
super.getDateOrDetail());
}

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

/**
* Prints a standard breakline meant to break up sections of text
*/
public static void printBreak() {
System.out.println(" ________________________________________________________");
}

/**
* Prints out the logo to introduce the program
*/
public static void introduceDuke() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
printBreak();
}

/**
* Standard greeting that indicates program start / ready to recieve user input
*/
public static void greetUser() {
System.out.println(" Hello! I'm Duke");
System.out.println(" What can I do for you?");
printBreak();
}

/**
* Standard farewell that marks end of program
*/
public static void byeUser() {
printBreak();
System.out.println(" Bye. Hope to see you again soon!");
printBreak();

}

/**
* Informs user of a caught error by providing a message with proper indentation/formating.
*/
public static void printError(String s) {
Output.printBreak();
System.out.println(" " + s);
}

public static void printEmptyLine() {
System.out.println("\n");
}

protected static void printDeletedTask(String taskString) {
System.out.println(String.format(" Noted. I've removed this task: \n %s", taskString));
}

protected static void printAddedTask(String taskString) {
System.out.println("Got it. I've added this task :");
System.out.println(" " + taskString);
}

protected static void printCompletedTask(String taskString) {
System.out.println("Nice! I've marked this task as done:");
System.out.println(" " + taskString);
}

protected static void printTaskList (String listString) {
System.out.println("Here are the tasks in your list:");
System.out.println(listString);
}

protected static void printMatches (String matchList) {
printBreak();
if (matchList.length() == 0) {
System.out.println(" Sorry! I couldn't find any matches.");
return;
}

System.out.println("Here are the matching tasks in your list:");
System.out.println(matchList);
}

protected static void printTaskCount(int count) {
System.out.println(String.format(" Now you have %s tasks in the list.", count));
}
}
78 changes: 78 additions & 0 deletions src/main/java/RetrieveList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;
import java.nio.file.Files;
import java.io.IOException;


public class RetrieveList {
private static int TASK_TYPE_INDEX = 1;
private static int TASK_STATUS_INDEX = 4;
private static int TASK_DESCRIPTION_INDEX = 7;


/**
* RetrieveList attempts to load the previously stored list in [user.home]/duke-data.txt
* @return TaskManager object with pre-loaded list or an empty one if stored list could not be retrieved.
*/
public static TaskManager retrieveList() {
try {
String home = System.getProperty("user.home");
Path path = Paths.get(home, "duke-data.txt");
return parseTxt(path);
} catch (InvalidPathException e) {
Output.printError("Seems like this is your first time using Duke.");
return new TaskManager();
}
}

private static TaskManager parseTxt(Path path) {
TaskList tasks = new TaskList();
try {
Files.lines(path).forEachOrdered(line -> {
char key = line.charAt(TASK_TYPE_INDEX);
Character statusChar = line.charAt(TASK_STATUS_INDEX);
String description = line.substring(TASK_DESCRIPTION_INDEX);

boolean status = false;
if (statusChar.equals('\u2713')) {
status = true;
}

switch (key) {
case 'T':
addTodo(tasks, description, status);
break;
case 'D':
addDeadline(tasks, description, status);
break;
case 'E':
addEvent(tasks, description, status);
}

});
return new TaskManager(tasks);
} catch (IOException e) {
return new TaskManager();
}

}

private static void addTodo(TaskList tasks, String description, boolean status) {
tasks.addTodo(description.strip(), status);
}

private static void addDeadline(TaskList tasks, String description, boolean status) {
String cleanString = description.replace(')', ' ').replace('(', ' ').replaceFirst("by", " ");
String[] inputs = cleanString.split(":", 2);
tasks.addDeadline(inputs[0].strip(), inputs[1].strip(), status);
}

private static void addEvent(TaskList tasks, String description, boolean status) {
// dev note: "repeated code"
String cleanString = description.replace(')', ' ').replace('(', ' ').replaceFirst("at", " ");
String[] inputs = cleanString.split(":", 2);
tasks.addEvent(inputs[0].strip(), inputs[1].strip(), status);
}

}
25 changes: 25 additions & 0 deletions src/main/java/SaveList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.nio.file.Path;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


public class SaveList {

protected static void saveListToFile(Path path, ArrayList<Task> list) {
try {
FileWriter writer = new FileWriter(path.toString());
list.forEach(x -> {
try {
writer.write(x.toString() + "\n");
} catch (IOException e) {
Output.printError("A task could not be saved: " + x.toString());
}
});
writer.close();
} catch (IOException e) {
Output.printError("Not able to open or close the filewriter");
}
}

}
9 changes: 9 additions & 0 deletions src/main/java/StringPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public final class StringPair {
public String first;
public String second;

public StringPair(String first, String second) {
this.first = first;
this.second = second;
}
}
33 changes: 33 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Task {
protected String description;
private boolean isComplete;

public Task(String description) {
this.description = description;
this.isComplete = false;
}

@Override
public String toString() {
return this.getCompletionStatusIcon() + " " + this.description;
}

protected boolean markAsComplete() {
this.isComplete = true;
return this.isComplete;
}

protected boolean checkIsComplete() {
return this.isComplete;
}

private String getCompletionStatusIcon() {
return (isComplete ? "[\u2713]" : "[\u2718]");
}

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


}
Loading