所以我正在尝试用java编写基本程序。它接受输入,对输入进行计算并显示结果。它有3个构造函数,第一个不接受输入并创建错误消息,第二个和第三个接受输入并对它们执行计算。
所以程序应该接受(来自创建者类):
订单o1 =新订单(“车牌号”,12.7,6)
该程序接受上述代码,但不执行任何计算,只显示“新订单”。
不确定我在这里做错了什么,我添加了评论来尝试解释我在做什么。
public class Order {
private static int orderNum = 0;
public Order(){
orderNum++; //setting the static variable
}
private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;
//setting the instance variables and booleans
public boolean isDiscounted(){ //setting the valid
discount boolean value
if (discount > 100 || discount ==0) {
return false;
}
else {
return true;
}
}
public boolean isValidOrder(){ //setting the valid order boolean value
if (total ==0){
return false;
}
else {
return true;
}
}
public Order(String m) {
orderNum++;
this.message = m;
isValidOrder = false;
isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set
public Order(String pN, double p, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.quantity = q;
isValidOrder = true;
isDiscounted = false;
}
//second constructor accepting 3 parameters, with boolean values set
public Order(String pN, double p, double d, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.discount = d;
this.quantity = q;
isValidOrder = true;
isDiscounted = true;
}
//third constructor accepting 4 parameters, with boolean values set
public void setTotal(double t){
total = t;
isDiscounted = true;
}
//setting the total
public double getTotal(){
if (isValidOrder == true){
return ((price * quantity)-(price * quantity * (discount/100)));
}
else if (isDiscounted == false){
return (price * quantity);
}
return total;
}
//getting the total and calculations
@Override
public String toString(){
if (isDiscounted == true){
message = ("Order number: " + Order.orderNum + "\nProduct
Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:
" + quantity
+ "\nDiscount: " + discount + "\nTotal Price: " + total);
}
//first part of toString method, with discount
else if (isDiscounted == false){
message = ("Order number: " + Order.orderNum + "\nProduct
Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:
" + quantity
+ "\nTotal Price: " + total);
}
//second part of toString method, with no discount
else if (isValidOrder == false){
message = "Error";
}
//third part of toString method with error message
return message;
}
}
答案 0 :(得分:1)
我修改了你的代码检查一次。
public class Order {
private static int orderNum = 0;
public Order(){
orderNum++; //setting the static variable
}
private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;
//setting the instance variables and booleans
public boolean isDiscounted() { //setting the discount boolean
if (discount > 100 || discount == 0) {
return false;
} else {
return true;
}
}
public boolean isValidOrder() { //setting the valid order boolean value
if (total == 0) {
return false;
} else {
return true;
}
}
public Order(String m) {
orderNum++;
this.message = m;
isValidOrder = false;
isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set
public Order(String pN, double p, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.quantity = q;
isValidOrder = true;
isDiscounted = false;
}
//second constructor accepting 3 parameters, with boolean values set
public Order(String pN, double p, double d, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.discount = d;
this.quantity = q;
isValidOrder = true;
isDiscounted = true;
}
//third constructor accepting 4 parameters, with boolean values set
public void setTotal(double t) {
total = t;
isDiscounted = true;
}
//setting the total
public double getTotal() {
if (isValidOrder == true) {
return ((price * quantity) - (price * quantity * (discount / 100)));
} else if (isDiscounted == false) {
return (price * quantity);
}
return total;
}
//getting the total and calculations
@Override
public String toString() {
if (isDiscounted == true) {
message = ("Order number: " + Order.orderNum + " \n Product Name: "
+ productName + "\nProduct Price: " + price + "\nOrder Quantity: " + quantity +
"\nDiscount: " + discount + "\nTotal Price: " + total);
}
//first part of toString method, with discount
else if (isDiscounted == false) {
message = ("Order number: " + Order.orderNum + "\nProduct Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:" + quantity
+ "\nTotal Price: " + total);
}
//second part of toString method, with no discount
else if (isValidOrder == false) {
message = "Error";
}
//third part of toString method with error message
return message;
}
}