在java中编写构造函数

时间:2017-04-24 14:05:28

标签: java constructor inventory

这是我需要做的事情

  

编写一个构造函数,接受四个字段的参数,每个字段的mutator方法以及每个字段的访问器方法。此外,编写一个实例方法来计算该库存项目的总价值。您可以通过将numberOnHand乘以价格来计算总价值,并将该值作为双精度值返回。

程序已经大部分已经完成,但是我无法弄清楚如何按照预期的方式进行。

这就是我所得到的。

    public PetStoreInventory(String s, String d, int units, double p) {
    //your code goes here
}

public void setDescription( String s){
    //your code goes here
}

public void setDepartment( String d){
  //your code goes here
}

public void setUnitsOnHand ( int units){
    //your code goes here
}

public void setPrice ( double p ){
    //your code goes here
}

public String getDescription(){
    return "";  //so it compiles, you must change this
}

public String getDepartment(){
  return "";  //so it compiles, you must change this
}

public int getUnitsOnHand(){
    return 0;   //so it compiles, you must change this
}

public double getPrice(){
    return 0.0; //so it compiles, you must change this
}

public double calcTotalValue(){
    return 0.0; //so it compiles, you must change this
}

This is the desired output 此输出来自的程序已经生成

2 个答案:

答案 0 :(得分:0)

您需要为这四个字段添加实例变量,然后在代码中使用这些变量。因此在构造函数中,您将设置这些实例变量的值(这些值通常从另一个类发送到构造函数)。

private String description;
private String dept;
private int units;
private double price;

public PetStoreInventory(String s, String d, int units, double p) {
    this.description = s;
    this.dept = d;
    this.units = units;
    this.price = p;
}

public void setDescription( String s){
    // set instance var equal to the passed parameter
    this.description = s; 
}

public void setDepartment( String d){
    this.dept = d;
}

public void setUnitsOnHand ( int units){
    this.units = units;
}

public void setPrice ( double p ){
    this.price = p;
}

public String getDescription(){
    return description;  // return the instance var
}

public String getDepartment(){
   return dept; 
}

public int getUnitsOnHand(){
   return units;   
}

public double getPrice(){
   return price; 
}

public double calcTotalValue(){
   double totalVal = 0.0; //initalise a new variable

   /* the next line must calculate the total using the instance
      variables created at the beginning.
    */
   totalVal =?? //to be completed as it is an assignment

   return totalVal;
}

只有行'totalVal = ??'需要完成。按照问题使用实例变量,如果您需要更多帮助,请告诉我们!

答案 1 :(得分:0)

请注意,建议您在查看此解决方案以尝试自己的解决方案之后,我们会从错误和培训中吸取教训。 我试图让你的理解变得简单

public class PetStoreInventory {
private String description;
private String department;
private int unitsOnHand;
private double price;
private double totalValue;
public PetStoreInventory(String description, String department,int unitsOnHand, double price){
        this.description = description;
        this.department = department;
        this.unitsOnHand = unitsOnHand;
        this.price = price;
    }

    public double calculateTotalValue(){
        return unitsOnHand*price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public int getUnitsOnHand() {
        return unitsOnHand;
    }
    public void setUnitsOnHand(int unitsOnHand) {
        this.unitsOnHand = unitsOnHand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getTotalValue() {
        return totalValue;
    }
    public void setTotalValue(double totalValue) {
        this.totalValue = totalValue;
    }

    public static void main(String[] args){
        PetStoreInventory item1 = new PetStoreInventory("Jack Russel", "Dogs", 4, 359.95);
        String description = item1.getDescription();
        String department = item1.getDepartment();
        int unitsOnHand = item1.getUnitsOnHand();
        double price = item1.getPrice();
        double totalValue = item1.calculateTotalValue();

        System.out.println(description + "\t" + department + "\t" + unitsOnHand + "\t" +price  + "\t" + totalValue);

    }
} 

结果将是例如:

Jack Russel    Dogs    4   359.95    1439.8