I want to learn more about how inheritance works, more like I NEED to learn more about it if I am to get any better at this.
In my code I have the value of a cd defined as how many cds are in stock times the price of the cd. I did this in my first class Compactdisk.
Now I have created a sub class that adds a new parameter in to my array, and that all makes sence to me now.
What if I want to alter the output of a method I have already done though? Value for instance. With my new subclass I want to make Value mean (price X stock X restock) where restock is going to be a 5% restocking fee.
I should not have to alter Compactdisk, as CdwArtist is a sub and inherits everything, so I added what you see below to CdwArtist. I think it is right, but how would I then get Inventory to grab this new Value instead of the old Value?
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static CdwArtist[] sort(CdwArtist[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(CdwArtist[] 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
CdwArtist[] cds = new CdwArtist[100];
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
for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
{// begin main While
cds[i] = new CdwArtist();
cds[i].setName(nameInput);
System.out.print("Enter CD Artist Name: "); // prompt for artist name
CdwArtist artist = new CdwArtist(input.nextLine());
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
My first 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
and my new sub class, with my added code in bold.
import java.util.*;
public class CdwArtist extends Compactdisk
{
public String artist; // artist performing on cd
public int restock; // restocking percentage
// Artist constructor
public CdwArtist()
{
artist = "";
[B]restock = .05[/B]
}
public CdwArtist(String in)
{
artist=in;
}
// get value
public void setArtist(String in)
{
artist=in;
}
[B] public void setRestock(float cdRestock)
{
restock = cdRestock;
}[/B]
// return value
public String getArtist()
{
return (artist);
}
[B]// returns indivudual inventory value for a disk
public float getValue()
{
return((price * nstock)* restock);
}[/B]
} //End Class
I was hoping that I could just add a new method with the same name, and since everything is just inherited that my Inventory class would take the last inherited value for Value. It did not work. I still only get the original value that I set up in Compactdisk.
Am I making any sense?