I thought I understood how this worked, but maybe I do not.
I intentionally left a field out of my compactdisk class (artist) so that I could create a sub class later, impliment it, and see how that whole thing worked.
Sounded simple, I still think it is, I just think I am doing something wrong.
Here is my original class:
import java.lang.Comparable;
public class Compactdisk implements Comparable
{// begin class
//InventoryCD class has 5 fields
private String name; // Name of cd
private float price; // price of cd
private int itemno; // item number of cd
private int nstock; // how many units in stock
private int i; // cd counter for array
private float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
name = "";
price = 0;
itemno = 0;
nstock = 0;
i = 0;
value = 0;
}
// set values
public void setName(String diskName)
{
name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void seti(int Count)
{
i = Count;
}
// return values
public String getName()
{
return (name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
public int compareTo(Object in)
{
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
}
// returns indivudual inventory value for a disk
public float getValue()
{
return(price * nstock);
}
}// end class
It works ok, does what I want it to do.
I then created this sub class just for cd artist that I thought would just add to it.
import java.util.*;
public class Cdartist extends Compactdisk
{
private String artist; // artist performing on cd
// Artist constructor
public Cdartist()
{
artist = "";
}
// set value
public void setCdArtist(String cdArtist)
{
artist = cdArtist;
}
// return value
public String getCdArtist()
{
return (artist);
}
} //End Class
My understanding was that this new sub class is just an extention of the original and could be used with little or no extra effort. So I changed my Inventory application to include this object accordingly.
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static Compactdisk[] sort(Compactdisk[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(Compactdisk[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[100];
cds[0] = new Compactdisk();
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
String nameInput = input.nextLine(); //read cd name
maxlength ++;
for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
{// begin main While
cds[i] = new Compactdisk();
cds[i].setName(nameInput);
System.out.print("Enter CD Artist Name: "); // prompt for artist name
// input=new Scanner(System.in);
cds[i].setCdArtist(input.nextLine()); // artist name input from user
System.out.print("Enter Price of this CD: "); // prompt for price
cds[i].setPrice(input.nextFloat()); // price input from user.
while (cds[i].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[i].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[i].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[i].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
input=new Scanner(System.in); // internal loop prompt
nameInput = input.nextLine(); //name input from user
maxlength ++;
} // End main While
//System.out.println(toString(cds));
System.out.println(toString(sort(cds)));
System.out.print("Ending Program.");
}// end method main
} // end class Payroll
I am getting a cannot find symbol error on the new code. This usually means there is a problem with that object, I am guessing Inventory cannot find it. Do I have it set up wrong, or is my logic screwy?
Thanks for any help.