I have an inventory program written to include an array and a method to calculate total cost for all inventory items entered. I now have to include a subclass that overrides the original to include "one unique feature". I created a new file named ItemDetails to set up for the subclasses of the original Item. I need to include one unique feature and calculate the value of the inventory and calculate a 5% restocking fee in this subclass. Do I just transfer some of the relevant lines into the other class? Or do I write some code twice? I don't know what to do next. Any help is useful. Thanks. This is what I have so far:
package inventory3;
public class ItemDetails extends Items
{
public static void override()
    {
    private String Name;
    private double pNumber, Units, Price;
public ItemDetails()
        {
        }
    }
}
This is the Item class file that it is supposed to override:
package invent开发者_JS百科ory3;
import java.lang.Comparable;                
    public class Items implements Comparable
{
       private String Name;
       private double pNumber, Units, Price;
public Items()
    {
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
    }
public int compareTo(Object item)
    {
  Items tmp = (Items) item;
    return this.getName().compareTo(tmp.getName());
    } 
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
    {
    Name = productName;
    pNumber = productNumber;
    Units = unitsInStock;
    Price = unitPrice;
    }
    //setter methods
public void setName(String n)
    {
    Name = n;
    }
public void setpNumber(double no)
    {
    pNumber = no;
    }
public void setUnits(double u)
    {
    Units = u;
    }
public void setPrice(double p)
    {
    Price = p;
    }
//getter methods
public String getName()
    {
return Name;
    }
public double getpNumber()
    {
return pNumber;
    }
public double getUnits()
    {
return Units;
    }
public double getPrice()
    {
return Price;
    }
public double calculateTotalPrice()
    {
    return (Units * Price);
    }
public static double getCombinedCost(Items[] item)          
    {
    double combined = 0;        
    for(int i =0; i < item.length; ++i)
        {
        combined = combined + item[i].calculateTotalPrice();        
        } 
    return combined;
    }
}
You simply declare a method with the same signature as the method in the parent class. So yours would look like:
package inventory3;
public class ItemDetails extends Items {
    private String Name;
    private double pNumber, Units, Price;
    public ItemDetails(String Name, double pNumber, double Units, double Price) {
        this.Name = Name;
        this.pNumber = pNumber;
        this.Units = Units;
        this.Price = Price;
    }
    // getters and setters....
    // The @Override is optional, but recommended.
    @Override
    public double calculateTotalPrice() {
        return Units * Price * 1.05; // From my understanding this is what you want to do
    }
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论