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. 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.
package expenseledger;
import java.util.ArrayList;
class ExpenseItem{
private String serial, description;
private double amount;
ExpenseItem(String des, double am){
description = des;
amount = am;
}
ExpenseItem(String s, String des, double am){
serial =s;
description = des;
amount = am;
}
String getSerial(){
return serial;
}
String getDescription(){
return description;
}
double getAmount(){
return amount;
}
public String toString(){
return serial +" "+description+" "+amount;
}
}
public class ExpenseLedger {
static ArrayList list = new ArrayList <>();
void addExpense(String description,double amount){
ExpenseItem item = new ExpenseItem(description, amount);
list.add(item);
}
public static void main(String[] args) {
}
}
Comments
Leave a comment