Skip to content

Commit

Permalink
Merge pull request #9 from sweijie24/branch-Listing
Browse files Browse the repository at this point in the history
Create lists for expenses and savings
  • Loading branch information
sweijie24 authored Mar 12, 2024
2 parents 9d13ed3 + 57d5000 commit e0471a6
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void handleCommands(String input) {

public void run() {
Scanner scanner = new Scanner(System.in);

ui.showWelcome();

boolean isExit = false;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/seedu/budgetbuddy/Expense.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package seedu.budgetbuddy;

import java.time.LocalDate;

public class Expense extends Transaction{
protected String description;
private LocalDate dateAdded;

public Expense(String category, int amount, String description) {
public Expense(String category, double amount, String description) {
super(category, amount);
this.description = description;
this.dateAdded = LocalDate.now();
}
public LocalDate getDateAdded() {
return dateAdded;
}

public String getDescription(){
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/seedu/budgetbuddy/ExpenseList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.budgetbuddy;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class ExpenseList {
Expand All @@ -13,6 +14,42 @@ public ExpenseList() {
"Groceries", "Utility", "Transport", "Entertainment", "Others"));
}

public List<Expense> getExpenses() {
return expenses;
}

public void listExpenses(String filterCategory) {
System.out.println("Expenses:");
for (int i = 0; i < expenses.size(); i++) {
Expense expense = expenses.get(i);
if (filterCategory == null || expense.getCategory().equalsIgnoreCase(filterCategory)) {
System.out.print(i+1 + " | ");
System.out.print("Date: " + expense.getDateAdded() + " | ");
System.out.print("Category: " + expense.getCategory() + " | ");
System.out.print("Amount: $" + expense.getAmount() + " | ");
System.out.println("Description: " + expense.getDescription() + " | ");
}
}
System.out.println("-----------------------------------------------------------------------------");
System.out.println("Total Expenses: $" + calculateTotalExpenses());

}

public double calculateTotalExpenses() {
double totalExpenses = 0;
for (Expense expense: expenses) {
if (expense.getAmount() < 0) {
try {
throw new Exception("Expenses should not be negative");
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
totalExpenses += expense.getAmount();
}
}
return totalExpenses;
}
public void addExpense(String category, String amount, String description) {
int amountInt = Integer.parseInt(amount);
Expense expense = new Expense(category, amountInt, description);
Expand Down Expand Up @@ -42,5 +79,4 @@ public void deleteExpense(int index){
System.out.println("Invalid expense index.");
}
}

}
42 changes: 41 additions & 1 deletion src/main/java/seedu/budgetbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import seedu.budgetbuddy.command.DeleteExpenseCommand;
import seedu.budgetbuddy.command.Command;
import seedu.budgetbuddy.command.MenuCommand;

import seedu.budgetbuddy.command.ListExpenseCommand;
import seedu.budgetbuddy.command.ListSavingsCommand;

public class Parser {

Expand All @@ -25,6 +26,10 @@ private String extractDetailsForAdd(String details, String prefix) {
return details.substring(startIndex, endIndex).trim();
}

public Boolean isListCommand(String input) {
return input.startsWith("list");
}

/**
* Checks if the provided input starts with the word "menu" .
*
Expand Down Expand Up @@ -64,6 +69,37 @@ public Boolean isReduceSavingCommand(String input) {
}


public Command handleListCommand(String input, ExpenseList expenseList, SavingList savingList) {
String[] parts = input.split(" ");
String action = parts[0];

switch (action) {
case "list":
if (parts.length == 2) {
// List expenses or savings
String listType = parts[1];
if (listType.equalsIgnoreCase("expense")) {
return new ListExpenseCommand(expenseList);
} else if (listType.equalsIgnoreCase("savings")) {
return new ListSavingsCommand(savingList, expenseList);
}
} else if (parts.length == 3 && parts[1].equalsIgnoreCase("expense")) {
String filterCategory = parts[2];
return new ListExpenseCommand(expenseList, filterCategory);
} else if (parts.length == 3 && parts[1].equalsIgnoreCase("savings")) {
String filterCategory = parts[2];
return new ListSavingsCommand(savingList, expenseList, filterCategory); // Pass expenseList instance
} else {
return null;
}
break;
// Add, edit, delete, and other commands...
default:
return null;
}
return null;
}

/**
* Processes all menu commands and returns the corresponding Command object.
* This method interprets the user's input and displays either the entire menu or the associated menu item
Expand Down Expand Up @@ -253,6 +289,10 @@ public Command parseCommand(ExpenseList expenses, SavingList savings, String inp
return handleReduceSavingCommand(savings, input);
}

if (isListCommand(input)) {
return handleListCommand(input, expenses, savings);
}

return null;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/budgetbuddy/Saving.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package seedu.budgetbuddy;

public class Saving extends Transaction{

public Saving(String category, int amount) {
public Saving(String category, double amount) {
super(category, amount);
}

Expand Down
55 changes: 54 additions & 1 deletion src/main/java/seedu/budgetbuddy/SavingList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,65 @@
public class SavingList {
protected ArrayList <Saving> savings;
protected ArrayList<String> categories;
private double initialAmount;

public SavingList() {
this.savings = new ArrayList<>();
this.categories = new ArrayList<>(Arrays.asList("Salary",
"Investments", "Gifts", "Others"));
this.initialAmount = 0;
}

public void findTotalSavings() {
double totalSavings = 0;
for (int i = 0; i < savings.size(); i++) {
Saving saving = savings.get(i);
totalSavings = totalSavings + saving.getAmount();
}

this.initialAmount = totalSavings;
}


public void listSavings(String filterCategory, ExpenseList expenseList) {
findTotalSavings();
System.out.println("Savings:");
for (int i = 0; i < savings.size(); i++) {
Saving saving = savings.get(i);
if (filterCategory == null || saving.getCategory().equalsIgnoreCase(filterCategory)) {
System.out.print(i + 1 + " | ");
System.out.print("Category: " + saving.getCategory() + " | ");
System.out.print("Amount: $" + saving.getAmount() + " | ");
}
}
System.out.println("------------------------------------------------------------------------");
System.out.println("Initial Savings Amount: $" + initialAmount);
System.out.println("Expenses Deducted: ");

double totalExpenses = 0;
for (Expense expense : expenseList.getExpenses()) {
totalExpenses += expense.getAmount();
System.out.println("$" + expense.getAmount() + " spent on " + expense.getDescription() +
" on " + expense.getDateAdded());
}
System.out.println("------------------------------------------------------------------------");

double remainingAmount = calculateRemainingSavings(initialAmount, totalExpenses);
System.out.println("Remaining Amount: $" + remainingAmount);

}

public double calculateRemainingSavings(double initialAmount, double totalExpenses) {
double remainingAmount = initialAmount - totalExpenses;
if (remainingAmount < 0) {
try {
throw new Exception("Insufficient Funds");
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return remainingAmount;
}
}

public void addSaving(String category, String amount) {
Expand Down Expand Up @@ -47,5 +101,4 @@ public void reduceSavings(int index, double amount){
System.out.println("Invalid saving index.");
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/seedu/budgetbuddy/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public abstract class Transaction {
String category;
double amount;

public Transaction(String category, int amount) {
public Transaction(String category, double amount) {
this.category = category;
this.amount = amount;
}
Expand All @@ -13,6 +13,7 @@ public String getCategory() {
return category;
}

// Getters and setters
public double getAmount() {
return amount;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/seedu/budgetbuddy/command/ListExpenseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.budgetbuddy.command;

import seedu.budgetbuddy.ExpenseList;

public class ListExpenseCommand extends Command {
private ExpenseList expenses;
private String filterCategory;
public ListExpenseCommand(ExpenseList expenses) {
this.expenses = expenses;
}

public ListExpenseCommand(ExpenseList expenses, String filterCategory) {
this.expenses = expenses;
this.filterCategory = filterCategory;
}

@Override
public void execute() {
expenses.listExpenses(filterCategory);
}
}
27 changes: 27 additions & 0 deletions src/main/java/seedu/budgetbuddy/command/ListSavingsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seedu.budgetbuddy.command;

import seedu.budgetbuddy.ExpenseList;
import seedu.budgetbuddy.SavingList;

public class ListSavingsCommand extends Command {
private SavingList savings;
private ExpenseList expenses;
private String filterCategory;

public ListSavingsCommand(SavingList savings, ExpenseList expenses) {
this.savings = savings;
this.expenses = expenses;
}

public ListSavingsCommand(SavingList savings, ExpenseList expenses, String filterCategory) {
this.savings = savings;
this.expenses = expenses;
this.filterCategory = filterCategory;
}

@Override
public void execute() {
savings.listSavings(this.filterCategory, this.expenses);
}
}

31 changes: 31 additions & 0 deletions src/test/java/seedu/budgetbuddy/ExpenseListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.budgetbuddy;

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

public class ExpenseListTest {

@Test
public void calculateTotalExpenses_addingIntegers_success() {
ExpenseList expenseList = new ExpenseList();
expenseList.addExpense("Transport", "50", "Bus Fare");
expenseList.addExpense("Food", "30", "Lunch");

assertEquals(80, expenseList.calculateTotalExpenses());
}

@Test
public void calculateTotalExpenses_addingNegativeIntegers_exceptionThrown() {
ExpenseList expenseList = new ExpenseList();
expenseList.addExpense("Transport", "-50", "Bus Fare");
expenseList.addExpense("Food", "-30", "Lunch");

try {
assertEquals(0, expenseList.calculateTotalExpenses());
fail();
} catch (Exception e) {
assertEquals("java.lang.Exception: Expenses should not be negative", e.getMessage());
}
}
}
31 changes: 31 additions & 0 deletions src/test/java/seedu/budgetbuddy/SavingListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.budgetbuddy;

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

public class SavingListTest {

@Test
public void calculateRemainingSavings_sufficientFunds_success() {
SavingList savingList = new SavingList();
double initialAmount = 1000;
double totalExpenses = 200;
double expectedRemaining = 800;

double actualRemaining = savingList.calculateRemainingSavings(initialAmount, totalExpenses);

assertEquals(expectedRemaining, actualRemaining);
}

@Test
public void testCalculateRemainingSavings_insufficientFunds_exceptionThrown() {
SavingList savingList = new SavingList();
double initialAmount = 100;
double totalExpenses = 200;

assertThrows(RuntimeException.class, () -> {
savingList.calculateRemainingSavings(initialAmount, totalExpenses);
});
}
}
2 changes: 1 addition & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bye
bye

0 comments on commit e0471a6

Please sign in to comment.