I need help with a loop for the inventory program
CODE
public class Inventory2 {
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);
inventory.display();
System.out.println("\nEnd CD inventory\n");
}
} // end class CD
CODE
class CD {
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;
private int valCDs;
private int namCDs;
private int priCDs;
///Construct a new inventory for CDs
Inventory() {
cds = new CD[25];
numCDs = 0;
}
public void add(CD cd) {
cds[numCDs] = cd;
++numCDs;
}
public void display() {
System.out.println("\nCD Inventory\n");
System.out.println("PartID Name Copies Price Value");
}
} // end class Inventory
*Edited to add the [ code] tags to make the code readable. Please
This post has been edited by pbl: 18 Dec, 2008 - 04:31 PM