导入文件中的数据并用于计算工资

时间:2016-03-26 18:51:38

标签: java

处理一个java程序,该程序计算从TXT文件导入的不同类型员工的工资。

如果我发表评论,该程序将成功构建并运行:

System.out.println("\nAverage Salary for 2014: " + (totalSalary / indexYear2014));

System.out.println("\nAverage Salary for 2015: " + (totalSalary / indexYear2015));

但是,似乎根本没有导入/使用数据。

以下是完整代码:

package computeemployeesalary;

/**
 *
 * @author Jason Snook
 * Course name: Intermediate Programming (CMIS 242)
 * Assignment: Project 1 - Compute Employee Salary
 */

/**
 * This program computes the salaries of a collection of employees of
 * different types: Employee, Salesman, Executive
 * 
 * Salary for employee is calculated by multiplying monthly salary by 12
 * 
 * Salary for salesman takes the base of employee, and then adds commission,
 * which has a cap of 20000
 * 
 * Salary for executive takes the base of employee, and then adds a bonus
 * in the event that the current stock is more than 100
*/

// Imports
import java.util.Scanner;
import java.io.*;

/**
 * This class contains the employee name and monthly salary
 */
// Employee class
class Employee {
    // Variable declaration
    String employeeName;
    int monthlySalary = 0;

    // Employee constructor
    public Employee(String employeeName, int monthlySalary) {
        this.employeeName = employeeName;
        this.monthlySalary = monthlySalary;
    } // end Employee constructor method

    // Calculate employee annual salary
    public int annualSalary() {
        return monthlySalary * 12;
    } // end annualSalary method

    // Return employee name and annual salary as String
    @Override // takes advantage from compiler checking
    public String toString() {
        return "Employee Name: " + employeeName +
                " Salary: $" + monthlySalary;
    } // end toString method
} // end employee class

/**
 * This class contains the salesman annual sales and extends employee class
 */
// Salesman subclass, extends Employee class
class Salesman extends Employee {
    // Variable declaration
    int annualSales = 0;

    // Salesman constructor
    public Salesman(String employeeName, int monthlySalary, int annualSales) {
        super(employeeName, monthlySalary);
        this.annualSales = annualSales;
    } // end Salesman constructor method

    @Override // takes advantage from compiler checking
    // Computes commission and annual salary
    public int annualSalary() {
        int commission = annualSales * 3 / 100;

        // if commission is greater than 25000, then set commission to 25000
        if (commission > 25000) {
            commission = 25000;
        } // emd comission if

        return (monthlySalary * 12 + commission);
    } // end annualSalary method

    @Override // takes advantage from compiler checking
    // Displays output details as String
    public String toString(){
        return "Employee Name: " + employeeName +
                " Salary: $" + monthlySalary;
    } // end toString method
} // end Salesman class

/**
 * This class contains the current stock price and extends employee class
 */
// Executive subclass, extends Employee class
class Executive extends Employee {
    // Variable declaration
    int currentStockPrice = 0;

    // Executive constructor
    public Executive(String employeeName, 
            int monthlySalary, int currentStockPrice) {
        super(employeeName, monthlySalary);
        this.currentStockPrice = currentStockPrice;
    } // end Executive constructor method

    @Override // takes advantage from compiler checking
    // Computes commission and annual salary
    public int annualSalary() {
        int executiveBonus = 0;

        // Adds executive bonus if stock is greater than 100
        if(currentStockPrice > 100) {
            executiveBonus = 20000;
        } // end executiveBonus if

        return (monthlySalary * 12 + executiveBonus);
    } // end annualSalary method

    @Override // takes advantage from compiler checking
    // Displays output details as String
    public String toString() {
        return "Employee Name: " + employeeName +
                " Salary: $" + monthlySalary;
    } // end toString method
} // end Executive class

/**
 * This class computes salary based on input file and reports results
 */
public class ComputeEmployeeSalary {
    // Main method
    public static void main(String[] args) throws IOException {
    // Import text file and compute salary increase

        try {
            // Create a File instance
            java.io.File file = new java.io.File("employees.txt");

            // Create Scanner object
            Scanner input = new Scanner(file);

            // Create arrays for 2014 and 2015
            Employee year2014[] = new Employee[20];
            Employee year2015[] = new Employee[20];

            // Create indexes for 2014 and 2015 arrays
            int indexYear2014 = 0, indexYear2015 = 0;

            // Read data from a file
            while (input.hasNext()) {
                String year = input.next();
                String employeeTitle = input.next();
                String employeeName = input.next();
                int monthlySalary = input.nextInt();

                // Action if employee is a regular employee.
                if (employeeTitle.equalsIgnoreCase("Employee")) {
                    Employee regularEmployee = new Employee(employeeName, 
                            monthlySalary);

                    if (year == "2014") {
                        year2014[indexYear2014++] = regularEmployee;
                    } // end 2014 if

                    if (year == "2015") {
                        year2015[indexYear2015++] = regularEmployee;
                    } // end 2015 if
                } // end Employee if

                // Action if employee is a salesman.
                if (employeeTitle.equalsIgnoreCase("Salesman")){
                    int annualSales = input.nextInt();

                    Salesman salesEmployee = new Salesman(employeeName, 
                            monthlySalary, annualSales);

                    if (year == "2014") {
                        year2014[indexYear2014++] = salesEmployee;
                    } // end 2014 if

                    if (year == "2015") {
                        year2015[indexYear2015++] = salesEmployee;
                    } // end 2015 if
                } // end Salesman if

                // Action if employee is an executive.
                if (employeeTitle.equalsIgnoreCase("Executive")) {
                    int currentStockPrice = input.nextInt();

                    Executive executiveEmployee = new Executive(employeeName,
                    monthlySalary, currentStockPrice);

                    if (year == "2014") {
                        year2014[indexYear2014++] = executiveEmployee;
                    } // end 2014 if

                    if (year == "2015") {
                        year2015[indexYear2015++] = executiveEmployee;
                    } // end 2015 if
                } // end Executive if

            }  // end While

            // Generate Report for 2014
            int totalSalary = 0;

            System.out.println("===== Salary Report for 2014 =====");

            for (int i = 0; i < indexYear2014; i++) {
                System.out.print(year2014[i]);
                System.out.println(" Annual Salary: " +
                        year2014[i].annualSalary());
            } // end annualSalary 2014 for

            for (int i = 0; i < indexYear2014; i++) {
                totalSalary+= year2014[i].annualSalary();
            } // end totalSalary 2014 for

            System.out.println("\nAverage Salary for 2014: " +
                    (totalSalary / indexYear2014));

            // Generate Report for 2015
            System.out.println("\n===== Salary Report for 2015 =====");

            for (int i = 0; i < indexYear2015; i++) {
                System.out.print(year2015[i]);
                System.out.println(" Annual Salary: " +
                        year2015[i].annualSalary());
            } // end annualSalary 2015 for

            for (int i = 0; i < indexYear2015; i++) {
                totalSalary+= year2015[i].annualSalary();
            }  // end totalSalary 2015 for

            System.out.println("\nAverage Salary for 2015: " +
                    (totalSalary / indexYear2015));

            // Close input file
            input.close();
        } // end try
        catch (IOException i) {
            System.out.println("Error: File Not Found, error code: " + i);
        } // end catch
    } // end main method  
} // end ComputeEmployeeSalary class

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

当你注释掉这两行时它起作用的原因是它们导致除以零的错误。 indexYear2014indexYear2015都是0。

原因在于您比较字符串的方式:

year == "2015"应为year.equals("2015")