2013-10-30 83 views
0

我剛開始學習Java,並試圖根據作業表編寫一個程序(在該帖子的底部給出了此表)。但是,我真的不太明白如何使用方法。我已經在「Customer.java」類中編寫了方法,並且正在嘗試在「TestCustomer.java」類中使用它們。但是,因爲我真的不知道如何做到這一點,所以它變得非常可怕。我在這方面搜索了一些信息,但我似乎一直在讓自己更加困惑。你們有沒有機會向我展示使用這些方法的正確方法,或者至少讓我指出正確的方向?非常感謝您提供的任何幫助。故障調用和使用Java方法

Customer類

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

} 

分配表(不擔心現在打印到文件,只是想主要機械工作) enter image description here enter image description here

+0

什麼不起作用? – tbodt

+0

請整理您的文章。我們只需要查看代碼中與手頭問題相關的部分,而不是整個程序。 –

+0

我收到各種錯誤,試圖調用這些方法。西爾維奧,我不確定哪些部分是相關的,什麼不是:/ – user2774647

回答

2

您似乎對調用方法的語法感到困惑。語法如下:

object.method(arguments) 

如果沒有參數,它看起來像這樣:

object.method() 

另外,你需要像你這樣在這裏使用存取和突變的方法,而不是直接設置實例變量:

customer1.listSaleAmount = 65.00; 

你應該實現這樣的方法:

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

public double getListSaleAmount() { 
    return listSaleAmount; 
} 

and make listSaleAmount private。

問題#2:定義方法的語法。你正使用該代碼來定義的方法:

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

您應該使用此代碼:

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

的問題是該方法首部內的古怪放置分號。

+0

謝謝你看看這個。有沒有可能通過調用我的一個方法給我一個例子?這兩個仍然會出現錯誤,並且無法正常工作。 customer1.printTaxRate(); Customer.printTaxRate(); – user2774647

+0

還有另一個問題。我會更新答案以包含該內容。 – tbodt

+1

@ user2774647查看您的「客戶」類。 'printTaxRate'方法被錯誤地寫爲'printTaxRate;'。將其更改爲'printTaxRate()' – Justin