代写 Java 第一题

第一题
Create a Receipt class that represents a customer’s 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里面什么代码都没给