Help on java programming! You will write a class called Customer. A customer is
ID: 3670546 • Letter: H
Question
Help on java programming!
You will write a class called Customer. A customer is described by:
a name
an ArrayList of Invoice objects
Use good principles of class design.
Here is the Invoice class:
public class Invoice {
private String invoiceID;
private double amountDue;
public Invoice(String invoiceID, double amountDue) {
this.invoiceID = invoiceID;
this.amountDue = amountDue;
}
public String getInvoiceID() {
return invoiceID;
}
public double getAmountDue() {
return amountDue;
}
}
1. Here is the class header, instance data variables,. and one constructor.
public class Customer {
private String name;
private ArrayList<Invoice> invoiceList;
public Customer(String name) {
this.name = name;
invoiceList = new ArrayList<Invoice>();
}
Write a second, overloaded constructor that takes in the customer's name as well as a first invoice to be added to the list.
2.Write getters and setters for the class.
3. Write a toString method. The text representation should include the customer name and the number of invoices for that customer.
4. Write a method called getTotalDue that returns the total amount of money due by the customers. The total amount due is the sum of the money due on all invoices.
5. Write a method called addInvoice that adds a new invoice to the customer
6. You now decide to include a counter to keep track of how many Customerobjects have been created.
Write:
the declaration of a variable to keep track of this information
the code that would be placed inside the Customer construtor
a getter method for this variable
Note: You are not counting how many invoices a customer has. You are counting how many customers there are.
7. Write an enumerated data type that describes an invoice's status as paid, due, or past due.
(You do not need to keep track of any other information about the status.)
Explanation / Answer
Customer.java
import java.io.*;
import java.util.*;
public class Customer {
// static variable has single instance across all
// the objects created for this class
private static int numberOfCustomers = 0;
private String name;
private ArrayList<Invoice> invoiceList;
public Customer(String name) {
this.name = name;
invoiceList = new ArrayList<Invoice>();
// increment number of customers created
numberOfCustomers += 1;
}
public Customer(String name, Invoice firstInvoice) {
this.name = name;
invoiceList = new ArrayList<Invoice>();
invoiceList.add(firstInvoice);
// increment number of customers created
numberOfCustomers += 1;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Invoice> getInvoiceList() {
return this.invoiceList;
}
public void setInvoiceList(ArrayList<Invoice> newInvoiceList) {
this.invoiceList = newInvoiceList;
}
public double getTotalDue() {
double totalAmountDue = 0.0;
// iterate through all the invoices and sum up amount due
for (Invoice invoice: invoiceList) {
totalAmountDue += invoice.getAmountDue();
}
return totalAmountDue;
}
@Override
public String toString() {
return "Name: " + this.name + ", Number Of Invoices: " + invoiceList.size();
}
public void addInvoice(Invoice newInvoice) {
invoiceList.add(newInvoice);
}
// good practice to use static method to access variable
public static int getNumberOfCustomers() {
return numberOfCustomers;
}
}
Enumerated Data Type for status of invoice:
public enum InvoiceStatus {
PAID, DUE, PAST_DUE
}
Best practice will be to inclue this in invoice class and have a getter to request its status.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.