You are required to write a program that implements the computerized maintenance of your
monthly expenditure record keeping. You will have to build two classes ExpenseItem,
ExpenseLedger
The class ExpenseLedger will record all the expenses you make. You record an expense by giving
it a serial number, a description (that tells what it is about) and the money you spent on this item. So
you need to create a class ExpenseItem with serial, description and amount. Provide constructors,
getters and setters. Also provide toString for this class. Now in the ExpenseLedger class you need to
build a collection that will hold the instance of ExpenseItem. Provide the following methods in the
ExpenseLedger
1. int removeSmallExpenses(double amt):This method will find expense items that are smaller or equal to amount amt & then remove these items from list. Note that multiple items can be smaller than this value & thus each of these should be removed & return number of items removed.
import java.util.LinkedList;
public class ExpenseLedger {
private LinkedList<ExpenseItem> expensesLedger;
public ExpenseLedger() {
expensesLedger = new LinkedList<>();
}
int removeSmallExpenses(double amt) {
LinkedList<ExpenseItem> expenseItems = new LinkedList<>();
for (ExpenseItem expenseItem : expensesLedger) {
if (expenseItem.getAmount() <= amt) {
expenseItems.add(expenseItem);
}
}
for (ExpenseItem expenseItem : expenseItems) {
expensesLedger.remove(expenseItem);
}
return expenseItems.size();
}
}
public class ExpenseItem {
private int serial;
private String description;
private double amount;
public ExpenseItem() {
serial = -1;
description = "?";
amount = -1;
}
public ExpenseItem(int serial, String description, double amount) {
this.serial = serial;
this.description = description;
this.amount = amount;
}
public int getSerial() {
return serial;
}
public void setSerial(int serial) {
this.serial = serial;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return serial + " " + description + " " + amount;
}
}
Comments
Leave a comment