首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java薪资计划,总计薪资时数和总薪资问题

Java薪资计划,总计薪资时数和总薪资问题
EN

Stack Overflow用户
提问于 2015-09-15 23:20:18
回答 1查看 1.9K关注 0票数 0

对于这个程序,我试图为输入的员工设置一个薪资。该程序让用户输入员工的姓名、税务ID和工资,并自动为输入的每个员工生成一个ID号。输入这些信息后,用户将被要求提供工作2周的小时数。为此,输入员工ID号以引用工作一定时间的员工。接下来,输入第一周或第二周以确定他们工作的哪一周。最后,输入那个星期的工作时数。信息完成后,用户输入0以停止输入数据,并将薪资打印在图表中。我遇到的主要问题是,您必须输入每个员工的ID、周和小时,不管他们是否在2周期间工作。如果我不为某个员工输入任何信息,我想试着找出如何将工作时数和总薪资号设置为0。例如,如果我输入: 1,对于员工1;1,对于第1周;20,对于在第1周工作的小时;按enter;1,对于员工1;2,对于第2周;以及30,对于在第2周的工作时间,这些信息将打印在表中。假设我有另一名员工,员工2。如果员工2在第一周或第二周根本没有工作,我不想输入所有这些信息,因此,如果我决定不输入ID、周号和工作时数,则该表应打印0表示工作时数,并打印0表示“总薪资”。然而,当我尝试这样做时,它给了我一个outOfBoundsException: Index: 1 Size: 1错误。我在想有没有人能解决这个问题。下面是我的代码的三个类。在主类中给出了一个用outOfBoundsException执行的程序的例子。

代码语言:javascript
复制
import java.util.Scanner;
import java.util.ArrayList;

/**
 * A class that contains methods for prompting a user for ACME employee information including names, tax IDs, and wages.
 */
public class EmployeeRecord
{
    // ArrayLists used in methods.
    ArrayList<String> employees = new ArrayList<String>();
    ArrayList<String> tIds = new ArrayList<String>();
    ArrayList<Double> wages = new ArrayList<Double>();
    ArrayList<String> employeesLast = new ArrayList<String>();

    Scanner in = new Scanner(System.in);

    // Private Instance variables used in methods.
    private String employeeId = "%03d";
    private String employeeName = " ";
    private String employeeNameLast = " ";
    private String taxId = " ";
    private double wage = 0.0;

    /**
     * A method that prompts the user to enter ACME employee names, tax IDs, and wages. This method also generates an ID number for each employee.
     */
    public void setEmployeeInfo()
    {
        System.out.println("Please enter the names of each ACME employee, each employee tax ID, and each employee wage rate. Press Q when you are done entering names.");

        while(!employeeName.equalsIgnoreCase("Q"))
        {
                employeeName = in.next();
                if(employeeName.equalsIgnoreCase("Q"))
                {
                break;
                }
                employeeNameLast = in.next();
                taxId = in.next();
                wage = in.nextDouble();

                employees.add(employeeName);
                employeesLast.add(employeeNameLast);
                tIds.add(taxId);
                wages.add(wage);

                System.out.println("Employee ID  |  Employee Name        |  Tax ID          |  Wage");
                for(int i = 1; i <= employees.size(); i++)
                {
                    System.out.printf(String.format(employeeId, i) + "          | " + employees.get(i - 1) + " " + employeesLast.get(i - 1) + "              | " + tIds.get(i - 1) + "          | " + "%1.2f",wages.get(i - 1));
                    System.out.println();
                }
        }
    }

    /**
     * A method that gets the list of ACME employee first names added to the record.
     * @return
     * Returns the ArrayList containing the first names of each employee entered.
     */
    public ArrayList<String> getEmployeeArrayList()
    {
        return employees;
    }

    /**
     * A method that gets the list of ACME employee last names added to the record.
     * @return
     * Returns the ArrayList containing the last names of each employee entered.
     */
    public ArrayList<String> getEmployeeLastArrayList()
    {
        return employeesLast;
    }

    /**
     * A method that gets the list of ACME employee tax IDs added to the record.
     * @return
     * Returns the ArrayList containing the tax IDs of each tax ID entered.
     */
    public ArrayList<String> getTaxIdsArrayList()
    {
        return tIds;
    }

    /**
     * A method that gets the list of ACME employee wages added to the record.
     * @return
     * Returns the ArrayList containing the wages of each wage entered.
     */
    public ArrayList<Double> getWageArrayList()
    {
        return wages;
    }

}
代码语言:javascript
复制
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * A class that contains methods for entering payroll information for each ACME employee. This class uses methods from the EmployeeRecord class in order to run and therefore, relies on the EmployeeRecord class to operate.
 */
public class Employee
{
    // ArrayLists that will implement return methods from the EmployeeRecord class.
    ArrayList<String> eMp;
    ArrayList<String> eMpL;
    ArrayList<Double> eW;

    // ArrayLists used to store values in setEmployeePayroll() method.
    ArrayList<Integer> eId = new ArrayList<Integer>();
    ArrayList<Double> hours = new ArrayList<Double>();
    ArrayList<Double> tPay = new ArrayList<Double>();

    // Instance Variables used in setEmployeePayroll() method.
    private String employeeId = "%03d";
    private int weekNumber = 0;
    private double hoursWorked = 0.0;
    private double hoursWorked2 = 0.0;
    private int terminate = 1000;
    private int i = 0;


    Scanner in = new Scanner(System.in);

    /**
     * A method that implements the EmployeeRecord class to prompt the user to enter information for the payroll of each ACME employee. The payroll for each employee is then displayed.
     */
    public void setEmployeePayroll()
    {
        // Constructs a new EmployeeRecord to implement classes from EmployeeRecord class.
        EmployeeRecord e = new EmployeeRecord();
        e.setEmployeeInfo();

        eMp = e.getEmployeeArrayList();
        eMpL = e.getEmployeeLastArrayList();
        eW = e.getWageArrayList();

        // Local variables used in this method.
        double totalPay = 0.0;
        double totalHours = 0.0;
        double overTime = 0.0;
        double overTime2 = 0.0;

        System.out.println("Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.");

        while(terminate != 0)
        {
            terminate = in.nextInt();
            if(terminate == 0)
            {
                break;
            }

            weekNumber = in.nextInt();

            if(weekNumber == 1)
            {
                hoursWorked = in.nextDouble();
            }
            else if(weekNumber == 2)
            {
                hoursWorked2 = in.nextDouble();
            }

            // Checks to see if an employee receives a 150% bonus on their payroll.
            if(hoursWorked > 0 && hoursWorked <= 40 && hoursWorked2 > 0 && hoursWorked2 <= 40)
            {
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay = totalHours * (eW.get(i - 1));
                tPay.add(totalPay);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked2 > 40 && hoursWorked > 0 && hoursWorked <= 40)
            {
                overTime2 = hoursWorked2 - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay = totalHours * (eW.get(i - 1)) + (overTime2 * 1.5);
                tPay.add(totalPay);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked > 40 && hoursWorked2 <= 40 && hoursWorked2 > 0)
            {
                overTime = hoursWorked - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay = totalHours * (eW.get(i - 1)) + (overTime * 1.5);
                tPay.add(totalPay);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked > 40 && hoursWorked2 > 40)
            {
                overTime = hoursWorked - 40;
                overTime2 = hoursWorked2 - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay = totalHours * (eW.get(i - 1)) + (1.5 * (overTime + overTime2));
                tPay.add(totalPay);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }

            i = terminate;
        }

        // Constructs a new date format for the date of the payroll.
        DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d = new Date();

        // Gets current date and time for payroll.
        System.out.println("ACME Payroll run on " + format.format(d));
        System.out.println();
        System.out.println("Employee Number  |  Employee Name    |  Hours Worked  |  Total Pay");
        for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)
        {
            System.out.println(String.format(employeeId, i) + "              | " + eMp.get(i - 1) + " " + eMpL.get(i - 1) + "        | " + hours.get(i - 1) + "           | " + tPay.get(i - 1));
        }
    }
}
代码语言:javascript
复制
/**
 * The main class that runs the contents of the EmployeeRecord and Employee classes.
 *
 *
 */
public class runPayroll 
{
    public static void main(String[] args)
    {
        Employee eE = new Employee();
        eE.setEmployeePayroll();
    }
}
/**
 * Please enter the names of each ACME employee, each employee tax ID, and each employee wage rate. Press Q when you are done entering names.
Jane Smith 1010101 10
Employee ID  |  Employee Name        |  Tax ID          |  Wage
001          | Jane Smith              | 1010101          | 10.00
John Smith 1111111 10
Employee ID  |  Employee Name        |  Tax ID          |  Wage
001          | Jane Smith              | 1010101          | 10.00
002          | John Smith              | 1111111          | 10.00
q
Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.
1 1 20
1 2 30
0
ACME Payroll run on 2015/09/15 22:25:19

Employee Number  |  Employee Name    |  Hours Worked  |  Total Pay
Exception in thread "main" 001              | Jane Smith        | 50.0           | 500.0
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at Employee.setEmployeePayroll(Employee.java:128)
    at runPayroll.main(runPayroll.java:11)

 */
EN

回答 1

Stack Overflow用户

发布于 2015-09-16 02:45:09

此问题存在于Employee类的底部。

代码语言:javascript
复制
for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)
        {
            System.out.println(String.format(employeeId, i) + "              | " + eMp.get(i - 1) + " " + eMpL.get(i - 1) + "        | " + hours.get(i - 1) + "           | " + tPay.get(i - 1));
        }

ArrayList是一个零索引列表.

简单地说,假设你要列出一个列表的所有元素,就像大多数人会认为的那样:

1 2 3 4 5 6 7 8 9

这是一个单索引列表。一个零索引列表(计算机编程中使用的类型)如下所示:

0 1 2 3 4 5 6 7 8

如果要访问第一个元素,则必须在0位置获取该元素。如果你想得到第五个元素,你必须在第4位置得到这个元素。它有点向后,但这是多年来的样子,在处理列表和数组一段时间后,这是有意义的。有关使用零索引数组的原因,请参阅here

至于代码,您正试图像一个索引列表一样访问它:

for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)

列表中只有一个条目,如下所示:

0

for循环开始迭代。它从int i = 1开始。小问题:数组中没有1索引。Java不知道该如何处理它,所以它就崩溃了。若要处理零索引列表,应使用

for(int i = 0; i < e.getEmployeeArrayList().size(); i++)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32597227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档