2013-02-22 68 views
1

我剛剛開始在Java中,我有一個奇怪的問題,我只是似乎無法得到的根。我有兩個程序,一個是從文本文件中獲取數據,然後將其調用到類中進行一些計算,最後將輸出結果放入另一個文本文檔中。Java:麻煩呼叫另一個類

一切正常,除了這部分在這裏:

public class Paycheck 
{ 
//Constants for the private class 
private final String EMPLOYEE_NAME;   //Employee name   
private final String SOC_SEC_NUM;    //Employee Social Security Number 
private final double WAGE_RATE;    //Employee wage 
private final double TAX_RATE;    //Employee tax withheld 
private final double HOURS_WORKED;   //Employee's hours of work 

//Variables for the private class 
private double grossPay;      //Employee Gross Pay 
private double taxWithheld;     //Employee Tax Withheld 
private double netPay;      //Employee Net Pay 





//This is the constructor. It is called whenever an instance of the class is created 
public Paycheck (String name, String ssn, double wage, double tax, double hours) 
{ 
    EMPLOYEE_NAME = name; //Instance employee name 
    SOC_SEC_NUM = ssn; //Instance employee SSN 
    WAGE_RATE = wage;  //Instance employee wage rate 
    TAX_RATE = tax;  //Instance employee tax rate 
    HOURS_WORKED = hours; //Instance employee hours worked 
} 

//This calculates the variables in the paycheck class 
public void calcWages() 
{ 
    grossPay = WAGE_RATE * HOURS_WORKED; //Calculates Gross Pay 
    taxWithheld = grossPay * TAX_RATE;  //Calculates Taxes Withheld 
    netPay = grossPay - taxWithheld;  //Calculates net pay 

} 




//Returns the Paycheck objects Employee Name 
public String getEmployeeName() 
{ 
    return EMPLOYEE_NAME; 
} 

//Returns the employee SSN of the Paycheck object 
public String getSocSecNum() 
{ 
    return SOC_SEC_NUM; 
} 

//Reeturns a Paycheck object's employee Wage Rate 
public double getWageRate() 
{ 
    return WAGE_RATE; 
} 

//Returns a Paycheck object's employee tax rate 
public double getTaxRate() 
{ 
    return TAX_RATE; 
} 

//Returns an Paycheck object's employee hours worked 
public double getHoursWorked() 
{ 
    return HOURS_WORKED; 
} 

//Returns a Paycheck object's gross pay 
public double getGrossPay() 
{ 
    return grossPay; 
} 

//Returns a Paycheck object's Taxes Withheld 
public double getTaxWithheld() 
{ 
    return taxWithheld; 
} 

//Returns a paycheck object't net pay 
public double getNetPay() 
{ 
    return netPay; 
} 

的calcWages()做必要的計算和低於這個是一系列讓語句來調用它們。但是,我的輸出不會返回calcWages()參數的任何值。

我在這裏添加了getters,我的其他程序抓住了他們。然而,我的其他程序的最終輸出爲0.

我在哪裏錯了?

這是主要的方法是稱他們

public static void main(String [] args) throws IOException //Throws clause 
{ 
    //Declare constants 
    final String INPUT_FILE = "Employee.txt";   //Input text file containing Employee information 
    final String OUTPUT_FILE= "PayrollHistory.txt"; //Output text file that will receive the data 

    //Declare Variables 
    String payPeriodDate;  //Ending date of the pay period 
    String employeeName;  //Employee Name in text file 
    String employeeSocSecNum; //Employee SSN in text file 
    double employeeHours;  //Employee hours worked 
    double employeeTax;   //Employee Tax rate 
    double employeeWage;  //Employee Wage rate 
    double totalGrossPay;  //Total employee Gross for pay period 
    double totalTaxWithheld; //Total Tax Withheld for pay period 
    double totalNetPay;   //Total Net Payroll for pay period 
    String input;    //String input for double conversion in JoptionPane 

    DecimalFormat money = new DecimalFormat ("#0.00"); // Decimal Format to put money in the right format(USD) 

    //This ensures that the input file actually exists in the program folder 
    //And exits the program if it does not, along with the prompt. 
    File file = new File(INPUT_FILE); 
    if (!file.exists()) 
    { 
    JOptionPane.showMessageDialog(null, "The " + INPUT_FILE + " file cannot be found." + 
         "Program terminated."); 
    System.exit(0); 
    } 


    // Create Scanner object to enable reading data from input file 
    Scanner inputFile = new Scanner(file); 

    // Create FileWriter and PrintWriter objects to enable 
    // writing (appending not overwriting) data to text file 
    FileWriter fwriter = new FileWriter(OUTPUT_FILE, true); 
    PrintWriter outputFile = new PrintWriter(fwriter); 

    //Initialize accumulator values 
    totalGrossPay = 0.0; 
    totalTaxWithheld = 0.0; 
    totalNetPay = 0.0; 

    //Get the pay period for the employee 
    payPeriodDate = JOptionPane.showInputDialog("Enter pay period ending date (mm/dd/yyyy):"); 
    outputFile.println("PAY PERIOD ENDING DATE: " + payPeriodDate);   //Inputs pay period date into the text file 
    outputFile.println(); // Blank line 
    outputFile.println(); // Blank line 


    while (inputFile.hasNext()) // This will look through the input file and get the necessary variable input 
    { 
    // Read employee Name from Input File 
    employeeName = inputFile.nextLine(); 
    // Read employee SSN from input file 
    employeeSocSecNum = inputFile.nextLine(); 
    // Read employee Wage Rate from input file 
     //Parses it into a double type 
    input = inputFile.nextLine(); 
    employeeWage = Double.parseDouble(input); 
     //Read employee tax rate from input file 
     //Parses it into a double type 
     input = inputFile.nextLine(); 
     employeeTax = Double.parseDouble(input); 


     //Get number of hours worked 
     input = JOptionPane.showInputDialog("Employee Name: " + employeeName + 
                 "\nEnter number of hours worked:"); 
     employeeHours = Double.parseDouble(input); 

     //This call the paycheck class to create a new Paycheck Object 
     Paycheck employee = new Paycheck (employeeName, employeeSocSecNum, employeeWage, employeeTax, employeeHours); 

     // Call Paycheck class methods into the output file 
    outputFile.println("Employee Name: " + employeeName);        //Employee Name 
    outputFile.println("SSN: " + employeeSocSecNum);         //Employee SSN 
    outputFile.println("Hours Worked: " + employeeHours);        //Employee Hours Worked 
    outputFile.println("Wage Rate: " + money.format(employeeWage));     //Employee Wage Rate 
     outputFile.println("Gross Pay: " + money.format(employee.getGrossPay()));   //Employee Gross Pay 
     outputFile.println("Tax Rate: " + money.format(employeeTax));      //Employee Tax Rate 
     outputFile.println("Tax Withheld: " + money.format(employee.getTaxWithheld())); //Employee Tax Withheld 
     outputFile.println("Net Pay: " + employee.getNetPay());       //Employee Net Pay 
    outputFile.println(); // Blank line 
+1

郵政編碼您正在調用calcWages和您打印輸出的位置。您可能引用了錯誤的對象 – 75inchpianist 2013-02-22 18:25:10

+3

嗨,可以顯示實際調用此類的代碼,那不正常?你指的是什麼輸出? – dave823 2013-02-22 18:28:02

+1

你究竟在哪裏*打電話給你的吸氣者?你沒有添加。我們正在尋找一種主要方法。 – Colleen 2013-02-22 18:31:16

回答

3

你不顯得呼喚你的干將其實之前調用calcWages(),所以grossPay, taxWithheld, and netPay仍然會是0,因爲這是未初始化的數字Java的默認值。

您需要先調用employee.calcWages(),然後再引用這些值才能更改。

+1

爲了澄清一點OP,您需要實際_call_'calcWages()',例如,在您創建新的Paycheck之後,但在打印語句之前,您需要'employee.calcWages();' - 也不要忘記接受@Colleen的回答 – knoight 2013-02-22 19:06:35

+0

謝謝,我也會編輯澄清! – Colleen 2013-02-22 19:08:07

+1

不想「竊取」你的答案......就好像他錯過了一點點:) – knoight 2013-02-22 19:08:53

0

部分是因爲calcWages()聲明無效public void calcWages()),這意味着它不應該返回任何值,但只是完成一系列(本例中爲計算工資單詳細資料)。調用它之後,繼續引用它處理的實例變量。

+0

是的,我知道,但下面的get語句應該能夠實際得到我的價值是嗎? – 2013-02-22 18:40:22

+2

你在哪裏打電話calcWages?也許你沒有得到的是該功能不會做任何事情,除非你把它稱爲 – 2013-02-22 18:45:56

+0

哪些地方得到聲明? – amphibient 2013-02-22 19:11:41

0

您已聲明變量爲final,這意味着它們只能分配一次。

+2

不。他沒有實際初始化它們。 – Colleen 2013-02-22 18:53:29

+0

我不明白。我需要初始化哪裏? – 2013-02-22 19:03:43

+0

@Colleen,看起來像你的答案是正確的。他最初發布的代碼有些不完整。 – 2013-02-22 19:15:53