无法获得计算显示

时间:2016-05-21 06:07:19

标签: java

所以我有一个项目需要创建它:

 public void calculate (){
         if (isDiscounted == true){
                total = quantity * price - quantity * price * (discount/100);
            }
         else {
                total = quantity * price;
                 }
          }

但总数在这里没有正确显示,当我将其设置为say(“指南针”,20,30)时,折扣不适用,只有正常总数:

 public String getOrderDetails(){
        message = message;
        if(isValidOrder == true && isDiscounted == false){
            message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;  
        }
        else if(isValidOrder == true && isDiscounted == true){
            message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" +"Discount : " + discount + "%" + "\n" + "Total Price: $" + total;  
        }  
        else {
            return message;  
        }
    return message; 
}

我是一个非常初学的程序员,所以我不确定你是否明白我在说什么。基本上我无法将其链接起来:/

以下是代码的其余部分:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package order;
import java.util.Arrays;
/**
 *
 * @author Alexander
 */
public class Order {
  private static String[] products = {
    "Compass", "Eraser", "Pen", "Pencil", "Pencil Case", "Pencil Sharpener", "Ruler", "Scissors"
  };
  private static double[] prices = {
    4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5
  };
  public static int orderNum = 0; // 
  private String productName;
  private double price = 0;
  private int discount = 0;
  private final int minDiscount = 0;
  private final int maxDiscount = 50;
  private int quantity = 0;
  private final int minQuantity = 0;
  private final int maxQuantity = 1000;
  private double total;
  private String message;
  private boolean isDiscounted = false;
  private boolean isValidOrder = true;

  public void calculate() {
    if (isDiscounted == true) {
      total = quantity * price - quantity * price * (discount / 100);
    } else {
      total = quantity * price;
    }
  }


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

  public Order(String productName, int quantity) {
    this.quantity = quantity;
    this.productName = productName;
    testQuantity(quantity);
    getPrice(productName);
    if (isValidOrder == true) {
      calculate();
    }
    orderNum++;
  }

  public Order(String productName, int quantity, int discount) {
    this.productName = productName;
    this.quantity = quantity;
    this.discount = discount;
    testQuantity(quantity);
    testDiscount(discount);
    getPrice(productName);
    if (isValidOrder != false) {
      calculate();
    }
    orderNum++;
  }



  private void getPrice(String pce) {
    Arrays.sort(products);
    int searchProductArray = Arrays.binarySearch(products, pce);
    if (searchProductArray >= 0) {
      price = prices[searchProductArray];
      productName = products[searchProductArray];
      isValidOrder = true;
    } else {
      price = 0.0;
      isValidOrder = false;
      message = "**ERROR**: Invalid product name";
    }
  }




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

  public void testDiscount(int discount) {
    boolean isDiscounted = false;
    if (discount <= minDiscount) {
      message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
    } else if (discount > maxDiscount) {
      message = "**ERROR**: The discount rate cannot be greater than 50";
    } else {
      this.discount = discount;
      this.isDiscounted = true;
    }
  }

  public String getOrderDetails() {
    message = message;
    if (isValidOrder == true && isDiscounted == false) {
      message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
    } else if (isValidOrder == true && isDiscounted == true) {
      message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
    } else {
      return message;
    }
    return message;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Order O1 = new Order("Compass", 20, 30);




    System.out.println(O1.total);

    OrderLaunch frame = new OrderLaunch();
    frame.setVisible(true);
  }
}

1 个答案:

答案 0 :(得分:1)

整数分区!

这一行

Test2 <- function(j,card=5,step=10) {

  r <- t(rmultinom(j,step,rep(1,card)))
  s <- apply(r, 1:2, function(x) if(x > 0) sample(3,1,prob=runif(3)) else 1)

  return(s)


}

total = quantity * price - quantity * price * (discount / 100); 除以100.两者都是整数,因此如果折扣低于100,则计算结果为0.

你只需要进行双重操作:

discount