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

Fixed bugs for deletereceipt, datelist and percent commands #267

Merged
2 commits merged into from
Nov 8, 2019
Merged
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
9 changes: 1 addition & 8 deletions savedWallet.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@

1000.0
out $250.0 /date 2019-11-07 /tags
out $100.0 /date 2019-11-07 /tags
in $100.0 /date 2019-11-07 /tags
out $5.0 /date 2019-11-07 /tags food
out $-0.0 /date 2019-11-07 /tags

0.0
25 changes: 24 additions & 1 deletion src/main/java/executor/command/CommandDateList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import duke.exception.DukeException;
import interpreter.Parser;
import storage.StorageManager;
import java.time.format.DateTimeParseException;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class CommandDateList extends Command {
private String date;

/**
* Constructor for CommandDateList subCommand Class.
*
* @param userInput String is the user input from the CLI
*/
public CommandDateList(String userInput) {
Expand All @@ -24,6 +27,11 @@ public CommandDateList(String userInput) {
@Override
public void execute(StorageManager storageManager) {
String outputStr = "You have the following receipts for" + " " + date + "\n";
if (!isDate(this.date)) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Invalid date input. FORMAT : datelist yyyy-mm-dd");
return;
}
try {
outputStr += storageManager.getReceiptsByDate(this.date).getPrintableReceipts();
} catch (DukeException e) {
Expand All @@ -34,5 +42,20 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr(outputStr);
}

/**
* Boolean function for checking date format of input.
*
* @param dateString String is date input from the CLI
*/
public boolean isDate(String dateString) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
return dateFormat.parse(dateString) != null;
} catch (ParseException e) {
return false;
}
}

}

2 changes: 1 addition & 1 deletion src/main/java/executor/command/CommandDelete.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setCodeToast();
this.infoCapsule.setOutputStr(outputStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
import interpreter.Parser;
import storage.StorageManager;

public class CommandReceiptDelete extends Command {
import java.lang.reflect.InvocationTargetException;

public class CommandDeleteReceipt extends Command {
protected String userInput;

/**
* Constructor for CommandDeleteReceipt subCommand Class.
* @param userInput The user input from the CLI
*/
public CommandReceiptDelete(String userInput) {
public CommandDeleteReceipt(String userInput) {
super();
this.userInput = userInput;
this.description = "Deletes the specific entry that the user wants to remove. \n"
+ "FORMAT: receiptdelete <Index_of_Entry>";
this.commandType = CommandType.RECEIPTDELETE;
+ "FORMAT: deletereceipt <Index_of_Entry>";
this.commandType = CommandType.DELETERECEIPT;
}


Expand All @@ -26,7 +28,7 @@ public void execute(StorageManager storageManager) {
String integer = Parser.parseForPrimaryInput(this.commandType, userInput);
if (integer.isEmpty()) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Index input is missing. FORMAT : receiptdelete <Index_of_Entry>");
this.infoCapsule.setOutputStr("Index input is missing. FORMAT : deletereceipt <Index_of_Entry>");
return;
}
try {
Expand All @@ -41,8 +43,14 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
return;
} catch (NumberFormatException e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Invalid index input. Please enter an integer");
return;
}
this.infoCapsule.setCodeToast();
this.infoCapsule.setOutputStr(outputStr);
}


}
5 changes: 5 additions & 0 deletions src/main/java/executor/command/CommandPercent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setOutputStr("Tag input is missing. FORMAT : percent <tag>");
return;
}
if (storageManager.getWallet().getReceipts().getPrintableReceipts().isEmpty()) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("No receipts found in the list");
return;
}
try {
Double totalTag = storageManager.getReceiptsByTag(this.tag).getTotalCashSpent();
Double totalSpent = storageManager.getWalletExpenses();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/executor/command/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum CommandType {
BLANK(CommandBlank.class),
FIND(CommandFind.class),
DELETE(CommandDelete.class),
RECEIPTDELETE(CommandReceiptDelete.class),
DELETERECEIPT(CommandDeleteReceipt.class),
DONE(CommandMarkDone.class),
QUEUE(CommandQueue.class),
VIEWSCHEDULE(CommandSchedule.class),
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/CommandDateListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ void execute() {
CommandDateList dateOne = new CommandDateList("datelist 2019-02-01");
dateOne.execute(storageManager);
String output = dateOne.getInfoCapsule().getOutputStr();

assertEquals("You have the following receipts for 2019-02-01\n1. [transport] 3.0 2019-02-01\n", output);

CommandDateList d1 = new CommandDateList("datelist 123cwv");
d1.execute(storageManager);
String result = d1.getInfoCapsule().getOutputStr();
assertEquals("Invalid date input. FORMAT : datelist yyyy-mm-dd", result);

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import executor.command.CommandDelete;
import executor.command.CommandDeleteReceipt;
import executor.command.CommandPercent;
import executor.command.CommandReceiptDelete;
import org.junit.jupiter.api.Test;
import storage.StorageManager;
import ui.Receipt;
Expand All @@ -8,7 +9,7 @@

import java.time.LocalDate;

public class CommandReceiptDeleteTest {
public class CommandDeleteReceiptTest {
@Test
void execute() {
StorageManager storageManager = new StorageManager();
Expand All @@ -18,15 +19,20 @@ void execute() {
receiptOne.setDate(LocalDate.parse("2019-02-01"));
storageManager.getWallet().addReceipt(receiptOne);

CommandReceiptDelete d1 = new CommandReceiptDelete("receiptdelete 1");
CommandDeleteReceipt d1 = new CommandDeleteReceipt("deletereceipt 1");
d1.execute(storageManager);
String output = d1.getInfoCapsule().getOutputStr();
assertEquals("Receipt 1 has been deleted\n", output);

CommandReceiptDelete d2 = new CommandReceiptDelete("receiptdelete");
CommandDeleteReceipt d2 = new CommandDeleteReceipt("deletereceipt");
d2.execute(storageManager);
String result = d2.getInfoCapsule().getOutputStr();
assertEquals("Index input is missing. FORMAT : receiptdelete <Index_of_Entry>", result);
assertEquals("Index input is missing. FORMAT : deletereceipt <Index_of_Entry>", result);

CommandDeleteReceipt d3 = new CommandDeleteReceipt("deletereceipt 2.0f");
d3.execute(storageManager);
String out = d3.getInfoCapsule().getOutputStr();
assertEquals("Invalid index input. Please enter an integer", out);


}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/CommandPercentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void execute() {
percentTwo.execute(storageManager);
String result = percentTwo.getInfoCapsule().getOutputStr();
assertEquals("Tag input is missing. FORMAT : percent <tag>", result);

}

}
6 changes: 5 additions & 1 deletion src/test/java/CommandTagListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ void execute() {
CommandTagList dateOne = new CommandTagList("taglist food");
dateOne.execute(storageManager);
String output = dateOne.getInfoCapsule().getOutputStr();

assertEquals("You spent a total of $3.00 on food\n" + "1. [food] 3.0 2019-02-01\n\n", output);

CommandTagList d1 = new CommandTagList("taglist");
d1.execute(storageManager);
String result = d1.getInfoCapsule().getOutputStr();
assertEquals("Tag input is missing. FORMAT : taglist <tag>", result);
}

}