Skip to content

Commit

Permalink
- Revamped Tests to match
Browse files Browse the repository at this point in the history
 - Amended Checkstyle Violations

Todo:
 - Move TaskList, Wallet, ReceiptTracker, etc classes to Storage instead of Ui
Signed-off-by: Mudaafi <[email protected]>
  • Loading branch information
Mudaafi committed Nov 5, 2019
1 parent 03b41c6 commit c1c2937
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 95 deletions.
4 changes: 4 additions & 0 deletions src/main/java/executor/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public abstract class Command {
protected String description = "NO DESCRIPTION";

// Constructor

/**
* Base Constructor for all sub-classes to call super().
*/
public Command() {
this.infoCapsule = new InfoCapsule();
infoCapsule.setCodeError();
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/executor/command/CommandQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ public CommandQueue(String userInput) {

@Override
public void execute(StorageManager storageManager) {
String taskString = Parser.parseForFlag("task", this.userInput);
if (taskString == null) {
int mainTaskIndex;
try {
mainTaskIndex = Integer.parseInt(Parser.parseForPrimaryInput(this.commandType, this.userInput)) - 1;
} catch (Exception e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Please enter a valid task index. Use LIST to show all tasks.\n");
return;
}
String taskString = Parser.removeStr(this.commandType.toString(), this.userInput).trim();
taskString = Parser.removeStr(String.valueOf(mainTaskIndex + 1), taskString).trim();
taskString = Parser.removeStr("/task", taskString).trim();
if (taskString.equals("")) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("New Task to be queued was not specified.\n");
return;
Expand All @@ -38,12 +48,11 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setOutputStr("No Task detected after 'Queue'.");
return;
}
int mainTaskIndex = Integer.parseInt(Parser.parseForPrimaryInput(this.commandType, this.userInput));
storageManager.initializeQueueByIndex(mainTaskIndex);
String outputStr;
try {
storageManager.initializeQueueByIndex(mainTaskIndex);
storageManager.queueTaskByIndex(mainTaskIndex, newTaskCommandType, taskString);
outputStr = "New Task Queued after :" + storageManager.getTaskNameByIndex(mainTaskIndex);
outputStr = "New Task Queued after " + storageManager.getTaskNameByIndex(mainTaskIndex);
} catch (DukeException e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/executor/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ private void parseDateTime() {
if (this.detailDesc.equals("by")) {
try {
LocalDateTime dateTime = LocalDateTime.parse(this.taskDetails,
DateTimeFormatter.ofPattern("dd/MM/yy HH:mm"));
DateTimeFormatter.ofPattern("dd/MM/yy HHmm"));
LocalDate localDate = dateTime.toLocalDate();
LocalTime localTime = dateTime.toLocalTime();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yy");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmm");
this.setDate(localDate);
this.setTime(localTime);
System.out.println("Date Interpreted: "
Expand All @@ -39,7 +39,7 @@ private void parseDateTime() {
} catch (Exception e) {
this.setDate(LocalDate.now());
this.setTime(LocalTime.now());
System.out.println("Invalid Input. Unable to interpret Datetime (use: dd/MM/yy HH:mm) \n"
System.out.println("Invalid Input. Unable to interpret Datetime (use: dd/MM/yy HHmm) \n"
+ "So we have assigned this task to be deadline of today \n");
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/executor/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public String genTypeAcronym() {

/**
* Records the details from the user input into this Task object.
*
* @param userInput The input taken from the user in CLI
*/
public void recordTaskDetails(String userInput) {
Expand Down
75 changes: 63 additions & 12 deletions src/main/java/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class StorageManager {
private Wallet wallet = new Wallet();
private TaskList taskList = new TaskList();

/**
* Constructor for the StorageManager Object.
* @param taskPath String detailing the filepath to the stored Task Data
* @param walletPath String detailing the filepath to the stored Wallet Data
*/
public StorageManager(String taskPath, String walletPath) {
this.initializationStatus = "";
this.initializationStatus += this.loadTasks(taskPath);
Expand Down Expand Up @@ -63,6 +68,12 @@ public ReceiptTracker getReceiptsByDate(String date) throws DukeException {
}
}

/**
* Returns a TaskList of Task by a specific date.
* @param date LocalDate to be found
* @return TaskList containing all the Tasks matching date
* @throws DukeException Error when trying to access Tasks at the given date.
*/
public TaskList getTasksByDate(LocalDate date) throws DukeException {
TaskList filteredTaskList = new TaskList();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy");
Expand Down Expand Up @@ -108,6 +119,12 @@ public ReceiptTracker getReceiptsByYear(int year) throws DukeException {
}
}

/**
* Accessor method to ReceiptTracker's getReceiptByTag() method.
* @param tag String to be found in tag
* @return ReceiptTracker containing all the Receipts with tag
* @throws DukeException Error when trying to obtain all receipts with the tag
*/
public ReceiptTracker getReceiptsByTag(String tag) throws DukeException {
try {
return this.wallet.getReceipts().getReceiptsByTag(tag);
Expand Down Expand Up @@ -186,24 +203,27 @@ public TaskList getTasksByName(String name) throws DukeException {
}
}

/**
* Accessor Method to ReceiptTracker's getPrintableReceipts() method.
* @return String detailing all the Receipts to be printed to the User
*/
public String getPrintableReceipts() {
return this.wallet.getReceipts().getPrintableReceipts();
}

/**
* Accessor Method to TaskList's getPrintableTasks() method.
* @return String detailing all the Tasks to be printed to the User.
*/
public String getPrintableTasks() {
return this.taskList.getPrintableTasks();
}

public String getPrintableTaskByIndex(int index) throws DukeException {
try {
return getPrintableTaskByIndex(index);
} catch (ArrayIndexOutOfBoundsException e) {
throw new DukeException("The TaskList does not contain that Task index.\n");
} catch (IndexOutOfBoundsException e) {
throw new DukeException("TaskList does not contain that Task index.\n");
}
}

/**
* Accessor Method to Task's markDone Method.
* @param index int representing the index of the Task to mark done
* @throws DukeException Error when trying to Mark a Task as Done
*/
public void markTaskDoneByIndex(int index) throws DukeException {
try {
this.taskList.get(index).markDone();
Expand All @@ -212,6 +232,11 @@ public void markTaskDoneByIndex(int index) throws DukeException {
}
}

/**
* Loads all queued Tasks in particular indexed-Task.
* @param index int representing the index of the Task to load all the queuedTasks from
* @throws DukeException Error when trying to load queued Tasks.
*/
public void loadQueuedTasksByIndex(int index) throws DukeException {
try {
this.taskList.loadQueuedTasks(this.taskList.get(index));
Expand All @@ -220,6 +245,12 @@ public void loadQueuedTasksByIndex(int index) throws DukeException {
}
}

/**
* Creates a Task and adds it to the TaskList.
* @param taskType TaskType of the Task to be created.
* @param userInput String with all the Task details
* @throws DukeException Error when trying to add a task into the TaskList
*/
public void createTask(TaskType taskType, String userInput) throws DukeException {
try {
this.taskList.addTask(TaskList.createTask(taskType, userInput));
Expand All @@ -230,14 +261,34 @@ public void createTask(TaskType taskType, String userInput) throws DukeException
}
}

/**
* Returns a String detailing the most recent Task appended to the TaskList.
* @return String detailing the most recently appended Task
*/
public String getPrintableLatestTask() {
return this.taskList.getMostRecentTaskAdded().genTaskDesc();
}

public void initializeQueueByIndex(int index) {
this.taskList.get(index).initializeQueue();
/**
* Initializes a queuedTask List of a Task given its index.
* @param index int representing the index of the Task in question
* @throws DukeException Error when trying to access a Task outside the Array Size
*/
public void initializeQueueByIndex(int index) throws DukeException {
try {
this.taskList.get(index).initializeQueue();
} catch (IndexOutOfBoundsException e) {
throw new DukeException("No task found at Index: " + index + "\n");
}
}

/**
* Creates and queues a Task to an existing Task given its index.
* @param index int representing the index of the Task to queue the other Task on
* @param commandType CommandType of the Task to be created
* @param taskStr String with all the details of the Task to be created.
* @throws DukeException Error when trying to queue a task
*/
public void queueTaskByIndex(int index, CommandType commandType, String taskStr) throws DukeException {
try {
Task task = TaskList.createTask(TaskType.valueOf(commandType.toString()), taskStr);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/storage/StorageWallet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage;

import duke.exception.DukeException;
import executor.command.CommandAddIncomeReceipt;
import executor.command.CommandAddReceipt;
import executor.command.CommandAddSpendingReceipt;
import executor.command.CommandType;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Wallet loadData() throws DukeException {
if (loadedInput.equals("")) {
break;
}
wallet = parseAddReceiptFromStorageString(wallet, loadedInput);
parseAddReceiptFromStorageString(wallet, loadedInput);
}
} catch (Exception e) {
throw new DukeException("No Previously Saved Wallet Data.");
Expand All @@ -71,19 +72,18 @@ public Wallet loadData() throws DukeException {
* Converts saved String in StorageWallet to actual Receipt object and saves in Wallet Object.
* @param loadedInput The saved String to be converted
*/
private Wallet parseAddReceiptFromStorageString(Wallet wallet, String loadedInput) {
private void parseAddReceiptFromStorageString(Wallet wallet, String loadedInput) {
CommandType commandtype = Parser.parseForCommandType(loadedInput);
Receipt r = null;
if (commandtype == CommandType.OUT) {
CommandAddReceipt c = new CommandAddSpendingReceipt(loadedInput);
r = new Receipt(c.getCash(), c.getDate(), c.getTags());
} else if (commandtype == CommandType.IN) {
CommandAddReceipt c = new CommandAddSpendingReceipt(loadedInput);
CommandAddIncomeReceipt c = new CommandAddIncomeReceipt(loadedInput);
r = new IncomeReceipt(c.getCash(), c.getDate(), c.getTags());
}
if (r != null) {
wallet.addReceipt(r);
}
return wallet;
}
}
16 changes: 1 addition & 15 deletions src/test/java/CommandDisplayBalanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import executor.command.CommandDisplayBalance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import storage.StorageManager;
import ui.Wallet;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -32,20 +33,5 @@ private void endTextTracker() {

@Test
void executeTest() {
resetTextTracker();
assertEquals("", outContent.toString().trim());

resetTextTracker();
Wallet wallet = new Wallet();
Command c = new CommandDisplayBalance("");
c.execute(wallet);
assertEquals("Your Balance: $0.00", outContent.toString().trim());

resetTextTracker();
wallet.setBalance(500.0);
c.execute(wallet);
assertEquals("Your Balance: $500.00", outContent.toString().trim());

endTextTracker();
}
}
19 changes: 1 addition & 18 deletions src/test/java/CommandDisplayExpenditureTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import executor.command.Command;
import executor.command.CommandDisplayExpenditure;
import org.junit.jupiter.api.Test;
import storage.StorageManager;
import ui.Receipt;
import ui.Wallet;

Expand Down Expand Up @@ -31,23 +32,5 @@ private void endTextTracker() {

@Test
void execute() {
resetTextTracker();
assertEquals("", outContent.toString().trim());

resetTextTracker();
Command c = new CommandDisplayExpenditure("");
Wallet wallet = new Wallet();
c.execute(wallet);
assertEquals("Total Expenditure: $0.00", outContent.toString().trim());

resetTextTracker();
for (double x = 0.00; x < 11.0; ++x) {
Receipt receipt = new Receipt(x);
wallet.addReceipt(receipt);
}
c.execute(wallet);
assertEquals("Total Expenditure: $55.00", outContent.toString().trim());

endTextTracker();
}
}
22 changes: 16 additions & 6 deletions src/test/java/CommandMarkDoneTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import duke.exception.DukeException;
import executor.command.CommandMarkDone;
import executor.task.Task;
import executor.task.TaskList;
import executor.task.TaskType;
import org.junit.jupiter.api.Test;
import storage.StorageManager;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CommandMarkDoneTest {

@Test
void loadQueuedTasks() {
TaskList taskList = new TaskList();
Task mainTask = null;
Task queuedTask = null;
try {
mainTask = TaskList.createTask(TaskType.EVENT, "something/by somewhen");
queuedTask = TaskList.createTask(TaskType.DEADLINE, "Something Else / rly");
} catch (DukeException e) {
System.out.println(e.getMessage());
}
assert mainTask != null;
assert queuedTask != null;
TaskList queuedTaskList = new TaskList();
Task mainTask = TaskList.createTask(TaskType.EVENT,"something/by somewhen");
Task queuedTask = TaskList.createTask(TaskType.DEADLINE, "Something Else / rly");
queuedTaskList.addTask(queuedTask);
mainTask.setQueuedTasks(queuedTaskList);
taskList.addTask(mainTask);
StorageManager storageManager = new StorageManager();
storageManager.getTaskList().addTask(mainTask);
CommandMarkDone c = new CommandMarkDone("Done1");
c.execute(taskList);
c.execute(storageManager);

Task loadedTask = taskList.getList().get(1);
Task loadedTask = storageManager.getTaskList().get(1);
assertEquals(true, mainTask.getIsDone());
assertEquals(false, mainTask.isQueuedTasks());
assertEquals(TaskType.DEADLINE, loadedTask.getTaskType());
Expand Down
22 changes: 15 additions & 7 deletions src/test/java/CommandQueueTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import duke.exception.DukeException;
import executor.command.CommandQueue;
import executor.task.Task;
import executor.task.TaskList;
import executor.task.TaskType;
import org.junit.jupiter.api.Test;
import storage.StorageManager;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CommandQueueTest {

@Test
void execute() {
TaskList taskList = new TaskList();
Task testTask = TaskList.createTask(TaskType.EVENT,"something/by somewhen");
taskList.addTask(testTask);
CommandQueue testCommand = new CommandQueue("Queue 1 EventIce Cream Party / Tomorrow");
testCommand.execute(taskList);
Task testTask = null;
try {
testTask = TaskList.createTask(TaskType.EVENT, "something/by somewhen");
} catch (DukeException e) {
System.out.println(e.getMessage());
}
assert testTask != null;
StorageManager storageManager = new StorageManager();
storageManager.getTaskList().addTask(testTask);
CommandQueue testCommand = new CommandQueue("Queue 1 /task EventIce Cream Party / Tomorrow");
testCommand.execute(storageManager);

Task mainTask = taskList.getList().get(0);
Task mainTask = storageManager.getTaskList().get(0);
assertEquals(true, mainTask.isQueuedTasks());
Task queuedTask = mainTask.getQueuedTasks().getList().get(0);
Task queuedTask = mainTask.getQueuedTasks().get(0);
assertEquals(TaskType.EVENT, queuedTask.getTaskType());
assertEquals("Ice Cream Party", queuedTask.getTaskName());
assertEquals("", queuedTask.getDetailDesc());
Expand Down
Loading

0 comments on commit c1c2937

Please sign in to comment.