Use the following files:
Address.java
/**
Describes a mailing address.
*/
public class Address
{
private String name;
private String street;
private String city;
private String state;
private String zip;
/**
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;
}
}
InvoicePrinter.java
/**
This program demonstrates the invoice classes by printing
a sample invoice.
*/
public class InvoicePrinter
{
public static void main(String[] args)
{
Address samsAddress
= new Address("Sam's Small Appliances",
"100 Main Street", "Anytown", "CA", "98765");
Invoice samsInvoice = new Invoice(samsAddress);
samsInvoice.add(new Product("Toaster", 29.95), 3);
samsInvoice.add(new Product("Hair dryer", 24.95), 1);
samsInvoice.add(new Product("Car vacuum", 19.99), 2);
samsInvoice.add(new FixedPriceItem("Rush charge", 20.00));
System.out.println(samsInvoice.format());
}
}
LineItem.java
/**
Describes a quantity of an article to purchase.
*/
public class LineItem
{
private int quantity;
private Product theProduct;
/**
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 double getTotalPrice()
{
return theProduct.getPrice() * quantity;
}
/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
return String.format("%-30s%8.2f%5d%8.2f",
theProduct.getDescription(), theProduct.getPrice(),
quantity, getTotalPrice());
}
}
Product.java
/**
Describes a product with a description and a price.
*/
public class Product
{
private String description;
private double price;
/**
Constructs a product from a description and a price.
@param aDescription the product description
@param aPrice the product price
*/
public Product(String aDescription, double 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;
}
}