我的代码有点麻烦,我似乎无法弄明白。每当我运行它时,它会打印“Customer is null”而不是插入其名称。它也总是将所有税计算为0(我的if语句有问题吗?)。 你们有没有机会发现这个问题?好像其他一切都正常。 (在Customer类中编写方法,在TestCustomer类中调用它们,并且说明在帖子的末尾)。
感谢任何花时间阅读本文并尝试提供帮助的人。很抱歉这么多信息,我不知道造成这种情况的原因,所以我想我会包括所有内容。
客户类
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Customer {
public static double taxRate = 0.00;
public static double saleRate = 0.00;
String customerName;
double listSaleAmount;
double saleDiscount = 0;
double netSaleAmount;
double taxAmount;
double saleTotal;
boolean taxable;
public double total;
public Customer (String customerName, boolean taxable) {
}
public void calculateTax () {
saleDiscount = listSaleAmount*(saleRate/100);
netSaleAmount = listSaleAmount-saleDiscount;
if (taxable = true){
taxAmount = netSaleAmount*(taxRate/100);
}
else{
taxAmount = 0;
}
saleTotal = netSaleAmount + taxAmount;
total += saleTotal;
}
public void printRecord () {
System.out.println("Customer is " + customerName);
System.out.println("Sale amount is $" + listSaleAmount);
System.out.println("Discount amount is $" + saleDiscount);
System.out.println("Net Sale Amount is $" + netSaleAmount);
System.out.println("Tax amount is $" + taxAmount);
System.out.println("Total Sale Amount is $" + saleTotal);
System.out.println(" ");
}
public static void changeTaxAmount () {
Scanner input = new Scanner(System.in);
double userTaxAmount = Double.parseDouble(JOptionPane.showInputDialog("What is the Tax Rate? (8.25 & 8.50 for testing)"));
taxRate = userTaxAmount;
}
public static void changeSaleRate () {
Scanner input = new Scanner(System.in);
double userSaleAmount = Double.parseDouble(JOptionPane.showInputDialog("What is the Sale Discount Rate? (0.00 & 7.50 for testing)"));
saleRate= userSaleAmount;
}
public static void printTaxRate() {
System.out.println("Tax Rate is " + taxRate + "%.");
}
public static void printSaleRate() {
System.out.println("The Sale Rate is " + saleRate + ".");
System.out.println(" ");
}
}
TestCustomer类
public class TestCustomer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer customer1 = new Customer("Annie Smith", true);
Customer customer2 = new Customer("Bob Wilson", false);
Customer.changeTaxAmount();
Customer.printTaxRate();
Customer.changeSaleRate();
Customer.printSaleRate();
customer1.listSaleAmount = 65.00;
customer2.listSaleAmount = 52.00;
customer1.calculateTax();
customer1.printRecord();
customer2.calculateTax();
customer2.printRecord();
Customer.changeTaxAmount();
Customer.printTaxRate();
Customer.changeSaleRate();
Customer.printSaleRate();
customer1.listSaleAmount = 84.00;
customer2.listSaleAmount = 105.00;
customer1.calculateTax();
customer1.printRecord();
customer2.calculateTax();
customer2.printRecord();
double total2 = customer1.total + customer2.total;
System.out.println("The total of all sales is $" + total2);
}
}
作业单(不用担心现在打印到文件,只是想让主要的机制工作)
另外,感谢那些帮助我完成关于这个项目的最后一个问题的人。你帮了很多忙。
答案 0 :(得分:2)
在你的构造函数
中public Customer (String customerName, boolean taxable) {
}
您传入了参数,但从未将它们分配给您的类字段。
尝试
public Customer (String customerName, boolean taxable) {
this.customerName = customerName;
this.taxable = taxable;
}
答案 1 :(得分:1)
Customer类中的构造函数应将名称和应税值分配给Customer数据成员。
public Customer (String customerName, boolean taxable) {
this.customerName = customerName;
this.taxable = taxable;
}
答案 2 :(得分:1)
您必须在customer类中定义构造函数以设置customerName和texable类级变量的值:
public Customer (String customerName, boolean taxable) {
this.customerName = customerName;
this.taxable = taxable;
}
此外,如果条件如下,似乎存在以下问题:
if (taxable = true){
您应该使用==运算符进行比较:
if (taxable == true) {
实际上,您无需比较它。只需使用:
if (taxable) {
答案 3 :(得分:0)
不要使用if (taxable = true)
,只需使用if (taxable)
即可。