I\'m so confused with my java hw. looking for some help, all the question relate
ID: 3818753 • Letter: I
Question
I'm so confused with my java hw. looking for some help, all the question related to each other so I post all the question together. Thx a lot!
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;
}
}
Question 15
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.
Question 16
Write getters and setters for the class.
Question 17
Write a toString method. The text representation should include the customer name and the number of invoices for that customer.
Question 18
Write a method called getTotalDue that returns the total amount of money due by the customer. The total amount due is the sum of the money due on all invoices for that one, single customer.
Question 19
Write a method called addInvoice that adds a new invoice to the customer.
Question 20
You now decide to include a counter to keep track of how many Customer objects 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.
Question 21
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;
}
// 15
public Customer(String name, Invoice firstInvoice) {
this.name = name;
invoiceList = new ArrayList<Invoice>();
invoiceList.add(firstInvoice);
// increment number of customers created
numberOfCustomers += 1;
}
// 16
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;
}
// 18
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;
}
// 17
@Override
public String toString() {
return "Name: " + this.name + ", Number Of Invoices: " + invoiceList.size();
}
// 19
public void addInvoice(Invoice newInvoice) {
invoiceList.add(newInvoice);
}
// 20
// good practice to use static method to access variable
public static int getNumberOfCustomers() {
return numberOfCustomers;
}
}
// 21
Enumerated Data Type for status of invoice:
public enum InvoiceStatus {
PAID, DUE, PAST_DUE
}