Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导入文件中的数据并用于计算薪资_Java - Fatal编程技术网

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

Java 导入文件中的数据并用于计算薪资,java,Java,使用java程序计算不同类型员工的工资,这些员工是从TXT文件导入的 如果我注释掉以下内容,程序将成功构建并运行: System.out.println("\nAverage Salary for 2014: " + (totalSalary / indexYear2014)); System.out.println("\nAverage Salary for 2015: " + (totalSalary / indexYear2015)); 然而,似乎根本没有导入/使用数据 以下是完整的代

使用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
包computeemployeesalary;
/**
*
*@作者杰森·斯努克
*课程名称:中级编程(CMIS 242)
*任务:项目1-计算员工工资
*/
/**
*此程序计算一组员工的工资
*不同类型:员工、推销员、高管
* 
*员工的工资按月薪乘以12计算
* 
*销售员的工资以员工为基数,然后加上佣金,
*它的上限是20000
* 
*高管薪酬以员工为基数,然后再加上奖金
*如果当前库存超过100
*/
//进口
导入java.util.Scanner;
导入java.io.*;
/**
*此类包含员工姓名和月薪
*/
//雇员阶级
班级员工{
//变量声明
字符串employeeName;
int monthlySalary=0;
//雇员建造师
公共雇员(字符串employeeName,每月整数){
this.employeeName=employeeName;
this.monthlySalary=monthlySalary;
}//结束雇员构造函数方法
//计算员工年薪
公共国际年度日历(){
每月返回*12;
}//结束年度日历方法
//以字符串形式返回员工姓名和年薪
@重写//利用编译器检查的优势
公共字符串toString(){
return“员工姓名:”+employeeName+
“工资:$”+月薪;
}//结束toString方法
}//结束雇员类
/**
*此类包含“销售人员年度销售额”和“扩展员工”类
*/
//Saleser子类,扩展Employee类
类销售员扩展雇员{
//变量声明
int annualSales=0;
//销售员建造师
公共销售员(字符串员工姓名,每月整数,每年整数){
超级(员工姓名,每月一次);
this.annualSales=annualSales;
}//结束推销员构造函数方法
@重写//利用编译器检查的优势
//计算佣金和年薪
公共国际年度日历(){
国际佣金=年销售额*3/100;
//如果佣金大于25000,则将佣金设置为25000
如果(佣金>25000){
佣金=25000;
}//emd委员会,如果
回报(每月*12+佣金);
}//结束年度日历方法
@重写//利用编译器检查的优势
//将输出详细信息显示为字符串
公共字符串toString(){
return“员工姓名:”+employeeName+
“工资:$”+月薪;
}//结束toString方法
}//结束销售员课程
/**
*此类包含当前股票价格并扩展employee类
*/
//Executive子类,扩展Employee类
班级主管扩展员工{
//变量声明
int currentStockPrice=0;
//执行构造
公共行政人员(字符串employeeName,
每月整数,当前股价整数){
超级(员工姓名,每月一次);
this.currentStockPrice=currentStockPrice;
}//结束执行构造函数方法
@重写//利用编译器检查的优势
//计算佣金和年薪
公共国际年度日历(){
int executiveBonus=0;
//如果股票超过100,则增加高管奖金
如果(当前股票价格>100){
执行奖金=20000;
}//结束执行奖金,如果
回报(每月*12+高管奖金);
}//结束年度日历方法
@重写//利用编译器检查的优势
//将输出详细信息显示为字符串
公共字符串toString(){
return“员工姓名:”+employeeName+
“工资:$”+月薪;
}//结束toString方法
}//结束高级管理课程
/**
*此类根据输入文件计算薪资并报告结果
*/
公共类ComputeEmployeeSalary{
//主要方法
公共静态void main(字符串[]args)引发IOException{
//导入文本文件并计算加薪
试一试{
//创建一个文件实例
java.io.File File=new java.io.File(“employees.txt”);
//创建扫描仪对象
扫描仪输入=新扫描仪(文件);
//为2014年和2015年创建阵列
员工2014年[]=新员工[20];
员工2015年[]=新员工[20];
//为2014和2015阵列创建索引
int indexYear2014=0,indexYear2015=0;
//从文件中读取数据
while(input.hasNext()){
字符串year=input.next();
字符串employeeTitle=input.next();
字符串employeeName=input.next();
int monthlySalary=input.nextInt();
//如果员工是正式员工,则执行操作。
if(雇员职务.同等信号情况(“雇员”)){
员工正式员工=新员工(员工名称,
每月);
如果(年份=“2014年”){
2014年[indexYear2014++]=正式员工;
}//2014年底,如果
如果(年份=“2015”){
2015年[indexYear2015++]=正式员工;
}//2015年底,如果
}//如果
//如果员工是销售人员,则采取行动。
if(雇员职务、同等信号(“销售人员”)){
int annualSales=input.nextInt();
推销员推销员雇员