-2

我的代码有点问题,我似乎无法弄清楚。每当我运行它时,它都会打印“客户为空”而不是插入他们的名字。它还总是将所有税款计算为 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(" ");
}
}

测试客户类

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);
    }

}

作业表(现在不担心打印到文件,只希望主要机制工作) 在此处输入图像描述 在此处输入图像描述

另外,感谢那些帮助我解决这个项目的最后一个问题的人。你帮了很多忙。

4

4 回答 4

2

在你的构造函数中

public Customer (String customerName, boolean taxable) {

}

您被传入参数,但您从未将它们分配给您的类字段。

尝试

public Customer (String customerName, boolean taxable) {
    this.customerName = customerName;
    this.taxable = taxable;
}
于 2013-10-30T02:01:02.940 回答
1

您必须在客户类中定义您的构造函数来设置 customerName 和 texable 类级别变量的值:

public Customer (String customerName, boolean taxable) {
  this.customerName = customerName;
  this.taxable = taxable;
}

此外,以下 if 条件似乎存在问题:

if (taxable = true){

您应该使用 == 运算符进行比较:

if (taxable == true) {

其实不用比较。只需使用:

if (taxable) {
于 2013-10-30T02:07:34.037 回答
1

Customer 类中的构造函数应将名称和应税值分配给 Customer 数据成员。

public Customer (String customerName, boolean taxable) {
    this.customerName = customerName;
    this.taxable = taxable;
}
于 2013-10-30T02:01:24.750 回答
0

而不是使用if (taxable = true),只需使用if (taxable).

于 2013-10-30T02:13:57.677 回答