Question: Implement the classes and functionality needed to support the data storage you've defined. This will, in essence, complete the coding of your project, which will

 Implement the classes and functionality needed to support the data storage you've defined. This will, in essence, complete the coding of your project, which will allow you the final week of the course to test, incorporate feedback, and prepare your application for final submission. 

 

FeedBack: You will want to implement some kind of looping mechanism to make a menu that repeatedly asks for the next piece of information. Also great use of composition for the Expenses - that makes your program a little more flexible and forgiving when dealing with new entries into your arraylist. Consider the variables for each class as potential columns for your database tables!

My program:

Category. Java

class Category {

    private String name; 


 

    public Category(String name) {

        this.name = name;

    }


 

    public String getName() {

        return name;

    }


 

    @Override

    public String toString() {

        return "Category{" +

                "name='" + name + '\'' +

                '}';

    }

}

Expense.java

import java.time.LocalDate;

class Expense {

    private int id;

    private double amount;

    private String description;

    private Category category;

    private LocalDate date;


 

    public Expense(int id, double amount, String description, Category category, LocalDate date) {

        this.id = id;

        this.amount = amount;

        this.description = description;

        this.category = category;

        this.date = date;

    }


 

    public int getId() {

        return id;

    }


 

    public double getAmount() {

        return amount;

    }


 

    public String getDescription() {

        return description;

    }


 

    public Category getCategory() {

        return category;

    }


 

    public LocalDate getDate() {

        return date;

    }


 

    @Override

    public String toString() {

        return "Expense{" +

                "id=" + id +

                ", amount=" + amount +

                ", description='" + description + '\'' +

                ", category=" + category +

                ", date=" + date +

                '}';

    }

}

ExpenseTracker.java

import java.util.ArrayList;

import java.util.List;


 

public class ExpenseTracker {

    public List expenses;

    public List categories;

    public User user;


 

    public ExpenseTracker(User user) {

        this.user = user;

        this.expenses = new ArrayList<>();

        this.categories = new ArrayList<>();

    }


 

    public void addExpense(Expense expense) {

        expenses.add(expense);

    }


 

    public void removeExpense(Expense expense) {

        expenses.remove(expense);

    }


 

    public List getExpensesByCategory(Category category) {

        List filteredExpenses = new ArrayList<>();

        for (Expense expense : expenses) {

            if (expense.getCategory().equals(category)) {

                filteredExpenses.add(expense);

            }

        }

        return filteredExpenses;

    }


 

    public double getTotalSpending() {

        double total = 0;

        for (Expense expense : expenses) {

            total += expense.getAmount();

        }

        return total;

    }


 

    public double getSpendingByCategory(Category category) {

        double total = 0;

        for (Expense expense : getExpensesByCategory(category)) {

            total += expense.getAmount();

        }

        return total;

    }


 

    @Override

    public String toString() {

        return "ExpenseTracker{" +

                "expenses=" + expenses +

                ", categories=" + categories +

                ", user=" + user +

                '}';

    }

}


User.java

class User {

    private int id;

    private String name;

    private String email;


 

    public User(int id, String name, String email) {

        this.id = id;

        this.name = name;

        this.email = email;

    }


 

    public int getId() {

        return id;

    }


 

    public String getName() {

        return name;

    }


 

    public String getEmail() {

        return email;

    }


 

    @Override

    public String toString() {

        return "User{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", email='" + email + '\'' +

                '}';

    }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!