将构造函数参数传递给JAVA方法

将构造函数参数传递给JAVA方法,java,methods,parameters,constructor,Java,Methods,Parameters,Constructor,我正在攻读软件开发课程,目前我正在学习Java 我正在从事的项目(不用担心,我可以获得外部帮助)给了我一个问题,更重要的是,我不明白他们如何措辞手头的任务。这就是我现在的处境 我需要做的是创建一个包含两个变量的构造函数,然后将其中一个参数传递给一个方法。类和构造函数被称为Order(),方法被称为testQuantity。这是我不明白的。第一个参数productName的值由代码this.productName=productName指定给实例变量productName但是对于第二个参数quan

我正在攻读软件开发课程,目前我正在学习Java

我正在从事的项目(不用担心,我可以获得外部帮助)给了我一个问题,更重要的是,我不明白他们如何措辞手头的任务。这就是我现在的处境

我需要做的是创建一个包含两个变量的构造函数,然后将其中一个参数传递给一个方法。类和构造函数被称为
Order()
,方法被称为
testQuantity
。这是我不明白的。第一个参数
productName
的值由代码
this.productName=productName指定给实例变量
productName
但是对于第二个参数
quantity
它不要求我将值赋给实例变量
quantity
,而是“quantity参数将传递给testQuantity方法。”

好的,这是我到目前为止的代码,它还远没有完成,还有四个类我还没有开始。同时,也可以随意指出您在过程中发现的任何问题。提前感谢您的帮助

package JaveGUIPrototype;
import java.util.Arrays;
/**
*
* @author User
*/
public class Order {
private String productName;
private double price;
private int discount;
private int quantity;
private double total;
private String message;
private boolean isDiscounted;
private boolean isValidOrder;
private static int orderNum = 0;

public Order() {
    // sets validity to false, sets message, increments orderNum by 1
    isValidOrder = false;
    message = "**ERROR**: Order number cannot be totalled as no details have been supplied";
    orderNum = orderNum + 1;
}

public Order(String productName, int quantity) {
    // sets productName variable to productName parameter value
    this.productName = productName;
    this.quantity = quantity;
}




public void testQuantity() {
    // tests the value of the quantity variable
    // quantity is 0 or less, order isn't valid, message explains why
    if(quantity <=0){
        isValidOrder = false;
        message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
    }
    // quantity is greater than 1000, order isn't valid, message explains why
    else if(quantity >1000) {
        isValidOrder = false;
        message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
    }
    // quantity is valid, quantity instance variable assigned value of parameter variable
    else {
        this.quantity = quantity;
    }
}

public void testDiscount(int discount) {
    // tests the value of the discount variable

    // discount is 0 or less, order isn't valid, message explains why
    if(discount <=0) {
        isValidOrder = false;
        message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
    }
    // discount is greater than 50, order isn't valid, message explains why
    else if(discount >50) {
        isValidOrder = false;
        message = "**ERROR**: The discount rate cannot be greater than 50";
    }
    // discount is valid, discount instance variable assigned value of parameter variable
    // isDiscounted variable set to true
    else {
        this.discount = discount;
        isDiscounted = true;
    }
}

String[] products = {"Compass",
                    "Eraser",
                    "Pen",
                    "Pencil",
                    "Pencil Case",
                    "Ruler",
                    "Scissors"};
double[] prices = {4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5};
int indexPos;
double indexVal;

public void getPrice(String productName) {
    // searches products array by value of productName variable
    indexPos = Arrays.binarySearch(products, productName);
    // tests for validity of result
    if (indexPos >-1) {
        // assigns value of prices array by index position
        indexVal = prices[indexPos];
        // assigns prices array value to price variable
        price = indexVal;
    }
    else {
        // product name not valid, not a valid order, message explains why
        isValidOrder = false;
        message = "**ERROR**: Invalid product name";
    }
}

public void calculate() {
    // tests for validity
    if(isValidOrder == true) {
        // tests for discount
        if(isDiscounted == false) {
            // equats total without discount
            total = quantity * price;
        }
        else {
            // equats total with discount
            total = quantity * price - quantity * price * (discount/100);
        }
    }
}

public String getOrderDetails() {
    //tests for validity
    if(isValidOrder == true) {
        //tests for discount
        if(isDiscounted == false) {
            // sets non discounted message details
            message = "Order number: " + orderNum +
                    "Product name: " + productName +
                    "Product price: $" + price +
                    "Order quantity: " + quantity +
                    "Total price: $" + total;
        }
        else {
            // sets discounted message details
            message = "Order number: " + orderNum +
                    "Product name: " + productName +
                    "Product price: $" + price +
                    "Order quantity: " + quantity +
                    "Discount: " + discount + "%" +
                    "Total price: $" + total;
        } 
    }

    return message;

}
封装javegui原型;
导入java.util.array;
/**
*
*@author用户
*/
公共阶级秩序{
私有字符串产品名称;
私人双价;
私人int折扣;
私人整数数量;
私人双总;
私有字符串消息;
私有布尔值被忽略;
私有布尔值isValidOrder;
私有静态int-orderNum=0;
公共秩序(){
//将有效性设置为false,设置消息,将orderNum增加1
isValidOrder=false;
message=“**错误**:由于未提供详细信息,无法合计订单号”;
orderNum=orderNum+1;
}
公共订单(字符串产品名称,整数数量){
//将productName变量设置为productName参数值
this.productName=productName;
这个。数量=数量;
}
public void testQuantity(){
//测试数量变量的值
//数量为0或更少,订单无效,消息解释了原因
如果(数量)
它不要求我将值赋给实例变量
quantity

那么为什么要这样做呢?更改
testQuantity
以接受参数,并传递构造函数的
quantity
参数:

public Order(String productName, int quantity) {
    this.productName = productName;
    testQuantity(quantity);
}

public void testQuantity(int quantity) {
    // Do something with quantity.
    // Copying the original implementation should suffice.
}
它不要求我将值赋给实例变量
quantity

那么为什么要这样做呢?更改
testQuantity
以接受参数,并传递构造函数的
quantity
参数:

public Order(String productName, int quantity) {
    this.productName = productName;
    testQuantity(quantity);
}

public void testQuantity(int quantity) {
    // Do something with quantity.
    // Copying the original implementation should suffice.
}
它不要求我将值赋给实例变量
quantity

那么为什么要这样做呢?更改
testQuantity
以接受参数,并传递构造函数的
quantity
参数:

public Order(String productName, int quantity) {
    this.productName = productName;
    testQuantity(quantity);
}

public void testQuantity(int quantity) {
    // Do something with quantity.
    // Copying the original implementation should suffice.
}
它不要求我将值赋给实例变量
quantity

那么为什么要这样做呢?更改
testQuantity
以接受参数,并传递构造函数的
quantity
参数:

public Order(String productName, int quantity) {
    this.productName = productName;
    testQuantity(quantity);
}

public void testQuantity(int quantity) {
    // Do something with quantity.
    // Copying the original implementation should suffice.
}

您可以创建多个构造函数,只要它们具有不同的签名(不同的参数编号、不同的参数类型),但返回类型不相同

您应该阅读更多关于方法重载的内容,但您需要:

public Order(String productName, int quantity) {
    // sets productName variable to productName parameter value
    this.productName = productName;
    this.quantity = quantity;
}

public Order(String productName) {
    // sets productName variable to productName parameter value
    this.productName = productName;

}

您可以创建多个构造函数,只要它们具有不同的签名(不同的参数编号、不同的参数类型),但返回类型不相同

您应该阅读更多关于方法重载的内容,但您需要:

public Order(String productName, int quantity) {
    // sets productName variable to productName parameter value
    this.productName = productName;
    this.quantity = quantity;
}

public Order(String productName) {
    // sets productName variable to productName parameter value
    this.productName = productName;

}

您可以创建多个构造函数,只要它们具有不同的签名(不同的参数编号、不同的参数类型),但返回类型不相同

您应该阅读更多关于方法重载的内容,但您需要:

public Order(String productName, int quantity) {
    // sets productName variable to productName parameter value
    this.productName = productName;
    this.quantity = quantity;
}

public Order(String productName) {
    // sets productName variable to productName parameter value
    this.productName = productName;

}

您可以创建多个构造函数,只要它们具有不同的签名(不同的参数编号、不同的参数类型),但返回类型不相同

您应该阅读更多关于方法重载的内容,但您需要:

public Order(String productName, int quantity) {
    // sets productName variable to productName parameter value
    this.productName = productName;
    this.quantity = quantity;
}

public Order(String productName) {
    // sets productName variable to productName parameter value
    this.productName = productName;

}

构造函数没有返回类型,因为构造函数不返回。构造函数没有返回类型,因为构造函数不返回。构造函数没有返回类型,因为构造函数不返回。构造函数没有返回类型,因为构造函数不返回。构造函数没有返回类型,因为构造函数不返回。我已将值赋给实例变量thiNK这是我必须将其传递给方法的方式,我在发布代码之前忘记了删除它,谢谢你,这正是我想做的,我在网上找到了示例(我想)但由于变量和方法不同,阅读起来很困难。再次感谢!我已经将值赋给实例变量,认为这是我必须将其传递给方法的方式,我忘记了在发布代码之前删除它,谢谢你,这正是我想要的,我在网上找到了示例(我想)但由于变量和方法不同,阅读起来很困难。再次感谢!我已经将值赋给实例变量,认为这是我必须将其传递给方法的方式,我忘记了在发布代码之前删除它,谢谢你,这正是我想要的,我在网上找到了示例(我想)但由于变量和方法不同,阅读起来很困难。再次感谢!我已经将值赋给实例变量,认为这是我必须将其传递给方法的方式,我忘记了在发布代码之前删除它,谢谢你,这正是我想要的,我在网上找到了示例(我想)但由于变量和方法不同,阅读起来很难。再次感谢!。