diff --git a/src/main/java/seedu/duke/Expense.java b/src/main/java/seedu/duke/Expense.java index 0aaa82092e..323cc0f30e 100644 --- a/src/main/java/seedu/duke/Expense.java +++ b/src/main/java/seedu/duke/Expense.java @@ -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); } - } diff --git a/src/main/java/seedu/duke/ExpenseList.java b/src/main/java/seedu/duke/ExpenseList.java index 4f0c08bac4..4842f6f00f 100644 --- a/src/main/java/seedu/duke/ExpenseList.java +++ b/src/main/java/seedu/duke/ExpenseList.java @@ -1,10 +1,34 @@ package seedu.duke; import java.util.ArrayList; +class ExpenseList { + protected ArrayList expenses; + protected ArrayList categories; -public class ExpenseList { - protected ArrayList expenses = new ArrayList<>(); - protected ArrayList 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); diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java new file mode 100644 index 0000000000..58792eb3ad --- /dev/null +++ b/src/main/java/seedu/duke/Parser.java @@ -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; + } + } +} + diff --git a/src/main/java/seedu/duke/Saving.java b/src/main/java/seedu/duke/Saving.java index 24b963e14e..5d26899d18 100644 --- a/src/main/java/seedu/duke/Saving.java +++ b/src/main/java/seedu/duke/Saving.java @@ -4,4 +4,4 @@ public class Saving extends Transaction { public Saving(String category, double amount, String description) { super(category, amount, description); } -} \ No newline at end of file +} diff --git a/src/main/java/seedu/duke/SavingList.java b/src/main/java/seedu/duke/SavingList.java index ec8c5dde17..30d40ee288 100644 --- a/src/main/java/seedu/duke/SavingList.java +++ b/src/main/java/seedu/duke/SavingList.java @@ -1,10 +1,33 @@ package seedu.duke; - import java.util.ArrayList; -public class SavingList { - protected ArrayList savings = new ArrayList<>(); - protected ArrayList categories = new ArrayList<>(); +class SavingList { + protected ArrayList savings; + protected ArrayList 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()){ diff --git a/src/main/java/seedu/duke/Transaction.java b/src/main/java/seedu/duke/Transaction.java index e8e1ea25bb..ac9d2eea81 100644 --- a/src/main/java/seedu/duke/Transaction.java +++ b/src/main/java/seedu/duke/Transaction.java @@ -1,5 +1,4 @@ package seedu.duke; - public abstract class Transaction { protected String category; @@ -16,6 +15,10 @@ public String getCategory(){ return category; } + public void setCategory(String category) { + this.category = category; + } + public void setCategory(){ this.category = category; } diff --git a/src/main/java/seedu/duke/Ui.java b/src/main/java/seedu/duke/Ui.java new file mode 100644 index 0000000000..94a19f8900 --- /dev/null +++ b/src/main/java/seedu/duke/Ui.java @@ -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."); + } + } +}