Pergunta

I'm trying to build a program that has certain requirements, the main being I have a class, and then make a subclass that adds a feature. I create the class DVD, and then I create the subclass.

I'm adding a method to add the year to the list, as well as a restocking fee which will be added to the final inventory value that prints. I built the subclass, created the overriding methods, but it is not being added to the output displayed. Not only that, but it is placing the input year in the wrong place. I am not getting any errors, it just acts like the subclass doesn't exist, even though my DVD class says that some of the methods are being overridden.

I'm thinking I must be missing something where I am supposed to call the new method, and maybe I read the resource wrong, but it sounded like I only needed to call the DVD class, and the methods I wanted overridden would be overridden automatically. I'd prefer to just add this information to the superclass, but it is a requirement for an assignment.

So I'm wondering how do I actually go about calling these override methods when I need them to add these new features? I keep seeing resources telling me how to create them, but not actually implement them.

From my main method, I call the dvd class and then print it. however, it only prints what's in the original dvd class, except for the odd addition of adding the year to where the product ID should be.

public class DVD {



String name;
int id;
int items;
double cost;

//default constructor
public DVD() {
    name = "";
    id = 0;
    items = 0;
    cost = 0.0;
}//end default constructor

//constructor to initialize object
public DVD(String dvdName, int itemNum, int quantity, double price) {
    name = dvdName;
    id = itemNum;
    items = quantity;
    cost =  price;
}//end constructor


//method to calculate value
public double getInventoryValue() {
       return items * cost;
}

//method to set name
public void setdvdName(String dvdName){
    this.name = dvdName;
}

//method to get name
public String getName(){
    return name;
}

//method to set id
public void setitemNum( int itemNum){
    this.id = itemNum;
}

//method to get id
public int getId(){
    return id;
}

//method to set items
public void setquantity(int quantity){
    this.items = quantity;   
}

//method to get items
public int getItems(){
    return items;
}

//method to set cost
public void setprice( double price){
    this.cost = price;
}

//method to get cost
public double getCost(){
    return cost;
}

/**
 *
 * @return
 */

public String toString() {

    return "DVD Name: " + getName() +
           "ID: " + getId() +
           "Items: " + getItems() +
           "Cost: " + getCost() + 
           "Total Value: " +getInventoryValue();
}
}

-

public class ExtendedDVD extends DVD{
double restockFee;
int year;

public ExtendedDVD(){
    year = 0;
}
public ExtendedDVD(int year) {
    this.year = year;
}

public void setRestockFee(){
    this.restockFee = 0.05;
}

public double getRestockFee(){
    return restockFee;
}


public void setYear(){
    this.year = 0;
}

public int getYear(){
    return year;
}

@Override
public double getInventoryValue(){
    double value1 = super.getInventoryValue();
    double value = restockFee * value1;
    double totalInventoryValue = value + super.getInventoryValue();
    return totalInventoryValue;
}

@Override
 public String toString(){
    return super.toString() + "Year" + getYear();
}
}

}

public class Inventory {

DVD[] inventory = new DVD[5];
int current = 0;
private int len;

public Inventory(int len){
    inventory = new DVD[len];

}

public double calculateTotalInventory() {
    double totalValue = 0;
    for ( int j = 0; j < inventory.length; j++ ) 
        totalValue += inventory[j].getInventoryValue();
    return totalValue;
}

/**
 *
 * @param dvd
 * @throws Exception
 */
public void addDVD(DVD dvd) throws Exception {
    if (current < inventory.length) {
        inventory[current++]=dvd;
    }else {
        Exception myException = new Exception();
        throw myException;

    }
    }
void sort() {
      for (DVD inventory1 : inventory) {
        len = current;
    }

      for (int i=0; i<len;i++) {
        for(int j=i;j<len;j++) {
            if (inventory[i].getName().compareTo(inventory[j].getName())>0) {
                DVD temp = inventory[j];
                inventory[j] = inventory[i];
                inventory[i] = temp;
            }
        }
    }
}




  public int getNumberOfItems() {
    return current;
}


    public void printInventory() {
    System.out.println("Current Inventory:");
    for(int i=0;i<current;i++) {
        System.out.println(inventory[i]);
    }
    System.out.println("The total value of the inventory is:"+calculateTotalInventory());
    }
}

-

public class inventoryprogram1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args){


    boolean finish = false;
    String dvdName;
    int itemNum;
    int quantity;
    double price;
    int year = 0;

    Inventory inventory = new Inventory(5);
    while (!finish) {
        Scanner input = new Scanner(System.in); // Initialize the scanner
        System.out.print("Please enter name of DVD: ");
        dvdName = input.nextLine();
        if (dvdName.equals("stop")) {
            System.out.println("Exiting Program");
            break;
        } else {
            System.out.print("Please enter Product Number: ");
            itemNum = input.nextInt();
            System.out.print("Please enter units: ");
            quantity = input.nextInt();
            System.out.print("Please enter price of DVD: ");
            price = input.nextDouble();
            System.out.print("Please enter production year: ");
            itemNum = input.nextInt();

            DVD dvd= new DVD(dvdName,itemNum,quantity,price);

            try {
              inventory.addDVD(dvd);
            }catch( Exception e) {
                System.out.println("Inventory is full.");
                break;
            }

            System.out.println("DVD: " + dvd);

        }//end else

    }
       inventory.sort();
       inventory.printInventory();
}
}
Foi útil?

Solução

if you want to use the new methods that you wrote in ExtendedDVD you need to instantiate that class you are still calling the original dvd class so you will still get those methods.

for example

DVD dvd = new DVD(dvdName, itemNum, quantity, price);

and

DVD Dvd = new ExtendedDVD(dvdName, itemNum, quantity, price);

are two different things

also if you look in your main method you are assigning itemNum twice that is why it is showing you the year

Outras dicas

In the main method you just instantiate a DVD object, not an ExtendedDVD object.

replace

DVD dvd= new DVD(dvdName,itemNum,quantity,price);

by something like

DVD dvd= new ExtendedDVD(year);

And obviously, you may want another constructor in ExtendedDVD

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top