Skip to content

Commit

Permalink
Merge pull request #35 from ZiliaAJY/branch-defense
Browse files Browse the repository at this point in the history
Branch defense
  • Loading branch information
ZiliaAJY authored Oct 12, 2024
2 parents c29adf5 + 2f6d740 commit 3042968
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 42 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {

test {
useJUnitPlatform()
jvmArgs '-ea'

testLogging {
events "passed", "skipped", "failed"
Expand Down Expand Up @@ -43,4 +44,5 @@ checkstyle {

run{
standardInput = System.in
jvmArgs '-ea'
}
6 changes: 5 additions & 1 deletion data/BudgetBuddy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ expense | plane tickets to korea | 1000.0 | 15/9/2024 | OTHERS
expense | zoo tickets | 20.0 | 15/9/2024 | FOOD
expense | plane tickets to japan | 1000.0 | 15/10/2024 | OTHERS
expense | plane tickets to europe | 1000.0 | 1/8/2024 | OTHERS
expense | wingstop | 20.0 | 15/10/2024 | OTHERS
expense | plane ticket to japan | 1000.0 | 10/10/2024 | TRANSPORT
expense | food | 5.5 | 10/10/2024 | FOOD
income | tuition fees | 1000.0 | 15/9/2024
income | tuition | 2.0E8 | 15/9/2024
income | tuition fee | 1000.0 | 15/9/2024
budget | 900.0 | 2024-09
budget | 900.0 | 2024-10
budget | 100.0 | 2023-02
budget | 2000.0 | 2024-05
budget | 200.0 | 2024-04
budget | 7000.0 | 2024-06
13 changes: 9 additions & 4 deletions src/main/java/seedu/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import seedu.budgetbuddy.commands.Command;
import seedu.budgetbuddy.commands.ExitCommand;
import seedu.budgetbuddy.exceptions.BudgetBuddyException;
import seedu.budgetbuddy.transaction.budget.Budget;
import seedu.budgetbuddy.transaction.budget.BudgetManager;
import seedu.budgetbuddy.transaction.expense.Expense;
Expand Down Expand Up @@ -48,7 +49,7 @@ public BudgetBuddy(String filepath) {

expenseManager = new ExpenseManager(expenses, numberOfExpenses);
incomeManager = new IncomeManager(incomes, numberOfIncomes);
budgetManager = new BudgetManager(budgets);
budgetManager = new BudgetManager(budgets, numberOfBudgets);
} catch (IOException e) {
Ui.showMessage("Error updating File");
}
Expand All @@ -61,12 +62,16 @@ public BudgetBuddy(String filepath) {
*/
public void run() {
Ui.displayWelcomeMessage();
Command command;
Command command = null;
Parser parser = new Parser(expenseManager, incomeManager, budgetManager);
do {
String userCommandText = Ui.getUserCommand();
command = parser.parseCommand(userCommandText);
command.execute();
try {
command = parser.parseCommand(userCommandText);
command.execute();
} catch (BudgetBuddyException e) {
System.out.println(e.getMessage());
}
try {
storage.save(expenseManager, incomeManager, budgetManager);
} catch (IOException e) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/budgetbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.budgetbuddy.commands.ListExpenseCommand;
import seedu.budgetbuddy.commands.ListIncomeCommand;
import seedu.budgetbuddy.commands.DisplayExpenseCommand;
import seedu.budgetbuddy.exceptions.BudgetBuddyException;
import seedu.budgetbuddy.commands.SearchExpenseCommand;
import seedu.budgetbuddy.transaction.budget.Budget;
import seedu.budgetbuddy.transaction.budget.BudgetManager;
Expand Down Expand Up @@ -63,7 +64,7 @@ public Parser(ExpenseManager expenseManager, IncomeManager incomeManager, Budget
* @return The corresponding {@code Command} to execute, or an {@code IncorrectCommand}
* if the input is invalid.
*/
public Command parseCommand(String userCommandText) {
public Command parseCommand(String userCommandText) throws BudgetBuddyException {
if (AddExpenseCommand.isCommand(userCommandText)) {
return AddExpenseValidator.processCommand(userCommandText);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/budgetbuddy/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,3 @@ public void createFileIfNotExists() throws IOException {
}
}
}

6 changes: 3 additions & 3 deletions src/main/java/seedu/budgetbuddy/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class Ui {

public static final String SEPARATOR = "========================================================\n";
public static final String SEPARATOR = "========================================================";
public static final String WELCOME_MESSAGE = "Welcome to Budget Buddy!";
public static final String EXIT_MESSAGE = "Bye!";
private static final Scanner scanner = new Scanner(System.in);
Expand Down Expand Up @@ -39,9 +39,9 @@ public static String getUserCommand() {
* @param message The message to display to the user.
*/
public static void displayToUser(String message) {
System.out.print(SEPARATOR);
System.out.println(SEPARATOR);
System.out.println(message);
System.out.print(SEPARATOR);
System.out.println(SEPARATOR);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/seedu/budgetbuddy/commands/AddBudgetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import seedu.budgetbuddy.transaction.budget.BudgetManager;

import java.time.YearMonth;
import java.util.logging.Logger;

/**
* Represents a command to add a budget for a specific month and year.
*/
public class AddBudgetCommand extends Command {
private static Logger logger = Logger.getLogger(AddBudgetCommand.class.getName());
private double amount;
private YearMonth date;

Expand All @@ -19,6 +21,8 @@ public class AddBudgetCommand extends Command {
* @param date The YearMonth representing the month and year for the budget.
*/
public AddBudgetCommand(double amount, YearMonth date) {
assert amount >= 0 : "Amount must be non-negative";
assert date != null : "Date cannot be null";
this.amount = amount;
this.date = date;
}
Expand All @@ -40,15 +44,19 @@ public static boolean isCommand(String command) {
*/
@Override
public void execute() {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be non-negative.");
}

// Check if a budget already exists for the specified date
Budget existingBudget = BudgetManager.getBudget(date);

if (existingBudget != null) {
// Update the existing budget's amount
existingBudget.addAmount(amount);
logger.info("Updated existing budget for date: " + date + " with amount: " + amount);
} else {
// Add a new budget since it does not exist
BudgetManager.addBudget(new Budget(amount, date));
logger.info("Added new budget for date: " + date + " with amount: " + amount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import seedu.budgetbuddy.transaction.budget.BudgetManager;

import java.time.YearMonth;
import java.util.logging.Logger;

/**
* Represents a command to deduct a budget for a specific month and year.
*/
public class DeductBudgetCommand extends Command {
private static Logger logger = Logger.getLogger(DeductBudgetCommand.class.getName());
private double amount;
private YearMonth date;

Expand All @@ -19,6 +21,8 @@ public class DeductBudgetCommand extends Command {
* @param date The YearMonth representing the month and year for the budget.
*/
public DeductBudgetCommand(double amount, YearMonth date) {
assert amount >= 0 : "Amount must be non-negative";
assert date != null : "Date cannot be null";
this.amount = amount;
this.date = date;
}
Expand All @@ -42,12 +46,7 @@ public static boolean isCommand(String command) {
public void execute() {
// Assume validation has guaranteed that existingBudget is not null
Budget existingBudget = BudgetManager.getBudget(date);

// Handle if existingBudget is null smoothly
if (existingBudget == null) {
return;
}
// Deduct the amount from the existing budget
existingBudget.deductAmount(amount);
logger.info("Deducted " + amount + " from budget for date: " + date);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seedu.budgetbuddy.exceptions;

import seedu.budgetbuddy.Ui;

/**
* Represents a custom exception for errors specific to BudgetBuddy application.
*/
public class BudgetBuddyException extends Exception {

/**
* Constructs a new BudgetBuddyException with the specified detail message.
*/
public BudgetBuddyException(String message) {
super(Ui.SEPARATOR + "\n" + message + "\n" + Ui.SEPARATOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Budget {
* @param date The YearMonth representing the month and year for the budget.
*/
public Budget(double amount, YearMonth date) {
assert amount >= 0 : "Initial amount cannot be negative";
this.amount = amount;
this.date = date;
}
Expand All @@ -31,6 +32,7 @@ public Budget(double amount, YearMonth date) {
* @param additionalAmount The amount to be added to the existing budget.
*/
public void addAmount(double additionalAmount) {
assert additionalAmount >= 0 : "Amount to add cannot be negative";
this.amount += additionalAmount;
Ui.displayBudgetTransactionMessage(toString(), BudgetManager.getNumberOfBudgets());
}
Expand All @@ -41,6 +43,7 @@ public void addAmount(double additionalAmount) {
* @param deductedAmount The amount to be deducted from the existing budget.
*/
public void deductAmount(double deductedAmount) {
assert deductedAmount >= 0 : "Amount to deduct cannot be negative";
this.amount -= deductedAmount;
if (amount <= 0) {
this.amount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.time.YearMonth;
import java.util.ArrayList;
import java.util.logging.Logger;

/**
* Manages the budgets for different months and years.
Expand All @@ -12,9 +13,17 @@
public class BudgetManager {
private static int numberOfBudgets = 0;
private static ArrayList<Budget> budgets = new ArrayList<>();
private static final Logger logger = Logger.getLogger(BudgetManager.class.getName());

public BudgetManager(ArrayList<Budget> budgets){
/**
* Construct a BudgetManager of array content incomes
*
* @param budgets The content to be instantiated
* @param numberOfBudgets The initial count of budgets managed by BudgetManager.
*/
public BudgetManager(ArrayList<Budget> budgets, int numberOfBudgets){
BudgetManager.budgets = budgets;
BudgetManager.numberOfBudgets = numberOfBudgets;
}

/**
Expand All @@ -24,8 +33,10 @@ public BudgetManager(ArrayList<Budget> budgets){
* @param budget The Budget object to be added.
*/
public static void addBudget(Budget budget) {
assert budget != null : "Budget to be added cannot be null";
budgets.add(budget);
numberOfBudgets++;
logger.info("Added budget: " + budget);
Ui.displayBudgetTransactionMessage(budget.toString(), numberOfBudgets);
}

Expand All @@ -36,8 +47,10 @@ public static void addBudget(Budget budget) {
* @param budget The Budget object to be added.
*/
public static void deleteBudget(Budget budget) {
assert budget != null : "Budget to be deleted cannot be null";
budgets.remove(budget);
numberOfBudgets--;
logger.info("Deleted budget: " + budget);
Ui.displayBudgetDeletedMessage(budget.toString(), numberOfBudgets);
}

Expand All @@ -57,12 +70,14 @@ public static int getNumberOfBudgets() {
* @return The existing Budget for the specified date, or null if no budget exists.
*/
public static Budget getBudget(YearMonth date) {
assert date != null : "Date cannot be null";
for (Budget budget : budgets) {
// Check if the budget's date matches the specified date
if (budget.getDate().equals(date)) {
return budget; // Return the existing budget
logger.info("Retrieved budget for date: " + date);
return budget;
}
}
logger.info("No budget found for date: " + date);
return null; // No budget found for the specified date
}

Expand All @@ -73,6 +88,8 @@ public static Budget getBudget(YearMonth date) {
public static void listBudgets(YearMonth date) {
String result = "";
if (date == null) {
logger.info("No date specified for listing budget.");

result += "Here are the budgets for the 12 most recent entries:\n";

int entriesToDisplay = Math.min(budgets.size(), 12);
Expand All @@ -82,7 +99,10 @@ public static void listBudgets(YearMonth date) {
}
} else {
// Assume validator guarantees date is valid
logger.info("Listing budgets for date: " + date);

Budget budget = getBudget(date);

if (budget != null) {
result += "Here is the budget for the specified month:\n";
result += budget.toString();
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/seedu/budgetbuddy/validators/AddBudgetValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import seedu.budgetbuddy.commands.AddBudgetCommand;
import seedu.budgetbuddy.commands.Command;
import seedu.budgetbuddy.commands.IncorrectCommand;
import seedu.budgetbuddy.exceptions.BudgetBuddyException;

import java.time.YearMonth;
import java.util.logging.Logger;

import static seedu.budgetbuddy.validators.AmountValidator.validateAmount;
import static seedu.budgetbuddy.validators.DateValidator.validateYearMonth;
Expand All @@ -13,10 +14,14 @@
* Validates commands for adding budgets.
*/
public class AddBudgetValidator {
private static Logger logger = Logger.getLogger(AddBudgetValidator.class.getName());

public static Command processCommand(String command) throws BudgetBuddyException {
assert command != null : "Command cannot be null";

public static Command processCommand(String command) {
if (command.equals("add budget")) {
return new IncorrectCommand("No description provided.");
logger.warning("Attempted to add budget without description.");
throw new BudgetBuddyException("No description provided.");
}

String trimmedCommand = command.substring("add budget ".length());
Expand All @@ -31,23 +36,22 @@ public static Command processCommand(String command) {
if (part.startsWith("a/")) {
amount = validateAmount(part);
if (amount == -1) {
return new IncorrectCommand("Invalid amount format. Amount should be a positive number.");
throw new BudgetBuddyException("Invalid amount format. " +
"Amount should be a positive number.");
}
} else if (part.startsWith("m/")) {
date = validateYearMonth(part);
}
}

// Validate amount
if (amount == 0) {
return new IncorrectCommand("Amount not entered.");
} else if (amount < 0) {
return new IncorrectCommand("Invalid amount format. Amount must be a positive value.");
if (amount <= 0) {
throw new BudgetBuddyException("Invalid amount: " + amount + ". Amount must be a positive value.");
}

// Validate date
if (date == null) {
return new IncorrectCommand("Invalid date format. Use m/MM/yyyy.");
throw new BudgetBuddyException("Invalid date format. Use m/MM/yyyy.");
}

// All validations passed, return the command
Expand Down
Loading

0 comments on commit 3042968

Please sign in to comment.