Skip to content

Commit

Permalink
Merge pull request #41 from Kishen271828/master
Browse files Browse the repository at this point in the history
Delete transaction implemented properly, Edit transaction functionality added
  • Loading branch information
Kishen271828 committed Mar 21, 2024
2 parents 8b6a8a7 + 653f4bf commit dda450d
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT

data/data.txt
..gitignore.un~
config/.idea/
/*.class
2 changes: 1 addition & 1 deletion src/main/java/command/AddOutflowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String execute(TransactionManager manager) {
String outflowCategory = null;

for (String part : commandParts) {
if (part.startsWith("n/")) {
if (part.startsWith("r/")) {
outflowName = part.substring(2);
} else if (part.startsWith("a/")) {
outflowAmount = Double.parseDouble(part.substring(2));
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/command/DeleteInflowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package command;

import financialtransactions.TransactionManager;

public class DeleteInflowCommand extends BaseCommand {
public DeleteInflowCommand(String[] commandParts) {
super(false, commandParts);
}

public String execute(TransactionManager manager) throws Exception {
String inflowIndex = null;
for (String part : commandParts) {
if (part.startsWith("i/")) {
inflowIndex = part.substring(2);
}
}
assert inflowIndex != null;
manager.removeInflow(Integer.parseInt(inflowIndex));
return "Ok. Inflow deleted";
}
}
21 changes: 21 additions & 0 deletions src/main/java/command/DeleteOutflowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package command;

import financialtransactions.TransactionManager;

public class DeleteOutflowCommand extends BaseCommand {
public DeleteOutflowCommand(String[] commandParts) {
super(false, commandParts);
}

public String execute(TransactionManager manager) throws Exception {
String outflowIndex = null;
for (String part : commandParts) {
if (part.startsWith("i/")) {
outflowIndex = part.substring(2);
}
}
assert outflowIndex != null;
manager.removeOutflow(Integer.parseInt(outflowIndex));
return "Ok. Outflow deleted";
}
}
41 changes: 41 additions & 0 deletions src/main/java/command/EditInflowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package command;

import financialtransactions.Inflow;
import financialtransactions.TransactionManager;

public class EditInflowCommand extends BaseCommand {
public EditInflowCommand(String[] commandParts) {
super(false, commandParts);
}

public String execute(TransactionManager manager) throws Exception {
int inflowIndex = -1;
String inflowName = null;
double inflowAmount = 0;
String inflowDate = null;
String inflowTime = null;
String inflowCategory = null;

for (String part : commandParts) {
if (part.startsWith("i/")) {
inflowIndex = Integer.parseInt(part.substring(2));
} else if (part.startsWith("n/")) {
inflowName = part.substring(2);
} else if (part.startsWith("a/")) {
inflowAmount = Double.parseDouble(part.substring(2));
} else if (part.startsWith("d/")) {
inflowDate = part.substring(2);
} else if (part.startsWith("t/")) {
inflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
inflowCategory = part.substring(2);
}
}

String inflowDateTime = inflowDate + " " + inflowTime;
Inflow updatedInflow = new Inflow(inflowName, inflowAmount, inflowDateTime);
updatedInflow.setCategory(Inflow.Category.valueOf(inflowCategory.toUpperCase()));
manager.editInflow(inflowIndex, updatedInflow);
return "Ok. Edited inflow";
}
}
41 changes: 41 additions & 0 deletions src/main/java/command/EditOutflowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package command;

import financialtransactions.Outflow;
import financialtransactions.TransactionManager;

public class EditOutflowCommand extends BaseCommand {
public EditOutflowCommand(String[] commandParts) {
super(false, commandParts);
}

public String execute(TransactionManager manager) throws Exception {
int outflowIndex = -1;
String outflowName = null;
double outflowAmount = 0.0;
String outflowDate = null;
String outflowTime = null;
String outflowCategory = null;

for (String part : commandParts) {
if (part.startsWith("i/")) {
outflowIndex = Integer.parseInt(part.substring(2));
} else if (part.startsWith("r/")) {
outflowName = part.substring(2);
} else if (part.startsWith("a/")) {
outflowAmount = Double.parseDouble(part.substring(2));
} else if (part.startsWith("d/")) {
outflowDate = part.substring(2);
} else if (part.startsWith("t/")) {
outflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
outflowCategory = part.substring(2);
}
}

String outflowDateTime = outflowDate + " " + outflowTime;
Outflow updatedOutflow = new Outflow(outflowName, outflowAmount, outflowDateTime);
updatedOutflow.setCategory(Outflow.Category.valueOf(outflowCategory.toUpperCase()));
manager.editOutflow(outflowIndex, updatedOutflow);
return "Ok. Edited outflow";
}
}
3 changes: 1 addition & 2 deletions src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {
manager = storage.loadFile();

UI ui = new UI();
ui.printMessage("Welcome. In order to login, type your command in the format:\nlogin u/USERNAME p/PASSWORD");
ui.printMessage("Welcome. Enter your username and password to login.");

Parser parser = new Parser();
BaseCommand baseCommand = null;
Expand Down Expand Up @@ -85,4 +85,3 @@ public static void main(String[] args) {
ui.closeScanner();
}
}

3 changes: 0 additions & 3 deletions src/main/java/financialtransactions/Outflow.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package financialtransactions;

public class Outflow extends Transaction<Outflow.Category> {
private static final double TAX_AMOUNT = 1.09;
public enum Category {
RENT, DEBT, SHOPPING, TREAT, EDUCATION, OTHER
}

public Outflow(String name, double amount, String date) {
super(name, -1.00 * amount, date);
//super.transactionType = "O";
super.amount *= TAX_AMOUNT;
}

public void setCategory(Category category) {
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/financialtransactions/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public T getNthTransaction(int n) throws Exception{
return this.transactionList.get(n);
}

public int getIndexOfParticularTransaction(T particularTransaction) {
return this.transactionList.indexOf(particularTransaction);
}

public boolean addTransaction(T newTransaction){
if (newTransaction != null){
transactionList.add(newTransaction);
Expand All @@ -28,16 +32,17 @@ public boolean addTransaction(T newTransaction){
return false;
}

public boolean removeTransactionIndex (int index){
printTransactionsSafeInfo();
if (index >= transactionList.size() || index < 0){
System.out.println("Invalid Index");
return false;
}
public boolean removeTransactionIndex (int index) throws Exception{
//printTransactionsSafeInfo();
transactionList.remove(index);
return true;
}

public boolean editTransactionIndex (int index, T transaction) throws Exception {
transactionList.set(index, transaction);
return true;
}

public double getBalance(){
double balance = 0.00;
for(Transaction<?> transaction : transactionList){
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/financialtransactions/TransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,42 @@ public boolean removeTransaction(int index) throws Exception{
if (transactionRemoved instanceof Inflow) {
return inflows.removeTransactionIndex(index);
}
if (transactionRemoved instanceof Outflow){
if (transactionRemoved instanceof Outflow) {
return outflows.removeTransactionIndex(index);
}
return false;
}

public boolean removeInflow(int index) throws Exception {
int numOfInflows = inflows.getTransactionListSize();
Transaction<?> transactionRemoved = inflows.getNthTransaction(numOfInflows - index);
transactionList.removeTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionRemoved));
return inflows.removeTransactionIndex(numOfInflows - index);
}

public boolean removeOutflow(int index) throws Exception {
int numOfOutflows = outflows.getTransactionListSize();
Transaction<?> transactionRemoved = outflows.getNthTransaction(numOfOutflows - index);
transactionList.removeTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionRemoved));
return outflows.removeTransactionIndex(numOfOutflows - index);
}

public boolean editInflow(int index, Transaction<?> updatedTransaction) throws Exception {
int numOfInflows = inflows.getTransactionListSize();
Transaction<?> transactionEdited = inflows.getNthTransaction(numOfInflows - index);
transactionList.editTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionEdited),
updatedTransaction);
return inflows.editTransactionIndex(numOfInflows - index, (Inflow) updatedTransaction);
}

public boolean editOutflow(int index, Transaction<?> updatedTransaction) throws Exception {
int numOfOutflows = outflows.getTransactionListSize();
Transaction<?> transactionEdited = outflows.getNthTransaction(numOfOutflows - index);
transactionList.editTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionEdited),
updatedTransaction);
return outflows.editTransactionIndex(numOfOutflows - index, (Outflow) updatedTransaction);
}

public double getTotalBalance() {
double inflowBalance = inflows.getBalance();
double outflowBalance = outflows.getBalance();
Expand All @@ -55,7 +85,7 @@ public String showLastNTransactions(int n) throws Exception{
throw new Exception("Invalid index");
}
int index = 1;
String returnedText = "Inflows:\nTransactions:";
String returnedText = "Inflows:\nTransactions:\n";
for (int i = listSize - 1; i > listSize - n - 1; i--) {
Transaction<?> transaction = transactionList.getNthTransaction(i);
if (transaction instanceof Inflow) {
Expand All @@ -65,7 +95,7 @@ public String showLastNTransactions(int n) throws Exception{
}

index = 1;
returnedText += "\nOutflows:\nTransactions:";
returnedText += "\nOutflows:\nTransactions:\n";
for (int i = listSize - 1; i > listSize - n - 1; i--) {
Transaction<?> transaction = transactionList.getNthTransaction(i);
if (transaction instanceof Outflow) {
Expand Down
46 changes: 33 additions & 13 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package parser;

import customexceptions.IncompletePromptException;
import command.AddInflowCommand;
import command.AddOutflowCommand;
import command.BaseCommand;
import command.DeleteInflowCommand;
import command.DeleteOutflowCommand;
import command.EditInflowCommand;
import command.EditOutflowCommand;
import command.ExitCommand;
import command.ViewHistoryCommand;
import customexceptions.IncompletePromptException;
import financialtransactions.TransactionManager;
import userinterface.UI;

Expand All @@ -15,42 +19,58 @@ public class Parser {

public Parser() {
this.ui = new UI();
this.isContinue = true;
}

public BaseCommand parseCommand(String command, TransactionManager manager) throws
Exception {
public BaseCommand parseCommand(String command, TransactionManager manager) throws Exception {
String[] commandParts = command.split("\\s+");
String action = commandParts[0];
switch (action) {
case "help":
//implement help command
break;
case "add-inflow":
if (commandParts.length < 3) {
if (commandParts.length < 6) {
throw new IncompletePromptException(command);
}
return new AddInflowCommand(commandParts);
case "add-outflow":
if (commandParts.length < 3) {
if (commandParts.length < 6) {
throw new IncompletePromptException(command);
}
return new AddOutflowCommand(commandParts);
case "delete-inflow":
if (commandParts.length < 2) {
throw new IncompletePromptException(command);
}
return new DeleteInflowCommand(commandParts);
case "delete-outflow":
if (commandParts.length < 2) {
throw new IncompletePromptException(command);
}
String index = commandParts[1];
manager.removeTransaction(Integer.parseInt(index));
break;
return new DeleteOutflowCommand(commandParts);
case "edit-inflow":
if (commandParts.length < 7) {
throw new IncompletePromptException(command);
}
return new EditInflowCommand(commandParts);
case "edit-outflow":
if (commandParts.length < 7) {
throw new IncompletePromptException(command);
}
return new EditOutflowCommand(commandParts);
case "view-history":
if (commandParts.length < 2) {
throw new IncompletePromptException(command);
}
return new ViewHistoryCommand(commandParts);
case "quit":
this.isContinue = false;
return new ExitCommand(commandParts);
default:
throw new IncompletePromptException(command);
throw new Exception("Invalid command");
}
throw new Exception("Error parsing");
return null;
}

public void setIsContinue(boolean isContinue) {
this.isContinue = isContinue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public void toSaveTest() {
managerTest.addTransaction(shopping);

assertEquals("Salary payment|400.00|May 23 2022 07:00PM|INCOME\n" +
"Shopping|-218.00|May 23 2022 08:00PM|SHOPPING\n", managerTest.toSave());
"Shopping|-200.00|May 23 2022 08:00PM|SHOPPING\n", managerTest.toSave());
}
}
Loading

0 comments on commit dda450d

Please sign in to comment.