Skip to content

Commit

Permalink
Merge pull request #234 from Araavinds/b-deleteReceipts
Browse files Browse the repository at this point in the history
New command to delete receipts
  • Loading branch information
Araavinds authored Nov 7, 2019
2 parents 1521720 + 7aedf8f commit 595c52f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/executor/command/CommandReceiptDelete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package executor.command;

import duke.exception.DukeException;
import interpreter.Parser;
import storage.StorageManager;

public class CommandReceiptDelete extends Command {
protected String userInput;

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


@Override
public void execute(StorageManager storageManager) {
String outputStr;
String integer = Parser.parseForPrimaryInput(this.commandType, userInput);
try {
int index = Integer.parseInt(integer) - 1;
outputStr = ("Receipt '"
+ (index + 1)
+ ") "
+ "Receipt at index"
+ index
+ " "
+ "'has been deleted. \n");
storageManager.deleteReceiptByIndex(index);
} catch (DukeException e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
return;
}
this.infoCapsule.setCodeToast();
this.infoCapsule.setOutputStr(outputStr);
}
}
1 change: 1 addition & 0 deletions src/main/java/executor/command/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum CommandType {
BLANK(CommandBlank.class),
FIND(CommandFind.class),
DELETE(CommandDelete.class),
RECEIPTDELETE(CommandReceiptDelete.class),
DONE(CommandMarkDone.class),
QUEUE(CommandQueue.class),
VIEWSCHEDULE(CommandSchedule.class),
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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
Expand Down Expand Up @@ -148,6 +149,21 @@ public void deleteTaskByIndex(int index) throws DukeException {
}
}

/**
* Deletes Receipt by Index in ReceiptTracker Object.
* Used in CommandDeleteReceipt
* @param index int of Receipt to be deleted
* @throws DukeException Error occured when trying to delete Receipt
*/
public void deleteReceiptByIndex(int index) throws DukeException {
try {
this.wallet.getReceipts().deleteReceiptsByIndex(index);
} catch (Exception e) {
throw new DukeException("Invalid 'receiptdelete' statement. "
+ "Please indicate the index of the receipt you wish to delete.\n");
}
}

/**
* Gets the taskName property of a Task by its index in a TaskList.
* Used in CommandDelete.
Expand All @@ -163,6 +179,7 @@ public String getTaskNameByIndex(int index) throws DukeException {
}
}


/**
* Gets the balance property of the Wallet Object.
* @return Double representing the balance property of the Wallet Object.
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ui/ReceiptTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ public Double getTotalCashSpent() {
return totalCashSpent;
}

/**
* Deletes a receipt via its index.
*
* @param index Index of the receipt to be deleted
*/
public void deleteReceiptsByIndex(int index) {
this.remove(index);
}

/**
* Prints all the receipts stored in the ReceiptTracker Object.
* @return String containing all the receipts to be printed to the User
Expand Down

0 comments on commit 595c52f

Please sign in to comment.