[SOLVED] Java

$25

File Name: Java_.zip
File Size: 47.1 KB

5/5 - (1 vote)

Create aReceiptclass that represents a customers receipt upon checkout. It should have the following public methods:
public Receipt ()
A constructor to create a new Receipt object.
public void add (ProductInfo pInfo)
Adds an instance of a product to the Receipt. This is called each time a product is purchased, such that multiple instances of the same product can be added to the receipt.
public String getPrintedReceipt ()
Returns a String representation of the Receipt suitable for printing. The output must match the following format:
Item Unit Qty Price
– –
Bic Pen (Blu 0.99 1 0.99
Bic Pen (Red 0.99 1 0.99
Bic Pen (Spa 2.99 3 8.97

Total 10.95

Specifically:
The output is tabbed appropriately so that the receipt maintains columns as above
The Item column shows the first 12 characters of product descriptions
Prices are formatted to 2 decimal places but do not contain a currency symbol
Tax is not calculated and is not included in the total
Each product appears once, and the quantity and subtotal reflect the number of items purchased for this product type
Items are sorted by SKU, but SKU is not displayed on the output
Hint:util.Collections is a helper class that provides (among other things) a method for sorting a Collection

class ProductInfo.java

/**
* Represents information about a specific product.
* @author Derek Reilly
*/
public class ProductInfo implements Comparable {
private String sku;
private String desc;
private double price;

/**
* A constructor with three parameters: sku, representing the SKU for the product, desc, a product description,
*and unitPrice, the price for a single Item of this product type.
* @param sku product Stock Keeping Unit
* @param desc product description
* @param unitPrice price per unit
*/
public ProductInfo (String sku, String desc, double unitPrice) {
this.sku = sku;
this.desc = desc;
this.price = unitPrice;
}
/**
* Returns the SKU
* @retrurn the SKU for the product
*/
public String getSKU () { return sku; }

/**
* Get the product description
* @return the product description
*/
public String getDescription () { return desc; }

/**
* Set the product description
* @param description the new description
*/
public void setDescription (String description) { desc = description; }

/**
* Get the unit price
* @return the price
*/
public double getUnitPrice () { return price; }

/**
* Set the price per unit
* @param description the new price
*/
public void setUnitPrice (double price) { this.price = price; }

/**
* Returns a String representation of this ProductInfo
* @return a description of this ProductInfo object
*/
public String toString () {
return desc + , SKU + sku + , $ + String.format (%.2f, price);
}

/**
* Tests if this ProductInfo object is equal to the object passed in.
* Equality depends on SKU comparison.
* @param o the object to compare
* @return true if equal, false if not
*/
public boolean equals (Object o) {
return (o instanceof ProductInfo) && ((ProductInfo) o).getSKU ().equals (this.getSKU ());
}

/**
* Generates a hash code value for this ProductInfo object, based on SKU.
* @return the hash code
*/
public int hashCode () { return this.getSKU().hashCode (); }

/**
* Compares this ProductInfo object with the object passed in.
* Comparison is based on lexical ordering of SKU.
* @param other the object to compare
* @return -1,0,1 if this ProductInfo comes before, equal to, comes after the object to compare, reespectively.
*/
public int compareTo (ProductInfo other) {
return this.getSKU ().compareTo (other.getSKU ());
}

}

class Receipt.java
class

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Java
$25