Question

You are required to write a program that implements the computerized maintenance of your monthly expenditure record keeping.

OO Object Associations

Aggregation Vs. Composition 

Built-in libraries and functions 


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 1. 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. 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

 2. 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.

 3. double totalExpenditure(): This method will calculate the total expenditure recorded in the ledger so far. You can do this by iterating through the expenses list and adding all the amounts to find the total expenditure so far.

 4. Override the toString method so that the ledger is printed in a well formatted way


 Main class will be provided to you, your code must run and produce the desired output. This will be a check of your correct implementation. Please use the names of classes and methods as provided in this description.



1 0
Add a comment Improve this question Transcribed image text
Answer #1

ExpenseItem.java


public class ExpenseItem {

    private int serialId;

    private String description;

    private double amount;

    

    public ExpenseItem(int serialId, String description, double amount) {

        this.serialId = serialId;

        this.description = description;

        this.amount = amount;

    }

    

    public int getSerialId() {

        return serialId;

    }

    

    public void setSerialId(int serialId) {

        this.serialId = serialId;

    }

    

    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 String.format(" %10d  %30s  %10.2f \n",getSerialId(),getDescription(),getAmount());

    }

}

ExpenseLedger.java


import java.util.ArrayList;

import java.util.Iterator;


public class ExpenseLedger {

    private static int serialId = 1;

    private final ArrayList records;

    

    public ExpenseLedger() {

        this.records = new ArrayList();

    }

    

    public void addExpense(String description, double amount) {

        records.add(new ExpenseItem(serialId++, description, amount));

    }

    

    public int removeSmallExpenses(double amt) {

        Iterator itr = records.iterator();

        int hadInitially = records.size();

        while (itr.hasNext()) {

            if (((ExpenseItem) itr.next()).getAmount() <= amt) {

                itr.remove();

            }

        }

        return hadInitially - records.size();

    }

    

    public double totalExpenditure() {

        double total = 0.0;

        for (ExpenseItem record : records) {

            total += record.getAmount();

        }

        return total;

    }

    

    @Override

    public String toString() {

        StringBuilder data = new StringBuilder("");

        for (ExpenseItem record : records) {

            data.append(record.toString());

        }

        return String.format("|%10s| %30s| %10s|\n", "Serial ID", "Description", "Amount")

        + String.format("-%10s--%30s--%10s-\n", "----------", "------------------------------", "----------")

        + data;

    }

}

Main.java


public class Main {

    public static void main(String[] args) {

        ExpenseLedger expenseLedger = new ExpenseLedger();

        expenseLedger.addExpense("TV",30);

        expenseLedger.addExpense("Twitch",40);

        expenseLedger.addExpense("Electricity",150);

        expenseLedger.addExpense("Internet",100);

        System.out.println(expenseLedger);

        System.out.println("Number of items removed having less than equal to Amount 30.00 is " + expenseLedger.removeSmallExpenses(35));

        System.out.println(expenseLedger);

        System.out.println("Total expenditure = " + expenseLedger.totalExpenditure());

    }

}


answered by: Musk Elon
Add a comment
Know the answer?
Add Answer to:
You are required to write a program that implements the computerized maintenance of your monthly expenditure record keeping.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your l...

    Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your local bank. You should declare the following collection of classes: 1) An abstract class Customer: each customer has: firstName(String), lastName (String), age (integer), customerNumber (integer) - The Class customer has a class variable lastCustomerNumber that is initialized to 9999. It is used to initialize the customerNumber. Hence, the first customer number is set to 9999 and each time a new customer...

  • If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry...

    If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry store and some of the Inventory (rings/necklaces/earrings) purchased there (The Jewelry store can be real or not) using Object-Oriented Programming (OOP). The important aspect of object-oriented programming is modular development and testing of reusable software modules. You love selling rings, necklaces, and earrings as well as programming and have decided to open a Jewelry store. To save...

  • M01 PA C++ Classes and Objects INTRODUCTION: Write a C++ program that implements and utilizes a...

    M01 PA C++ Classes and Objects INTRODUCTION: Write a C++ program that implements and utilizes a Stereo Receiver Class/Object. INCLUDE IN YOUR ASSIGNMENT At the top of each of your C++ programs, you should have at least four lines of documentation: Program name (the C++ file name(s)), Author (your name), Date last updated, and Purpose (a brief description of what the program accomplishes). Here is an example: // Program name: tictactoe.cpp // Author: Rainbow Dash // Date last updated: 5/26/2016...

  • Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...

    Java 1. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • CMSC 256 – Project 5 Programming Assignment 5 Note: When you turn in an assignment to...

    CMSC 256 – Project 5 Programming Assignment 5 Note: When you turn in an assignment to be graded in this class, you are making the claim that you neither gave nor received assistance on the work you turned in (except, of course, assistance from the instructor or teaching assistants). Program: Ticketing System Points: 100 A set of classes is used to handle the different ticket types for a theater event. All tickets have a unique serial number that is assigned...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • code must be in java. Assignment 5 Purpose In this assignment, you will implement a class...

    code must be in java. Assignment 5 Purpose In this assignment, you will implement a class hierarchy in Java. Instructions The class hierarchy you will implement is shown below account Package Account manager Package SavingAccount CheckingAccount AccountManager The descriptions of each class and the corresponding members are provided below. Please note that the Visibility Modifiers (public, private and protected) are not specified and you must use the most appropriate modifiers Class AccountManager is a test main program. Create it so...

  • Problem Description to implement a Java application, called ShoppingApplication, that can be used in a retail...

    Problem Description to implement a Java application, called ShoppingApplication, that can be used in a retail store. You are asked to implement three classes: Item, Invoice, and InvoiceDriver. Each of these classes is described below. Item class The Item class represents of an item that is being sold in the retail store (e.g., book or pencil) where an item is identified by three instance variables: name (of type Sring), weight (of type double), price (of type double), and currentDiscount (of...

  • Problem 0 Fix the Collections Practice.java file so that it imports all three classes listed below:...

    Problem 0 Fix the Collections Practice.java file so that it imports all three classes listed below: HashSet, HashMap, ArrayList Problem 1 - iterating through HashSets Complete the method named sum that accepts a HashSet of type Double as its parameter and returns a double sum - so that adds each element in the HashSet to the total sum and then returns. This method stub has been provided for you. Example: HashSet<Double> containing (5.0, 10.5) would return a sum of 15.5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT