Skip to content

Commit

Permalink
Merge pull request #34 from itsmejr257/junRong-Core-Integration
Browse files Browse the repository at this point in the history
Integrate functions to work together
  • Loading branch information
itsmejr257 authored Mar 12, 2024
2 parents 2216075 + 73491db commit b2b96fa
Show file tree
Hide file tree
Showing 25 changed files with 468 additions and 394 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ test {
}

application {
mainClass.set("seedu.budgetBuddy.BudgetBuddy")
mainClass.set("seedu.budgetbuddy.BudgetBuddy")
}

shadowJar {
archiveBaseName.set("duke")
archiveBaseName.set("budgetbuddy")
archiveClassifier.set("")
}

Expand Down
58 changes: 45 additions & 13 deletions src/main/java/seedu/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
package seedu.budgetbuddy;

import seedu.budgetbuddy.command.Command;

import java.util.Scanner;

public class BudgetBuddy {

private static final Ui ui = new Ui();
private static final Parser parser = new Parser();
private final Ui ui;
private final Parser parser;

public static void main(String[] args) {
ui.greet();
String input;
while (true) {
input = ui.readCommand();
if (input.equals("bye")) {
break;
private ExpenseList expenses;
private SavingList savings;
public BudgetBuddy() {
ui = new Ui();
parser = new Parser();
expenses = new ExpenseList();
savings = new SavingList();
}

public void handleCommands(String input) {
Command command = parser.parseCommand(expenses, savings, input);

if (command != null) {
command.execute();
} else {
System.out.println("Invalid Command");
}
}

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

boolean isExit = false;
while (!isExit) {
String input = scanner.nextLine();

if (parser.isExitCommand(input)) {
isExit = true;
} else {
handleCommands(input);
}
parser.parseInput(input);
ui.showAdd(input);

}
ui.showGoodBye();
ui.closeScanner();

ui.showGoodbye();
scanner.close();
}

public static void main(String[] args) {
new BudgetBuddy().run();
}
}
8 changes: 8 additions & 0 deletions src/main/java/seedu/budgetbuddy/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public Expense(String category, int amount, String description) {
this.description = description;
}

public String getDescription(){
return description;
}

public void setDescription(String description){
this.description = description;
}

@Override
public String toString() {
return "Category: " + category + " Amount: " + amount + " Description: " + description;
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/seedu/budgetbuddy/ExpenseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public ExpenseList() {
"Groceries", "Utility", "Transport", "Entertainment", "Others"));
}

public void addExpense(Expense expense) {
expenses.add(expense);
}

public void addExpense(String category, String amount, String description) {
int amountInt = Integer.parseInt(amount);
Expense expense = new Expense(category, amountInt, description);
Expand All @@ -26,4 +22,25 @@ public void addExpense(String category, String amount, String description) {
categories.add(category);
}
}

public void editExpense(String category, int index, double amount, String description) {
int categoryIndex = categories.indexOf(category);
if (categoryIndex != -1 && index > 0 && index <= expenses.size()) {
Expense expenseToEdit = expenses.get(index - 1);
expenseToEdit.setCategory(category);
expenseToEdit.setAmount(amount);
expenseToEdit.setDescription(description);
System.out.println("Expense edited successfully.");
} else {
System.out.println("Invalid category or index.");
}
}
public void deleteExpense(int index){
if (index >= 0 && index < expenses.size()){
expenses.remove(index);
} else {
System.out.println("Invalid expense index.");
}
}

}
219 changes: 193 additions & 26 deletions src/main/java/seedu/budgetbuddy/Parser.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,217 @@
package seedu.budgetbuddy;

import seedu.budgetbuddy.command.AddExpenseCommand;
import seedu.budgetbuddy.command.AddSavingCommand;
import seedu.budgetbuddy.command.EditExpenseCommand;
import seedu.budgetbuddy.command.EditSavingCommand;
import seedu.budgetbuddy.command.ReduceSavingCommand;
import seedu.budgetbuddy.command.DeleteExpenseCommand;
import seedu.budgetbuddy.command.Command;

public class Parser {

public void parseInput(String input) {
if (input.startsWith("add expense")) {
String[] parts = input.split(" ", 2);
parseExpense(parts[1]);
} else if (input.startsWith("add saving")) {
String[] parts = input.split(" ", 2);
parseSaving(parts[1]);
} else {
System.out.println("Invalid input");
private String extractDetailsForAdd(String details, String prefix) {
int startIndex = details.indexOf(prefix) + prefix.length();
int endIndex = details.length();

String[] nextPrefixes = { "c/", "a/", "d/" };
for (String nextPrefix : nextPrefixes) {
if (details.indexOf(nextPrefix, startIndex) != -1 && details.indexOf(nextPrefix, startIndex) < endIndex) {
endIndex = details.indexOf(nextPrefix, startIndex);
}
}
return details.substring(startIndex, endIndex).trim();
}

private void parseExpense(String details) {
public Boolean isExitCommand(String input) {
return input.startsWith("bye");
}

public Boolean isAddExpenseCommand(String input) {
return input.startsWith("add expense");
}

public Boolean isAddSavingCommand(String input) {
return input.startsWith("add savings");
}

public Boolean isEditExpenseCommand(String input) {
return input.startsWith("edit expense");
}

public Boolean isEditSavingCommand(String input) {
return input.startsWith("edit savings");
}

public Boolean isDeleteExpenseCommand(String input){
return input.startsWith("delete");
}

public Boolean isReduceSavingCommand(String input) {
return input.startsWith("reduce");
}


public Command handleAddExpenseCommand(ExpenseList expenses, String input) {
String[] parts = input.split(" ", 2);
String details = parts[1];
try {
String category = extractDetail(details, "/c");
String amount = extractDetail(details, "/a");
String description = extractDetail(details, "/d");
TaskManager.addExpense(category, amount, description);
String category = extractDetailsForAdd(details, "c/");
String amount = extractDetailsForAdd(details, "a/");
String description = extractDetailsForAdd(details, "d/");
return new AddExpenseCommand(expenses,category, amount, description);
} catch (Exception e) {
System.out.println("Error parsing expense. Ensure the format is correct.");
}

return null;

}

private void parseSaving(String details) {
public Command handleAddSavingCommand(SavingList savings, String input) {
String[] parts = input.split(" ", 2);
String details = parts[1];

try {
String category = extractDetail(details, "/c");
String amount = extractDetail(details, "/a");
TaskManager.addSaving(category, amount);
String category = extractDetailsForAdd(details, "c/");
String amount = extractDetailsForAdd(details, "a/");
return new AddSavingCommand(savings, category, amount);
} catch (Exception e) {
System.out.println("Error parsing saving. Ensure the format is correct.");
}

return null;
}

private String extractDetail(String details, String prefix) {
int startIndex = details.indexOf(prefix) + prefix.length();
int endIndex = details.length();
public Command handleEditExpenseCommand(ExpenseList expenses, String input) {
String[] parts = input.split(" ");
String category = null;
int index = -1;
double amount = -1;
String description = null;

String[] nextPrefixes = { "/c", "/a", "/d" };
for (String nextPrefix : nextPrefixes) {
if (details.indexOf(nextPrefix, startIndex) != -1 && details.indexOf(nextPrefix, startIndex) < endIndex) {
endIndex = details.indexOf(nextPrefix, startIndex);
for (String part : parts) {
if (part.startsWith("c/")) {
category = part.substring(2);
} else if (part.startsWith("i/")) {
try {
index = Integer.parseInt(part.substring(2));
} catch (NumberFormatException e) {
// Handle invalid index format
return null;
}
} else if (part.startsWith("a/")) {
try {
amount = Double.parseDouble(part.substring(2));
} catch (NumberFormatException e) {
// Handle invalid amount format
return null;
}
} else if (part.startsWith("d/")) {
description = part.substring(2);
}
}
return details.substring(startIndex, endIndex).trim();

// Validate required fields
if (category != null && index != -1 && amount != -1 && description != null) {
return new EditExpenseCommand(expenses, category, index, amount, description);
} else {
// Handle incomplete command
return null;
}
}

public Command handleEditSavingCommand(SavingList savings, String input) {
String[] parts = input.split(" ");
String category = null;
int index = -1;
double amount = -1;

for (String part : parts) {
if (part.startsWith("c/")) {
category = part.substring(2);
} else if (part.startsWith("i/")) {
try {
index = Integer.parseInt(part.substring(2));
} catch (NumberFormatException e) {
// Handle invalid index format
return null;
}
} else if (part.startsWith("a/")) {
try {
amount = Double.parseDouble(part.substring(2));
} catch (NumberFormatException e) {
// Handle invalid amount format
return null;
}
}
}

// Validate required fields
if (category != null && index != -1 && amount != -1) {
return new EditSavingCommand(savings, category, index, amount);
} else {
// Handle incomplete command
return null;
}
}

public Command handleDeleteExpenseCommand(ExpenseList expenses, String input) {
String[] parts = input.split("i/", 2);
try {
String indexAsString = parts[1].trim();
int index = Integer.parseInt(indexAsString) - 1;
return new DeleteExpenseCommand(expenses, index);
} catch (NumberFormatException e) {
return null;
}

}

public Command handleReduceSavingCommand(SavingList savings, String input) {
String description = input.replace("reduce", "").trim();

if(description.contains("i/") && description.contains("a/")) {
String[] parts = description.split("i/|a/", 3);

String indexToReduceAsString = parts[1].trim();
String amountToReduceAsString = parts[2].trim();
int indexToReduce = Integer.parseInt(indexToReduceAsString) - 1;
double amountToReduce = Double.parseDouble(amountToReduceAsString);

return new ReduceSavingCommand(savings, indexToReduce, amountToReduce);
}

return null;

}
public Command parseCommand(ExpenseList expenses, SavingList savings, String input) {

if (isAddExpenseCommand(input)) {
return handleAddExpenseCommand(expenses, input);
}

if (isAddSavingCommand(input)) {
return handleAddSavingCommand(savings, input);
}

if (isEditExpenseCommand(input)) {
return handleEditExpenseCommand(expenses, input);
}

if (isEditSavingCommand(input)) {
return handleEditSavingCommand(savings, input);
}


if (isDeleteExpenseCommand(input)) {
return handleDeleteExpenseCommand(expenses, input);
}

if (isReduceSavingCommand(input)) {
return handleReduceSavingCommand(savings, input);
}

return null;
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/budgetbuddy/Saving.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ public Saving(String category, int amount) {
super(category, amount);
}


@Override
public String toString() {
return "Category: " + category + " Amount: " + amount;
}


}
Loading

0 comments on commit b2b96fa

Please sign in to comment.