Implement the classes and functionality needed to support the data storage you've defined. This will, in essence,
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 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
public List
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
List
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 + '\'' +
'}';
}
}
Engineering Economy
ISBN: 978-0132554909
15th edition
Authors: William G. Sullivan, Elin M. Wicks, C. Patrick Koelling