调用和使用Java方法时出现问题

调用和使用Java方法时出现问题,java,methods,Java,Methods,我刚开始学习Java,我正试图根据作业表编写一个程序(在文章的底部给出了这张表)。然而,我真的不太明白如何很好地使用方法。我已经在“Customer.java”类中编写了我的方法,我正在尝试在“TestCustomer.java”类中使用它们。然而,由于我真的不知道如何做到这一点,它已经变成可怕的。我一直在寻找这方面的信息,但我似乎总是让自己更加困惑。你们有没有可能告诉我使用这些方法的正确方法,或者至少给我指出正确的方向?非常感谢你能提供的任何帮助 客户类别 import java.util.S

我刚开始学习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);
    }

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

}
作业表(现在不必担心打印到文件中,只需要让主要技术人员工作)

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

object.method(arguments)
如果没有参数,则如下所示:

object.method()
此外,您需要使用accessor和mutator方法,而不是像这里那样直接设置实例变量:

customer1.listSaleAmount = 65.00;
您应该实现如下方法:

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

public double getListSaleAmount() {
    return listSaleAmount;
}
并将
listsalealmount
设为私有

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

public static String printTaxRate; {
    System.out.println("Tax Rate is" + taxRate + "%.");
}
您应该使用以下代码:

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

问题在于方法标题中奇怪的分号。

请合并您的文章。我们只需要查看与当前问题相关的部分代码,而不是整个程序。我在尝试调用这些方法时遇到了各种各样的错误。西尔维奥,我真的不确定哪些部分是相关的,哪些是不相关的:/谢谢你看这个。你有没有可能通过调用我的方法给我举个例子?这两种方法仍然会产生错误,不能正常工作。customer1.printTaxRate();Customer.printTaxRate();还有一个问题。我将更新答案以包含该内容。@user2774647查看您的
Customer
类。方法
printaxrate
被错误地写为
printaxrate。将其更改为
printTaxRate()
谢谢大家!不知道为什么我会这样写。就像我说的,我对这个很陌生。我现在有了所有的打印方法。我现在正在努力让其他人工作,我会向他们汇报。再次感谢。终于消除了所有错误,但现在它为所有内容打印0或null。它唯一正确打印的是销售金额:(现在只需计算出这一部分…哈哈。不管怎样,再次感谢您对方法的帮助。