forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ShyamKrishna33/add-transactions
Create Add-Transaction Functionality
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
package financemanager; | ||
|
||
import parser.Parser; | ||
import transactions.Transaction; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class TransactionList { | ||
|
||
private ArrayList<Transaction> transactions; | ||
private Parser parser; | ||
|
||
public TransactionList() { | ||
// Initialise ArrayList in the constructor | ||
this.transactions = new ArrayList<>(); | ||
this.parser = new Parser(); | ||
} | ||
|
||
public ArrayList<Transaction> getTransactions() { | ||
return transactions; | ||
} | ||
|
||
private void addTransaction(Transaction t) { | ||
transactions.add(t); | ||
} | ||
|
||
public void processTransaction(String input) { | ||
addTransaction(parser.parseTransaction(input)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
package parser; | ||
|
||
import transactions.Transaction; | ||
|
||
public class Parser { | ||
|
||
public static final int ADD_COMMAND_INDEX = 3; | ||
|
||
public Transaction parseTransaction(String input) { | ||
String data = input.substring(ADD_COMMAND_INDEX + 1); | ||
String[] parseData = data.split("/"); | ||
String description = null; | ||
String date = null; | ||
String amount = null; | ||
String category = null; | ||
for(int i = 0; i < parseData.length; i++) { | ||
switch (parseData[i].trim()) { | ||
case "n": | ||
description = parseData[i + 1]; | ||
break; | ||
case "$": | ||
amount = parseData[i + 1]; | ||
break; | ||
case "d": | ||
date = parseData[i + 1]; | ||
break; | ||
case "c": | ||
category = parseData[i + 1]; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
assert amount != null; | ||
return new Transaction(description, Float.parseFloat(amount), date, category); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters