Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 1个字符串需要提供2个输出_Java_String - Fatal编程技术网

Java 1个字符串需要提供2个输出

Java 1个字符串需要提供2个输出,java,string,Java,String,它希望我返回“message”字符串,该字符串将显示以下输出。如果订单有效但未折扣,则输出中不需要折扣,但如果订单有效且折扣大于输出中需要折扣。现在,我尝试了一些事情,但我就是想不出来=/ getOrderDetails方法的主要任务是返回消息实例 变量如果订单有效且未打折,则需要设置 消息,使其显示如下(无折扣实例 变量)使用适当的实例变量作为 如下: - Order number: 1 - Product name: Pencil - Product Price: $0.6 - Order

它希望我返回“message”字符串,该字符串将显示以下输出。如果订单有效但未折扣,则输出中不需要折扣,但如果订单有效且折扣大于输出中需要折扣。现在,我尝试了一些事情,但我就是想不出来=/

getOrderDetails方法的主要任务是返回消息实例 变量如果订单有效且未打折,则需要设置 消息,使其显示如下(无折扣实例 变量)使用适当的实例变量作为 如下:

- Order number: 1
- Product name: Pencil
- Product Price: $0.6
- Order Quantity: 6
- Total Price: $3.6
如果订单有效且打折,则需要更改消息,使其包括 折扣实例变量如下所示:

- Order number: 1
- Product name: Pencil
- Product Price: $0.6
- Order Quantity: 6
- Total Price: $3.6
- Order number: 1
- Product name: Pencil
- Product Price: $0.6
- Order Quantity: 6
- Discount: 10%
- Total Price: $3.24
如果顺序无效,则仅返回消息实例变量

public class Order {

    public String productName; //Name
    public double price;  //Price
    public double discount;  //Discount
    public int quantity;  //Quantity
    public double total; //Total for the price multiplied with quantity (with or without discount)
    public String message; //Message
    public boolean isDiscounted = false; //Set to false, but if it's true, the discount comes in
    public boolean isValidOrder = true; //Set to true, but if it's false, the Order becomes invalid and Message comes in
    public static int orderNum = 0; //Order number set to 0 as default


        public void setProductName(String productName) {
            this.productName = productName;
        }//Set the name
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }//Set the price
        public void setDiscount(double discount) {
            this.discount = discount;
        }//Set the discount
        public void setPrice(double price) {
            this.price = price;
        }//Set the price
        public void setOrderDetails(boolean isValidOrder, boolean isDiscounted) {
            this.isValidOrder = isValidOrder;
        }//Set the Order Details
        public String getProductName() {
            return productName;
        }//Get the name
        public int getQuantity() {
            return quantity;
        }//Get the price
        public double getDiscount() {
            return discount;
        }//Get the discount
        public double getPrice() {
            return price;
        }//Get the price

        public Order() { //Constructor 1
            isValidOrder = false;
            message =  "**ERROR** : Order number cannot be totalled as no details have been supplied.";
            orderNum++;
        }

      public Order(String productName, int quantity){  //Constructor 2
          this.productName = productName;
          this.quantity = quantity;
          getPrice(this.productName);


          if(isValidOrder != false){
              calculate();
          }
          orderNum++;

      }

    public Order(String productName, int quantity, double discount){ //Constructor 3
        this.productName = productName;
        testQuantity(quantity);
        getPrice(productName);
                this.discount = discount;

          if(isValidOrder != false){
              calculate();
          }
                  orderNum++;
    }

    public String getOrderDetails(){ //Order Details

        if(isValidOrder == true && isDiscounted == true){

            message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + this.quantity + "\n" + "Discount: " + this.discount + "%" + "\n" + "Total Price: $" + total;  

        } 
                else if(isValidOrder == true && isDiscounted == false){

            message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + this.quantity + "\n" + "Total Price: $" + total;  
        }  
                return message; 
    }


    public void calculate(){ //Calculate for total
        if(this.isDiscounted == false){
            total = quantity * price;
        } 
                else {
            total = quantity * price - quantity * price * (discount / 100 );  
        }
    }

    public void getPrice(String productName){ //Switch so you can find what item ordered and allocate the correct price
        switch(productName){
        case "Pencil":
            this.price = 0.6;
            break;
        case "Pen":
            this.price = 0.3;
            break;
        case "Ruler":
            this.price = 1.2;
                        break;
        case "Pencil Sharpener":
            this.price = 0.3;
            break;
        case "Compass":
            this.price = 4.5;
            break;
        case "Eraser":
            this.price = 4.5;
            break;
        case "Scissors":
            this.price = 2.5;
                        break;
        case "Pencil Case":
            this.price = 10.0;
            break;
        default:
            this.price = 0.0;
            this.isValidOrder = false;
            this.message = "**ERROR** : Invalid product name";
                        break;
                }
    }

    public void testDiscount(double discount) { //Testing for a Discount
        if (discount <=0) {
            isDiscounted = false;
            message = "**ERROR** : The discount rate cannot be lower than or equal to 0.";
        }
        else if (discount >50) {
            isDiscounted = false;
            message = "**ERROR** : The discount rate cannot be higher than 50.";
        } 
       else {
       this.discount = discount;        
       this.isDiscounted = true;     
        }  
    }


    public void testQuantity(int quantity){  //Testing for Quantity
           if(quantity <=0) {
                isValidOrder = false;
                message = "**ERROR** : Invalid quantity. Quantity cannot be 0 or less.";
                }
            else if (quantity >1000) {
                isValidOrder = false;
                message = "**ERROR** : Invalid quantity. Quantity cannot be greater than 1000.";
            }  
                else {
                isValidOrder = true;
                this.quantity = quantity;
            }
    }

}
公共类秩序{
公共字符串productName;//名称
公共双价;//价格
公共双重折扣;//折扣
公共int数量;//数量
公共双倍合计;//价格乘以数量的合计(有无折扣)
公共字符串消息;//消息
public boolean isDiscounted=false;//设置为false,但如果为true,则折扣将生效
公共布尔值isValidOrder=true;//设置为true,但如果为false,则顺序无效,消息将进入
public static int orderNum=0;//订单号默认设置为0
公共无效setProductName(字符串productName){
this.productName=productName;
}//设定名称
公共无效设置数量(整数数量){
这个。数量=数量;
}//定价
公共折扣(双倍折扣){
这个。折扣=折扣;
}//设定折扣
公共定价(双倍价格){
这个价格=价格;
}//定价
public void setOrderDetails(布尔值isValidOrder,布尔值isDiscounted){
this.isValidOrder=isValidOrder;
}//设置订单详细信息
公共字符串getProductName(){
返回产品名称;
}//得到名字
公共整数getQuantity(){
退货数量;
}//拿到价钱
公共双折{
退货折扣;
}//打折
公开双价{
退货价格;
}//拿到价钱
公共秩序(){//1
isValidOrder=false;
message=“**错误**:由于未提供详细信息,订单号无法合计。”;
orderNum++;
}
公共订单(字符串产品名,整数数量){//2
this.productName=productName;
这个。数量=数量;
getPrice(此.productName);
if(isValidOrder!=假){
计算();
}
orderNum++;
}
公共订单(字符串产品名、整数数量、双重折扣){//3
this.productName=productName;
测试数量(数量);
getPrice(产品名称);
这个。折扣=折扣;
if(isValidOrder!=假){
计算();
}
orderNum++;
}
公共字符串getOrderDetails(){//订单详细信息
如果(isValidOrder==true&&isDiscounted==true){
message=“订单号:“+orderNum+”\n“+”产品名称:“+productName+”\n“+”产品价格:$“+Price+”\n“+”订单数量:“+this.Quantity+”\n“+”折扣:“+this.Discount+”%“+”\n“+”总价:$“+Total;
} 
else if(isValidOrder==true&&isDiscounted==false){
message=“订单号:“+orderNum+”\n“+”产品名称:“+productName+”\n“+”产品价格:$“+Price+”\n“+”订单数量:“+this.Quantity+”\n“+”总价:$”+Total;
}  
返回消息;
}
public void calculate(){//calculate for total
if(this.isDiscounted==false){
总数=数量*价格;
} 
否则{
总计=数量*价格-数量*价格*(折扣/100);
}
}
public void getPrice(String productName){//开关,以便您可以找到订购的商品并分配正确的价格
交换机(产品名称){
“铅笔”盒:
该价格=0.6;
打破
案例“笔”:
该价格=0.3;
打破
案例“标尺”:
这个价格=1.2;
打破
“卷笔刀”一案:
该价格=0.3;
打破
案例“罗盘”:
该价格=4.5;
打破
案例“橡皮擦”:
该价格=4.5;
打破
案例“剪刀”:
这个价格=2.5;
打破
“铅笔盒”:
这个价格=10.0;
打破
违约:
这个价格=0.0;
this.isValidOrder=false;
this.message=“**错误**:产品名称无效”;
打破
}
}
公共作废测试折扣(双重折扣){//测试折扣
如果(折扣50){
isDiscounted=假;
message=“**错误**:折扣率不能高于50。”;
} 
否则{
这个。折扣=折扣;
this.isDiscounted=true;
}  
}
公共void testQuantity(int-quantity){//测试数量
若有(数量1000){
isValidOrder=false;
message=“**错误**:数量无效。数量不能大于1000。”;
}  
否则{
isValidOrder=true;
这个。数量=数量;
}
}
}
Cr