无法获取消息显示
问题描述:
我有一个getOrderDetails()方法,但是当我输入(“Compass”,2000,20)时,我无法让它显示应该通过的错误消息。 *错误数量必须小于1000" 我在运行程序时,它会计算,而不是显示错误消息2000无法获取消息显示
public String getOrderDetails(){
message = message;
if(isValidOrder == true && isDiscounted == false){
message = "Order Number: " + orderNum + "\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: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" +"Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
}
else {
return message;
}
return message;
}
这是该消息的代码:。。
public void testQuantity(int quantity){
boolean isValidOrder = true;
if (this.quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
}
else if (this.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 (this.discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
}
else if (this.discount > maxDiscount) {
message = "**ERROR**: The discount rate cannot be greater than 50";
}
else {
this.discount = discount;
this.isDiscounted = true;
}
}
这是我的代码的其余部分:
/*
* 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.0);
System.out.println(total);
}
else {
total = quantity * price;
System.out.println(total);
}
}
public Order(){
isValidOrder = false;
message = "**ERROR** : Order number cannot be totalled as no details have been supplied.";
orderNum++;
}
public Order (String productName, int quantity) {
orderNum++;
this.quantity = quantity;
this.productName = productName;
testQuantity(quantity);
getPrice(productName);
if (isValidOrder == true){
calculate();
}
}
public Order (String productName, int quantity, int discount) {
orderNum++;
this.productName = productName;
this.quantity = quantity;
this.discount = discount;
testQuantity(quantity);
testDiscount(discount);
getPrice(productName);
if (isValidOrder != false){
calculate();
}
}
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 (this.quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
}
else if (this.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 (this.discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
}
else if (this.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: " + orderNum + "\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: " + orderNum + "\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" , 2000, 30);
System.out.println(O1.getOrderDetails());
OrderLaunch frame = new OrderLaunch();
frame.setVisible(true);
}
}
答
代码有几个问题:
1)testQuantity
方法创建本地isValidOrder
布尔并从而掩盖成员isValidOrder
布尔。
例:
public void testQuantity(int quantity){
// ----------------------
// Remove this boolean here...
// boolean isValidOrder = true;
// ----------------------
if (this.quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
}
else if (this.quantity > maxQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
}
else {
this.quantity = quantity;
this.isValidOrder = true;
}
}
2)在testQuantity方法要设置回真实在getPrice
方法中的构件isValidOrder
布尔设置为false后。
例:
private void getPrice(String pce) {
Arrays.sort(products);
int searchProductArray = Arrays.binarySearch(products, pce);
if (searchProductArray >= 0) {
price = prices[searchProductArray];
productName = products [searchProductArray];
// ----------------------
// Comment this boolean here for now until you figure things out
// isValidOrder = true;
// ----------------------
}
else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}
如果你解决这些问题2,那么这些电话会给你所需的输出
Order O1 = new Order("Compass" , 2000, 30);
System.out.println(O1.getOrderDetails());
错误:无效的数量。数量不能大于1000
答
您正在使用变量isValidOrder
来检查是否运行计算。它仍在计算无效数量值的原因是,在数量测试中将其设置为false后,在getPrice
方法中将isValidOrder
设置为true。
答
在你的代码中private void getPrice(String pce)设置isValidOrder = true;这是错误的。 评论该行&它会罚款炒锅:)
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";
}
}
但我需要得到越来越getOrderDetails时),显示(而不是有它作为一个独立的消息 –
我更新我的回答的消息。请看一下。 –