繁体   English   中英

麻烦调用和使用Java方法

[英]Trouble Calling and Using Java Methods

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

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

}

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

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

object.method(arguments)

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

object.method()

此外,您需要使用accessor和mutator方法,而不是像在此处一样直接设置实例变量:

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

问题是方法标题内部分散了奇怪的分号。

暂无
暂无

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

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