Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Edit Expense and Edit Savings #26

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/seedu/duke/Expense.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package seedu.duke;

public class Expense extends Transaction {
public Expense(String category, double amount, String description){

public Expense(String category, double amount, String description) {
super(category, amount, description);
}

}
30 changes: 27 additions & 3 deletions src/main/java/seedu/duke/ExpenseList.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package seedu.duke;
import java.util.ArrayList;
class ExpenseList {
protected ArrayList<Expense> expenses;
protected ArrayList<String> categories;

public class ExpenseList {
protected ArrayList<Expense> expenses = new ArrayList<>();
protected ArrayList<String> categories = new ArrayList<>();
public ExpenseList() {
this.expenses = new ArrayList<>();
this.categories = new ArrayList<>();
// Hardcode expense categories
categories.add("Housing");
categories.add("Groceries");
categories.add("Utility");
categories.add("Transport");
categories.add("Entertainment");
categories.add("Others");
}

// Method to edit expense
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);
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package seedu.duke;

public class Parser {
// Add methods to parse the new edit expense and edit saving commands
public static Transaction parseEditExpenseCommand(String command) {
String[] parts = command.split(" ");
String category = null;
int index = -1;
double amount = -1;
String description = null;

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);
}
}

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

public static Transaction parseEditSavingCommand(String command) {
String[] parts = command.split(" ");
String category = null;
int index = -1;
double amount = -1;
String description = null;

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);
}
}

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

2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/Saving.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class Saving extends Transaction {
public Saving(String category, double amount, String description) {
super(category, amount, description);
}
}
}
31 changes: 27 additions & 4 deletions src/main/java/seedu/duke/SavingList.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package seedu.duke;

import java.util.ArrayList;

public class SavingList {
protected ArrayList<Saving> savings = new ArrayList<>();
protected ArrayList<String> categories = new ArrayList<>();
class SavingList {
protected ArrayList<Saving> savings;
protected ArrayList<String> categories;

public SavingList() {
this.savings = new ArrayList<>();
this.categories = new ArrayList<>();
// Hardcode saving categories
categories.add("Salary");
categories.add("Investments");
categories.add("Gifts");
categories.add("Others");
}

// Method to edit saving
public void editSaving(String category, int index, double amount, String description) {
int categoryIndex = categories.indexOf(category);
if (categoryIndex != -1 && index > 0 && index <= savings.size()) {
Saving savingToEdit = savings.get(index - 1);
savingToEdit.setCategory(category);
savingToEdit.setAmount(amount);
savingToEdit.setDescription(description);
System.out.println("Saving edited successfully.");
} else {
System.out.println("Invalid category or index.");
}
}

public void reduceSavings(int index, double amount){
if (index >= 0 && index < savings.size()){
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/duke/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package seedu.duke;

public abstract class Transaction {

protected String category;
Expand All @@ -16,6 +15,10 @@ public String getCategory(){
return category;
}

public void setCategory(String category) {
this.category = category;
}

public void setCategory(){
this.category = category;
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/seedu/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.duke;
import java.util.Scanner;

public class Ui {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ExpenseList expenseList = new ExpenseList();
SavingList savingList = new SavingList();

// Edit an expense
editExpense(expenseList, scanner);

// Edit a saving
editSaving(savingList, scanner);

// Close the scanner to avoid resource leak
scanner.close();
}

private static void editExpense(ExpenseList expenseList, Scanner scanner) {
System.out.println("Enter edit expense command:");
String editExpenseCommand = scanner.nextLine();

Transaction editedExpense = Parser.parseEditExpenseCommand(editExpenseCommand);
if (editedExpense != null) {
expenseList.editExpense(editedExpense.getCategory(), 1, editedExpense.getAmount(),
editedExpense.getDescription());
System.out.println("Expense edited successfully.");
} else {
System.out.println("Invalid edit expense command.");
}
}

private static void editSaving(SavingList savingList, Scanner scanner) {
System.out.println("Enter edit saving command:");
String editSavingCommand = scanner.nextLine();

Transaction editedSaving = Parser.parseEditSavingCommand(editSavingCommand);
if (editedSaving != null) {
savingList.editSaving(editedSaving.getCategory(), 1, editedSaving.getAmount(),
editedSaving.getDescription());
System.out.println("Saving edited successfully.");
} else {
System.out.println("Invalid edit saving command.");
}
}
}
Loading