/* * Product.java * * Created on May 2, 2002, 8:13 PM */ package SimpleInherit; import IDs.*; import SortSearchEtc.*; /** * * @author redm1 * @version */ public class Product implements Comparable { private Id prodID; private String prodName; private double prodPrice; /** * Creates new Product * Default everythng */ public Product() { prodID = new Id(5); // 5 long id prodName = "Widget"; prodPrice = -9.99; // default hard to come up with } /** * Creates new Product * Given Product name and price */ public Product(String name, double price) { prodID = new Id(5); // 5 long id prodName = name; prodPrice = price; } /** * Creates new Product * Given Product id string, name and price */ public Product(String id, String name, double price) { prodID = new Id(id); // constructor builds from string prodName = name; prodPrice = price; } /////////////////////////////////////////////////////////////////////////// // inspectors /////////////////////////////////////////////////////////////////////////// /** * reports Product name */ public String getName() { return prodName; } /** * reports Product price */ public double getPrice() { return prodPrice; } /** * reports Product ID */ public Id getId() { return prodID; } /** * reports Product ID - as a string */ public String getIdStr() { return prodID.getId(); } /////////////////////////////////////////////////////////////// /// more elaborate information needs /////////////////////////////////////////////////////////////// /** * Produce a string representation for the Product */ public String toString() { String res = "Product: " + getIdStr() + " "; res += prodName.toUpperCase(); res += " Price: "; res += prodPrice; return res; } /////////////////////////////////////////////////////////////// // Mutators /////////////////////////////////////////////////////////////// /** * change Product name */ public void setName(String name) { prodName = name; } /** * change Product price */ public void setPrice(double price) { if (price <= 0.00) { System.out.println("ERROR - invalid product price"); } else { prodPrice = price; } } /** * raise Product price */ public double raisePrice (double pct) { // not a raise really - defend - ensure this method is used for a raise */ // non-exception handling version if (pct <= 0.0) { System.out.println("ERROR - invalid increase pct"); return -9; // indicate problem } else { prodPrice = prodPrice + (prodPrice * pct); } // pass back new pay rate return prodPrice; } /** * change Product ID * given a string */ public void setprodID (String id) { prodID = new Id(id); } /** * Compare two Products - reporting if they are equal or if one is larger than other * Uses Product ID */ public int compareTo(Object obj) { Product toCompare = (Product) obj; // cast to Product for comparison // get to ID Id idToCompare = toCompare.getId(); // use Id class' compareTo method int res = this.getId().compareTo( idToCompare); return res; } public static void main( String args[]) { System.exit(0); } }