0

我刚开始学习 Java,我正在尝试根据作业表编写程序(在帖子底部给出了此表)。但是,我真的不太明白如何很好地使用方法。我已经在“Customer.java”类中编写了我的方法,并且正在尝试在我的“TestCustomer.java”类中使用它们。但是,由于我真的不知道该怎么做,结果很糟糕。我已经搜索过这方面的信息,但我似乎一直让自己更加困惑。你们有没有机会向我展示使用这些方法的正确方法,或者至少为我指明正确的方向?非常感谢您提供的任何帮助。

客户类

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 Customer (String CustomerName, boolean taxable) {

}

public double calculateTax (double listSaleAmount) {
    saleDiscount = listSaleAmount*saleRate;
    netSaleAmount = listSaleAmount-saleDiscount;
    if (taxable == true) {
    taxAmount = netSaleAmount*taxRate;
    }
    else {
    taxAmount = 0.00;
    }
    saleTotal = listSaleAmount + taxAmount;


    return saleTotal;

}

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

public static double changeTaxAmount (double taxRate) {
    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;
    return taxRate;

}

public static double changeSaleRate (double saleRate) {
    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;
    return saleRate;
}

public static String printTaxRate; {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

public static String printSaleRate; {
    System.out.println("The Sale Rate is" + saleRate + ".");
}
}

测试客户类

import java.math.BigDecimal;
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);
Double totalOfAllSales = 0.00;


//I have no clue how to actually use the methods I created in the Customer class!
//These are my best guesses, which are obviously wrong
//Any help here would be greatly appreciated!
Customer.changeTaxAmount(taxRate);

Customer.printTaxRate;

Customer.changeSaleRate(saleRate);

Customer.printSaleRate;

customer1.listSaleAmount = 65.00;
customer2.listSaleAmount = 52.00;

totalOfAllSales += customer1.calculateTax;
totalOfAllSales += customer2.calculateTax;

customer1.printRecord;
customer2.printRecord;

Customer.changeTaxAmount(taxRate);
Customer.printTaxRate;
Customer.changeSaleRate(saleRate);
Customer.printSaleRate;

customer1.listSaleAmount = 84.00;
customer2.listSaleAmount = 105.00;
totalOfAllSales += customer1.calculateTax;
totalOfAllSales += customer2.calculateTax;
customer1.printRecord;
customer2.printRecord;

System.out.println("The total of all sales is $" + totalOfAllSales);
    }

}

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

4

1 回答 1

2

您似乎对调用方法的语法感到困惑。语法如下:

object.method(arguments)

如果没有参数,它看起来像这样:

object.method()

此外,您需要使用访问器和修改器方法,而不是像在此处那样直接设置实例变量:

customer1.listSaleAmount = 65.00;

您应该实现如下方法:

public void setListSaleAmount(double lsa) {
    listSaleAmout = lsa;
}

public double getListSaleAmount() {
    return listSaleAmount;
}

并设为listSaleAmount私有。

问题 #2:定义方法的语法。您正在使用此代码来定义一个方法:

public static String printTaxRate; {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

您应该使用以下代码:

public static String printTaxRate() {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

问题是方法头中奇怪的分号。

于 2013-10-30T00:24:26.177 回答