简体   繁体   English

麻烦调用和使用Java方法

[英]Trouble Calling and Using Java Methods

I just started learning Java and I'm trying to write a program based on an assignment sheet (gave this sheet at bottom of the post). 我刚开始学习Java,我正在尝试编写一个基于作业表的程序(在帖子的底部给出了这张表)。 However, I really don't quite understand how to use methods all that well. 但是,我真的不太明白如何使用方法。 I've written my methods in the "Customer.java" class, and I'm trying to use them in my "TestCustomer.java" class. 我已经在“Customer.java”类中编写了我的方法,并且我正在尝试在我的“TestCustomer.java”类中使用它们。 However, since I really don't know how to do this, it has turned out horribly. 但是,由于我真的不知道该怎么做,所以结果可怕。 I've searched for information on this, but I just seem to keep making myself more confused. 我已经搜索过这方面的信息,但我似乎总是让自己更加困惑。 Is there any chance you guys could show me the correct way to use these methods, or at least point me in the right direction? 你们有没有机会告诉我使用这些方法的正确方法,或者至少指出我正确的方向? Thank you a ton for any help you can provide. 非常感谢您提供的任何帮助。

Customer class 客户类

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 + ".");
}
}

TestCustomer class TestCustomer类

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

}

Assignment sheet (Not worrying about printing to a file right now, just want the main mechanics to work) 分配表(不用担心现在打印到文件,只是希望主要的机制工作) 在此输入图像描述在此输入图像描述

You seem to be confused about the syntax for calling a method. 您似乎对调用方法的语法感到困惑。 The syntax is as follows: 语法如下:

object.method(arguments)

If there are no arguments it looks like this: 如果没有参数,它看起来像这样:

object.method()

Also, you need to use accessor and mutator methods instead of directly setting instance variables like you do here: 此外,您需要使用accessor和mutator方法,而不是像在此处一样直接设置实例变量:

customer1.listSaleAmount = 65.00;

You should implement methods like these: 你应该实现这样的方法:

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

public double getListSaleAmount() {
    return listSaleAmount;
}

and make listSaleAmount private. 并使listSaleAmount私有的。

Problem #2: The syntax for defining the methods. 问题#2:定义方法的语法。 You are using this code to define a method: 您正在使用此代码来定义方法:

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

You should be using this code: 您应该使用此代码:

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

The problem is the weirdly placed semicolon inside the method header. 问题是方法标题内部分散了奇怪的分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM