簡體   English   中英

無法顯示計算結果

[英]Can't get calculation to display

因此,我有一個項目需要在其中創建:

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

但是總和在此處顯示不正確,當我將其設置為“(Compass“,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);
  }
}

整數除法!

這條線

total = quantity * price - quantity * price * (discount / 100);

discount除以100。兩者均為整數,因此,如果折扣低於100,則此值為0。

您只需要使其成為雙重操作即可:

total = quantity * price - quantity * price * (discount / 100.0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM