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

Branch j unit #21

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e873b56
Level-1: Greet , Echo , Exit [Completed]
qjie7 Aug 28, 2019
1549455
Level-2: Add , List [Completed]
qjie7 Aug 28, 2019
e96fb56
Level-3: Mark as Done + A-Classes [Completed]
qjie7 Aug 28, 2019
6d44d50
Level-3: Improvement on code organization by introducing a new class …
qjie7 Aug 28, 2019
d3b4e14
Level-4: ToDos, Events, Deadlines [Completed]
qjie7 Aug 28, 2019
c59e1b6
Level-4: ToDos, Events, Deadlines + A-Inheritance [Completed]
qjie7 Aug 28, 2019
2d4e494
Merge branch 'master' of https://github.com/qjie7/duke
qjie7 Aug 28, 2019
cd3c533
Level-5: Handle Errors (Minimal Requirements) [Completed]
qjie7 Aug 28, 2019
05b03be
Level-7: Save [Completed]
qjie7 Aug 28, 2019
f86d020
Level 7: Save [Bugfixed as output will show deadline and event in the…
qjie7 Aug 28, 2019
df5d845
Level-8: Dates And Times [Bug unsolved]
qjie7 Aug 28, 2019
bc5bcd3
Reorganised Code & Implementation logic until [Level-4 : ToDos, Event…
qjie7 Aug 30, 2019
8deb4a4
Continue on re-implementation on Level-5: Handle Errors. Removed enum…
qjie7 Aug 31, 2019
3ce4531
Introduce DukeTools class to consolidate all duke's function
qjie7 Aug 31, 2019
26c2bd6
Continue on re-implementation on Level-7: Save(write file done) and a…
qjie7 Aug 31, 2019
305d8c2
Manage to load data from the duke.txt file correctly.
qjie7 Aug 31, 2019
a9deb34
Re-implemented on Level-7: Save [Completed] , Now able to save and lo…
qjie7 Sep 1, 2019
e9e25e1
Level-8: Dates and Times [Completed]
qjie7 Sep 2, 2019
73495f0
Merged Branch
qjie7 Sep 2, 2019
8b7bf35
Merge commit 'e9e25e16805a27ef63980e0da64aac217644c1e2'
qjie7 Sep 2, 2019
9f0a538
Level-6: Delete [Completed]
qjie7 Sep 2, 2019
605e64d
Level-9: Find (Completed)
qjie7 Sep 2, 2019
dfa851f
Merge branch 'branch-Level-9'
qjie7 Sep 2, 2019
b5f7025
added Ui Class
qjie7 Sep 2, 2019
1747388
added Parser class
qjie7 Sep 2, 2019
c557615
added TaskList class
qjie7 Sep 3, 2019
f9cec82
added Storage class + A-MoreOOP (Minimal) [Completed]
qjie7 Sep 3, 2019
fb3e8fb
A-TextUiTesting: Text UI Testing [Completed]
qjie7 Sep 4, 2019
ebb8264
A-JUnit : JUnit Testing [Completed]
qjie7 Sep 7, 2019
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
95 changes: 89 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,93 @@
import java.text.ParseException;

public class Duke {
private Storage storage;
private Parser parser;
private TaskList tasks;
private Ui ui;

public Duke() {
ui = new Ui();
storage = new Storage();
parser = new Parser();
tasks = new TaskList(storage.load());
}

public void run() {
ui.showWelcome();
String day;

String userInput = ui.readUserInput();

while(!userInput.equals("bye")) {
String[] firstBox = parser.firstFilter(userInput); // return a string array with commands and description
String commands = firstBox[0];
try {
switch (commands) {
case "list":
ui.showTaskList(tasks.getTaskList());
break;

case "done":
tasks.runDone(userInput);
storage.save();
break;

case "delete":
tasks.runDelete(userInput);
storage.save();
break;

case "find":
tasks.runFind(userInput);
break;

case "todo":
try {
if (firstBox[1].isBlank()){
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}else {
tasks.runToDo(firstBox[1]);
}
}catch(DukeException e) {
e.printStackTrace();
}finally{
storage.save();
break;
}

case "deadline":
String[] secondBox = parser.secondFilter(firstBox[1] , "deadline");
day = parser.dayExtractor(secondBox);
String timeInString = parser.timeFormatter(secondBox[1].trim() , day);
tasks.runDeadline(secondBox[0] , timeInString);
storage.save();
break;

case "event":
secondBox = parser.secondFilter(firstBox[1] , "event");
timeInString = parser.timeFormatter(secondBox[1] , parser.dayExtractor(secondBox));
tasks.runEvent(secondBox[0] , timeInString);
storage.save();
break;

default:
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}


}catch(DukeException | ParseException e) {
e.printStackTrace();
}finally {
userInput = ui.readUserInput().trim();
}

}
ui.showExit();
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
new Duke().run();

}
}
6 changes: 6 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class DukeException extends Exception {
public DukeException(){}
public DukeException(String msg){
super(msg);
}
}
94 changes: 94 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import duke.Deadline;
import duke.Event;
import duke.Task;
import duke.ToDo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Parser {
private static final String WHITESPACE = " ";

public void loadParse(ArrayList<Task> taskList , String input) {
String[] arr = input.split(" \\| ");
String taskType = arr[0].trim();
String taskStatus = arr[1].trim();
String taskDescription = arr[2].trim();
String taskTime = "";

if (taskType.equals("T")) {
Task todo = new ToDo(taskDescription);
if (taskStatus.equals("\u2713")) {
todo.markAsDone();
}
taskList.add(todo);
}else if (taskType.equals("D")) {
taskTime = arr[3].trim();
Task deadline = new Deadline(taskDescription , taskTime);
if (taskStatus.equals("\u2713")) {
deadline.markAsDone();
}
taskList.add(deadline);
}else {
taskTime = arr[3].trim();
Task event = new Event(taskDescription , taskTime);
if (taskStatus.equals("\u2713")) {
event.markAsDone();
}
taskList.add(event);
}
}

public String dayExtractor(String[] secondBox) {
String day = secondBox[1].trim().split("/")[0];
return day;
}

public String timeFormatter(String dateInString , String day) throws ParseException {
DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm");
DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha");
DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha");
DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha");
DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha");

String output;

Date convertedDate = parser.parse(dateInString);
if (day.equals("1")){
output = stFormatter.format(convertedDate);
}else if (day.equals("2")) {
output = ndFormatter.format(convertedDate);
}else if (day.equals("3")) {
output = rdFormatter.format(convertedDate);
}else{
output = thFormatter.format(convertedDate);
}
return output;
}

public String[] firstFilter(String userInput) {
String[] filter = userInput.split(" ");
String firstWord = filter[0];
StringBuilder str = new StringBuilder();
for (int i = 1 ; i < filter.length ; i++) {
str.append(filter[i]);
str.append(WHITESPACE);
}
String[] result = {firstWord , str.toString().trim()};

return result;
}

public String[] secondFilter(String newString , String firstWord) {
String[] filter;
if (firstWord.equals("deadline")) {
filter = newString.split(" /by ");
}else{
filter = newString.split(" /at ");
}
return filter;
}
}
96 changes: 96 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import duke.Task;

import java.io.*;
import java.util.ArrayList;

public class Storage {

private ArrayList<Task> taskList = new ArrayList<>();
Parser parser = new Parser();

public ArrayList<Task> load() {
File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt");
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);

String input = bufferedReader.readLine();

while (input != null) {
parser.loadParse(taskList , input);
input = bufferedReader.readLine();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileReader != null) {
fileReader.close();
}
}catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
}catch (IOException e) {
e.printStackTrace();
}
return taskList;

}
}

public void save () {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try{
File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt");
fileWriter = new FileWriter(file , false);
bufferedWriter = new BufferedWriter(fileWriter);
for (Task t : taskList) {
String taskType = t.getType();
String taskStatus = t.getStatusIcon();
String taskDescription = t.getDescription();
String taskTime = t.getTime();

if (taskType.equals("T")) {
bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription);
bufferedWriter.newLine();

}else{
bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime);
bufferedWriter.newLine();
}


bufferedWriter.flush();
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWriter != null) {
fileWriter.close();
}
}catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}

}
}
81 changes: 81 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import duke.Deadline;
import duke.Event;
import duke.Task;
import duke.ToDo;

import java.util.ArrayList;

public class TaskList {
public TaskList() {}
public TaskList(ArrayList<Task> taskList) {
this.taskList = taskList;
}

private ArrayList<Task> taskList = new ArrayList<>();
Ui ui = new Ui();
//Storage storage = new Storage();

public ArrayList<Task> getTaskList() {
return taskList;
}

public void runDone(String userInput) {
int index = Integer.parseInt(userInput.split(" ")[1]);
Task chosenTask = taskList.get(index - 1);
chosenTask.markAsDone();
ui.showDone(taskList , index);
//storage.save();
}

public void runDelete(String userInput) {
int index = Integer.parseInt(userInput.split(" ")[1]);
ui.showDelete(taskList , index);
taskList.remove(index - 1);
ui.showTaskListSize(taskList);
//storage.save();
}

public void runFind(String userInput) {
int index = 0 ;
ArrayList<Task> searchResults = new ArrayList<>();
String keyword = userInput.split(" ")[1];
for (Task task : taskList) {
String description = task.getDescription();
if (description.contains(keyword)) {
searchResults.add(task);
}
}
ui.showMatchTasks(searchResults , index);
}

public void runToDo(String description) {
Task toDoTask = new ToDo(description);
taskList.add(toDoTask);
ui.showAddTodoTask(taskList , toDoTask);
//storage.save();
}

public void runDeadline(String description , String time) {

Task deadlineTask = new Deadline(description , time);
taskList.add(deadlineTask);
ui.showAddDeadlineTask(taskList , deadlineTask);
//storage.save();
}

public void runEvent(String description , String time) {
Task eventTask = new Event(description , time);
taskList.add(eventTask);
ui.showAddEventTask(taskList , eventTask);
//storage.save();
}









}
Loading