在java中创建子类

时间:2015-04-08 04:42:56

标签: java class subclass super

我需要创建一个子类,创建一个方法来计算产品库存的价值。子类方法还应该为该产品的库存价值增加5%的重新进货费用。它还应修改输出以显示您选择的附加功能和重新进货费用。这就是我到目前为止所拥有的。需要帮助从子类工具获取信息到类Product。这是我的主要内容:

// This is an inventory program part 1 "Section 2", Section 1 is at bottom with "//".
// begin main class
public class inventory {

    public static void main(String[] args) {

        // create array
        Product[] productsobject = new Product[2];

        // initialize array
        productsobject[0]= new Product(5211, "hammer", 1585, 7.48);
        productsobject[1]= new Product(5212, "shovel", 900, 9.48);

        float entireValue;// method to calculate entire inventory
        {
        entireValue = (productsobject[0].getItemTotalValue() + productsobject[1].getItemTotalValue());
        }

        // output products information one at a time
        productsobject[0].printInfo();
        productsobject[1].printInfo();

        //output entire inventory amount
        System.out.println("The entire value of both products is $" + entireValue + ".");
    }
}

这是我的Product课程:

//Begin class Product
    public class Product extends tools{

        //declare data types
        Integer productID;
        String productName;
        Integer quantity;
        Double productPrice;
        float itemTotalValue;
        //end of declaring data types

        //Constructor
        public Product (Integer productID, String productName, Integer quantity, Double productPrice)
        {
            super();
             this.productID= productID;
             this.productName= productName;
             this.quantity = quantity;
             this.productPrice= productPrice;
             // end of Constructor
         }
         //the following is getters and setters
        public Integer getProductID() {
            return productID;
        }

        public void setProductID(Integer productID) {
            this.productID = productID;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public Integer getQuantity() {
            return quantity;
        }

        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }

        public Double getProductPrice() {
            return productPrice;
        }

        public void setProductPrice(Double productPrice) {
            this.productPrice = productPrice;
        }


        // end of getters and setters

        public void printInfo(){// method for sorting array items
          System.out.println("This item is a " + getProductName() + ". The item # is " + getProductID() + ". The number of items available are " + getQuantity() + " and the price per item is $" + getProductPrice() + ". The total value of this item is $" + getItemTotalValue() + "."); 
       }

}
//end class Product

这是我的子类tools

public class tools {

    String typeFinish;

    public tools(String typeFinish)
    {
        this.typeFinish= typeFinish;
    }

    public String getTypeFinish() {
        return typeFinish;
    }

    public void setTypeFinish(String typeFinish) {

        super();
        this.typeFinish = typeFinish;
    }

    public float getItemTotalValue() {
        itemTotalValue = (float) ((productPrice * quantity) * 0.05);
        return itemTotalValue;
    }

    public void setItemTotalValue(float itemTotalValue) {
        this.itemTotalValue = itemTotalValue;
    }

    void typeFinish(){
        System.out.print("This tool has a fiberglass finish and the restocking fee is 5%.");

    }
}

0 个答案:

没有答案