In: Computer Science
Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows a user to enter and print an arbitrary invoice. Do not modify any of the existing classes.
..... ..... ..... /**
Describes an invoice for a set of purchased products.
*/
public class Invoice
{
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{
}
/**
Formats the invoice.
@return the formatted invoice
*/
public String format()
{
}
}
/**
Describes a quantity of an article to purchase.
*/
public class LineItem
{
/**
Computes the total cost of this line item.
@return the total price
*/
public double getTotalPrice()
{
}
/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
}
}
/**
Describes a product with a description and a price.
*/
public class Product
{
/**
Gets the product description.
@return the description
*/
public String getDescription()
{
}
/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
}
}
/**
Describes a mailing address.
*/
public class Address
{
/**
Formats the address.
@return the address as a string with three lines
*/
public String format()
{
}
}
public class Invoice
{
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{ LineItem anItem = newLineItem(aProduct, quantity);
items.add(anItem);
}
/**
Formats the invoice.
@return the formatted invoice
*/
public String format()
{
String r = " I N V O I C E\n\n"
+ billingAddress.format()
+ String.format("\n\n%-30s%8s%5s%8s\n",
"Description", "Price", "Qty", "Total");
for (LineItem i : items)
{
r = r + i.format() + "\n";
}
r = r + String.format("\nAMOUNT DUE: $%8.2f",
getAmountDue());
return r;
}
/**
Describes a quantity of an article to purchase.
*/
public class LineItem
{
/**
Constructs an item from the product and quantity.
@param aProduct the product
@param aQuantity the item quantity
*/
public LineItem(Product aProduct, int aQuantity)
{
theProduct = aProduct;
quantity = aQuantity;
}
/**
Computes the total cost of this line item.
@return the total price
*/
public Money getTotalPrice()
{
Money m = theProduct.getPrice();
for (int i = 2; i <= quantity; i++)
m.add(theProduct.getPrice());
return m;
}
/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
return String.format("%-30s%8s%5d%8s",
theProduct.getDescription(), theProduct.getPrice().format(),
quantity, getTotalPrice().format());
}
private int quantity;
private Product theProduct;
}
/**
Describes a product with a description and a price.
*/
public class Product
{
/**
Constructs a product from a description and a price.
@param aDescription the product description
@param aPrice the product price
*/
public Product(String aDescription, Money aPrice)
{
description = aDescription;
price = aPrice;
}
/**
Gets the product description.
@return the description
*/
public String getDescription()
{
return description;
}
/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
return price;
}
private String description;
private double price;
}
/**
Describes a mailing address.
*/
public class Address
{
/**
Constructs a mailing address.
@param aName the recipient name
@param aStreet the street
@param aCity the city
@param aState the two-letter state code
@param aZip the ZIP postal code
*/
public Address(String aName, String aStreet,
String aCity, String aState, String aZip)
{
name = aName;
street = aStreet;
city = aCity;
state = aState;
zip = aZip;
}
/**
Formats the address.
@return the address as a string with three lines
*/
public String format()
{
return name + "\n" + street + "\n"
+ city + ", " + state + " " + zip;
}
private String name;
private String street;
private String city;
private String state;
private String zip;
}
I hope it helps. Refer screenshot for better understanding of syntax