Here is my code for an assignment, but i need to add a sub class and a 5% restock fee to the program. I was wondering if someone could shoe me an example of adding a sub class to my cd inventory. The restock fee I can worry about later.
CODE
import java.util.Arrays;
public class Inventory3 {
public static void main(String[] args) {
CD cd;
Inventory inventory = new Inventory();
cd = new CD(001, "Master of Puppets", 2, 27.98f);
inventory.add(cd);
cd = new CD(022, "Heaven and Hell", 1, 9.99f);
inventory.add(cd);
cd = new CD(013, "Throwing Copper", 10, 17.95f);
inventory.add(cd);
cd = new CD(004, "Back in Black", 4, 8.99f);
inventory.add(cd);
cd = new CD(006, "Silver Side Up", 5, 10.99f);
inventory.add(cd);
inventory.display();
System.]out.println("\nEnd CD inventory\n");
}
} // end class CD
CODE
class CD implements Comparable { // compares array
private int partID;
private String name;
private int copies;
private float price;
CD(int partID, String name, int copies, float price) {
this.partID = partID;
this.name = name;
this.copies = copies;
this.price = price;
}
public String getName() {
return name;
}
public float value() {
return copies * price;
}
public int compareTo(Object o) {
CD cd = (CD) o;
return getName().compareTo(cd.getName());
}
public String toString() {
return String.format(" %03d %-22s %3d $%6.2f $%7.2f",
partID, name, copies, price, value());
}
} // end class CD
CODE
class Inventory {
private CD[] cds;
private int numCDs;
///Construct a new inventory for CDs
Inventory() {
cds = new CD[25];
numCDs = 0;
}
public void add(CD cd) {
cds[numCDs] = cd;
++numCDs; //add cd to collection
sort();
}
private void sort() { //This sorts the array
Arrays.sort(cds, 0, numCDs);
}
public double value() { //This is the value method
double total = 0;
for (int i = 0; i < numCDs; i++)
total += cds[i].value();
return total;
}
public void display() {
System.out.println("\nCD Inventory\n");
for (int i = 0; i < numCDs; i++)
System.out.println(cds[i]);
System.out.printf("\nThe total value of the inventory is $%.2f\n", value());
}
} // end class Inventory
*Edited to add the [ code] tags. Please

so DIC can read your code and help you
This post has been edited by pbl: 20 Dec, 2008 - 09:12 PM