Question: For this part of your course project, you will implement the database support defined in your design description using the Java programming language. Remember to
For this part of your course project, you will implement the database support defined in your design description using the Java programming language. Remember to keep the requirements from the Course Project Overview in mind as you implement this final portion of your design because, in the end, your application must fulfill the requirements laid out in the overview.
Your design description included a section on data storage, which should have included the following information:
- Table name(s)
- Fields the table will contain, including data type
- Table relationships, if any
Your task for this week is to 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.
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 + '\'' +
'}';
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
