The class ExpenseLedger will record all the expenses you make. You record an expense by giving it a serial number, a description and the money you spent on this item. So you need to create a class ExpenseItem with serial, description and amount. 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
void addExpense(description, amount): You should be able to figure out the serial of the expense yourself. This method will create an expense item and add it to the ledger int removeSmallExpenses(double amt):This method will find the expense items that are smaller or equal to the amount amt and then remove these items from the list. Note that multiple items can be smaller than this value and thus each of these should be removed. Also this method should return the number of items removed. double totalExpenditure(): This method will calculate the total expenditure recorded in the ledger so far.
import java.util.ArrayList;
class ExpenseItem {
private int serialNumber;
private String description;
private double amount;
public ExpenseItem() {
}
public ExpenseItem(int serialNumber, String description, double amount) {
this.serialNumber = serialNumber;
this.description = description;
this.amount = amount;
}
/**
* @return the serialNumber
*/
public int getSerialNumber() {
return serialNumber;
}
/**
* @param serialNumber the serialNumber to set
*/
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the amount
*/
public double getAmount() {
return amount;
}
/**
* @param amount the amount to set
*/
public void setAmount(double amount) {
this.amount = amount;
}
}
class ExpenseLedger {
private ArrayList<ExpenseItem> expenseItems;
public ExpenseLedger() {
this.expenseItems = new ArrayList<ExpenseItem>();
}
/**
* This method will create an expense item and add it to the ledger
*
* @param description
* @param amount
*/
public void addExpense(String description, double amount) {
int min = 1000;
int max = 10000;
int serialNumber = (int) (Math.random() * ((max - min) + 1)) + min;
expenseItems.add(new ExpenseItem(serialNumber, description, amount));
}
/**
* This method will find the expense items that are smaller or equal to the
* amount amt and then remove these items from the list. Note that multiple
* items can be smaller than this value and thus each of these should be
* removed. Also this method should return the number of items removed.
*
* @param amt
* @return
*/
public int removeSmallExpenses(double amt) {
int numberItemsRemoved = 0;
for (int i = 0; i < this.expenseItems.size(); i++) {
if (this.expenseItems.get(i).getAmount() <= amt) {
this.expenseItems.remove(i);
numberItemsRemoved++;
}
}
return numberItemsRemoved;
}
/**
* double totalExpenditure(): This method will calculate the total expenditure
* recorded in the ledger so far.
*
* @return
*/
public double totalExpenditure() {
double total_Expenditure = 0;
for (int i = 0; i < this.expenseItems.size(); i++) {
total_Expenditure += this.expenseItems.get(i).getAmount();
}
return total_Expenditure;
}
}
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
ExpenseLedger expenseLedger = new ExpenseLedger();
expenseLedger.addExpense("description 1", 500);
expenseLedger.addExpense("description 2", 400);
expenseLedger.addExpense("description 3", 200);
expenseLedger.addExpense("description 4", 1200);
System.out.println("The number of items removed: " + expenseLedger.removeSmallExpenses(550));
System.out.println("The total expenditure recorded in the ledger so far: " + expenseLedger.totalExpenditure());
}
}
Comments
Leave a comment