I need to make a subclass for the following code. I was planning on using the extends keyword but the instructor says to use =new keyword. I do not understand the = new keyword. Can someone please explain the difference and possibly provide an example based on the following code.
Code:
import java.util.Scanner;
public class inventorysupplys2 { // start of class
public static void main(String[] args) { // start of program
Scanner input = new Scanner( System.in );
int c;
System.out.printf( "\n\nHow many new movies do you want to enter? ");
c = input.nextInt();
input.nextLine();
inventorysupplystock2[] supply = new inventorysupplystock2[c];
String name; // for the name
double number; // for the item number
double quantity; // number of units in stock
double price; // price per item
double value = 0; // value of that item
double totalValue = 0; // value of whole inventory
int a = 0;
while (a < c){ // start of the loop getting the info for how ever many you are putting in
supply[a] = new inventorysupplystock2();
System.out.printf( "What is the name of the movie being entered: ");
name = input.nextLine();
supply[a].setName(name);
System.out.printf( "What will be this movie's item number: ");
number = input.nextDouble();
input.nextLine();
supply[a].setNumber(number);
System.out.printf( "How many are you entering into stock: ");
quantity = input.nextDouble();
input.nextLine();
supply[a].setquantity(quantity);
System.out.printf( "How much did this movie cost: ");
price = input.nextDouble();
input.nextLine();
supply[a].setPrice(price);
value = supply[a].getquantity() * supply[a].getPrice();
supply[a].setValue(value);
supply[a].setCount(a);
a++;
} // end of loop getting info
java.util.Arrays.sort(supply); //this sorts the stuff
System.out.printf( "Item number Name Quantity Price Value \n");
a = 0;
while (a < c){ // total value of inventory
supply[a].displayItems();
totalValue += supply[a].getValue();
a++;
}
// total value
System.out.printf( "The total value of the inventory is: %.2f", totalValue );
} // end of program
} // end of class
************************************************************
public class inventorysupplystock2 implements Comparable{
protected int count;
protected int quantity;
protected String productName;
protected double itemNumber;
protected double quantityNumber;
protected double unitPrice;
protected double unitValue;
public inventorysupplystock2() {
}
public int compareTo(Object o) {
return productName.compareTo(((inventorysupplystock2) o).getName());
}
public void displayItems(){
System.out.printf( "%-2s %-2s %-2s %.2f %.2f\n",
itemNumber, productName,
quantityNumber, unitPrice, unitValue);
}
// total value per movie
public void setValue(double value){
unitValue = value;
}
public double getValue(){
return unitValue;
}
public void setCount( int a ){
count = a;
}
// movie name
public void setName( String name ) {
productName = name;
}
public String getName() {
return productName;
}
// item number
public void setNumber( double number ) {
itemNumber = number;
}
public double getNumber() {
return itemNumber;
}
// quantity of that movie
public void setquantity( double quantity ) {
quantityNumber = quantity;
}
public double getquantity() {
return quantityNumber;
}
// price for that movie
public void setPrice( double price ) {
unitPrice = price;
}
public double getPrice(){
return unitPrice;
}
}
Comment