I am new to Java, I am six weeks through a nine week class. I am having problems extending a class in my program. My assignment is as follows:
Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.
My code compiles but does not print the contents of the ExtendDVD class. I have the three classes saved as seperate files. Can someone please help me find determine what is wrong. Thank you.
import java.util.Arrays;
public class Inventory2 // declares public class inventory
{
public static void main(String args[]) // starts the program
{
DVD[] dvd = new DVD[3];
dvd[0] = new DVD(1, "Knocked Up", 2, 17.99, "min100");// declares a new item number, dvd title, quantity, and price
dvd[1] = new DVD(2, "Napolean Dynamite", 4, 14.99, "100 Min");
dvd[2] = new DVD(3, "Happy Gilmore", 6, 9.99, "100 Min");
Inventory2 x = new Inventory2();// declare second instance of inventory to sort
x.SortDVD(dvd);
for(int i = 0; i < 3; i++)// loop to call array list and set dvd = i
{
System.out.println(dvd[i]);// print array i
System.out.println();
System.out.println("The item number is " + dvd[i].getItemNumber()); // print item number
System.out.println("Product Title is " + dvd[i].getDvdTitle()); //print dvd title
System.out.println("The number of units in stock is " + dvd[i].getOnHand()); //print on hand quantity
System.out.println("The price of each DVD is $" + dvd[i].getDvdCost()); //print cost
System.out.println("The value of this Item is $" + dvd[i].value()); // print value of all on hand
}
double total = 0;// initiate total inventory as double
for( int counter = 0; counter < dvd.length; counter++ )// calculate total inventory
total += dvd[ counter ].value();
System.out.println();
System.out.println( "The Value of All On Hand Inventory is $" + total );//print the total value of the inventory
}// end main
public void SortDVD(DVD[] theDVD)// method to sort array dvd
{
for (int index = 0; index < theDVD.length - 1; index++)
{
String s1 = theDVD[index].getDvdTitle(); // set title to sort
String s2 = theDVD[index + 1].getDvdTitle();// set title to compare in sort
if (comparewords(s1, s2)) // compares titles to sort
{
DVD temp = theDVD[index];// creates temporary instance of dvd
theDVD[index] = theDVD[index + 1];
theDVD[index + 1] = temp;
index = -1;
}
}
}
private boolean comparewords(String s1, String s2)// creates boolean and compares the two strings
{
boolean islarger = false; //continues to sort until true
for (int index = 0; index < s1.length(); index++) //loop to determine title length
{
if (index < s2.length())
{
if (s1.toLowerCase().charAt(index) > s2.toLowerCase().charAt(index))
{
islarger = true;//sort by longer name
break;
}
if (s1.toLowerCase().charAt(index) < s2.toLowerCase().charAt(index))
{
break;// stop
}
}
else
{
return true;
}
}
return islarger;// return larger moves title higher in the list
}
} // end class Inventory2
public class DVD // declares class inventory
{
int itemNumber;// declares item number as in
String dvdTitle;// declares dvd title as string
int onHand; // declares on hand as int
double dvdCost;// declares cost as double
public DVD(int stockNumber, String title, int inStock, double price, String studio) // constructor
{
itemNumber = stockNumber;// intitiates stock number, title, instock, and
// price
dvdTitle = title;
onHand = inStock;
dvdCost = price;
}
// set DVD StockNumber //sets stock number
public void setItemNumber(int stockNumber)
{
itemNumber = stockNumber;
}
public int getItemNumber() // class item number
{
return itemNumber; // returns item number
} // end method get Dvd Item
public void setDvdTitle(String title) // set DVD Title
{
dvdTitle = title;
}
public String getDvdTitle() // gets and returns DVD Title
{
return dvdTitle;
}
public void setOnHand(int inStock) // Set on hand
{
onHand = inStock;
}
public int getOnHand() // gets and returns on hand
{
return onHand;
} //
public void setDvdCost(double price) // sets dvd cost
{
dvdCost = price;
}
public double getDvdCost() // gets and returns DVD cost
{
return dvdCost;
}
public double value() // calculates the value of stock
{
return dvdCost * onHand;
}
} // end class DVD
public class ExtendDVD extends DVD
{
private String time = "";
public ExtendDVD( int item, String name, int units, double price, String studio )
{
super( item, name, units, price, studio);
this.time = studio;
}
public String getTime()
{
return time;
}
public void setTime( String time )
{
this.time = time;
}
public double getInventoryValue()
{
return 1.05 * getOnHand() * getDvdCost();
}
public double fee()
{
return 0.05 * getOnHand() * getDvdCost();
}
public String tostring()
{
return String.format("\nInventory Information\n%s %s\n%s\t %s\n%s\t%d\n%s\t $%, .2f \n%s\t$%, .2f",
"Item Number ", getItemNumber(),
"Title ", getDvdTitle(),
"Quantity On Hand ", getOnHand(),
"Cost ", getDvdCost(),
"Running Time ", getTime(),
"Fee ", fee(),
"Total Value ", getInventoryValue() );
}
}