Skip to content

Commit

Permalink
Merge pull request #24 from yyangdaa/yangda
Browse files Browse the repository at this point in the history
add addexpense & addsaving - yangda
  • Loading branch information
yyangdaa authored Mar 12, 2024
2 parents d04f025 + 75e79e6 commit 1fb068d
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test {
}

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

shadowJar {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/budgetBuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package seedu.budgetbuddy;

public class BudgetBuddy {

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

public static void main(String[] args) {
ui.greet();
String input;
while (true) {
input = ui.readCommand();
if (input.equals("bye")) {
break;
}
parser.parseInput(input);
ui.showAdd(input);
}
ui.showGoodBye();
ui.closeScanner();
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/budgetBuddy/Expense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package seedu.budgetbuddy;

public class Expense extends Transaction{
protected String description;

public Expense(String category, int amount, String description) {
super(category, amount);
this.description = description;
}

@Override
public String toString() {
return "Category: " + category + " Amount: " + amount + " Description: " + description;
}
}
29 changes: 29 additions & 0 deletions src/main/java/seedu/budgetBuddy/ExpenseList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.budgetbuddy;

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

public class ExpenseList {
protected ArrayList <Expense> expenses;
protected ArrayList<String> categories;

public ExpenseList() {
this.expenses = new ArrayList<>();
this.categories = new ArrayList<>(Arrays.asList("Housing",
"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);
expenses.add(expense);

if (!categories.contains(category)) {
categories.add(category);
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/seedu/budgetBuddy/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.budgetbuddy;

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 void parseExpense(String details) {
try {
String category = extractDetail(details, "/c");
String amount = extractDetail(details, "/a");
String description = extractDetail(details, "/d");
TaskManager.addExpense(category, amount, description);
} catch (Exception e) {
System.out.println("Error parsing expense. Ensure the format is correct.");
}
}

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

private String extractDetail(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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/seedu/budgetBuddy/Saving.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package seedu.budgetbuddy;

public class Saving extends Transaction{

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


@Override
public String toString() {
return "Category: " + category + " Amount: " + amount;
}
}
25 changes: 25 additions & 0 deletions src/main/java/seedu/budgetBuddy/SavingList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.budgetbuddy;

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

public class SavingList {
protected ArrayList <Saving> savings;
protected ArrayList<String> categories;

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

public void addSaving(String category, String amount) {
int amountInt = Integer.parseInt(amount);
Saving saving = new Saving(category, amountInt);
savings.add(saving);

if (!categories.contains(category)) {
categories.add(category);
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/budgetBuddy/TaskManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package seedu.budgetbuddy;

public class TaskManager {
private static ExpenseList expenses = new ExpenseList();
private static SavingList savings = new SavingList();

public static void addExpense(String category, String amount, String description) {
expenses.addExpense(category, amount, description);
}

public static void addSaving(String category, String amount) {
savings.addSaving(category, amount);
}
}

20 changes: 20 additions & 0 deletions src/main/java/seedu/budgetBuddy/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seedu.budgetbuddy;

public abstract class Transaction {
String category;
int amount;
String description;

public Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}

public String getCategory() {
return category;
}

public double getAmount() {
return amount;
}
}
63 changes: 63 additions & 0 deletions src/main/java/seedu/budgetBuddy/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package seedu.budgetbuddy;

import java.util.Scanner;

public class Ui {
private Scanner scanner;

public Ui() {
this.scanner = new Scanner(System.in);
}

public String readCommand() {
return scanner.nextLine().trim();
}

public void closeScanner() {
scanner.close();
}

public void greet() {
String Logo =
" ____ ____ \n" +
" | __ )| __ ) \n" +
" | _ \\| _ \\ \n" +
" | |_) | |_) |\n" +
" |____/|____/ \n";
System.out.println("Welcome to BudgetBuddy!\n" + Logo);
System.out.println("What can I do for you?");
showLine();
}

public void showLine() {
System.out.println("____________________________________________________________");
}

public void showError(String message){
System.out.println("an error occurred: " + message);
}

public void showGoodBye(){
System.out.println("Bye. Hope to see you again soon!");
}

public void showList(String list) {
showLine();
System.out.println(list);
showLine();
}

public void showAdd(String addedItem) {
showLine();
System.out.println("Got it. I've added this transaction:");
System.out.println(addedItem);
showLine();
}

public void showDelete(String deletedItem) {
showLine();
System.out.println("Noted. I've removed this transaction:");
System.out.println(deletedItem);
showLine();
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/budgetBuddy/data/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package seedu.budgetbuddy.data;

public class Storage {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seedu.budgetbuddy.exception;

public class budgetBuddyException extends Exception{
public budgetBuddyException(String message) {
super(message);
}
}
21 changes: 0 additions & 21 deletions src/main/java/seedu/duke/Duke.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package seedu.duke;
package seedu.budgetBuddy;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class DukeTest {
class budgetBuddyTest {
@Test
public void sampleTest() {
assertTrue(true);
}

@Test
public void sampleFailingTest() {
assertTrue(true);
}
}
17 changes: 9 additions & 8 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
Welcome to BudgetBuddy!
____ ____
| __ )| __ )
| _ \| _ \
| |_) | |_) |
|____/|____/

What is your name?
Hello James Gosling
What can I do for you?
____________________________________________________________
Bye. Hope to see you again soon!
2 changes: 1 addition & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
James Gosling
bye

0 comments on commit 1fb068d

Please sign in to comment.