如何修改此程序以使用ArrayList?

时间:2013-09-29 03:11:31

标签: java arraylist

首先,这里有说明:
http://ideone.com/eRHwUo
是的,这是功课!有了这样说(因为我只是喜欢这种语言),当你发布答案时,请尝试使用伪代码,这样我就不会将复制并粘贴到我的程序中。谢谢!

就代码而言,我已经为用户输入所有输入的部分完成了所需的一切。 但是,我需要帮助将数据从类“转移”到我们应该使用的'ArrayList'。我认为一旦我把它弄下来,我就能够对数组进行排序以找到选择“B”的ID号,如果用户输入“C”,我将只循环显示其中所有内容的数组。

无论如何,在代码上(这是主要的):

/*
 * Name:
 * Date:
 * Assignment 2
 */

import java.util.Scanner;

public class homework 
{
    public static void main(String[] args)
    {
        char userSelection;
        String convertString;
        String userStrings;
        Scanner kbd = new Scanner(System.in);

        do 
        {
            System.out.println("Here are your choices:");
            System.out.println("A. Enter employee data" +
                             "\nB. Search for employee data" +
                             "\nC. List all data" +
                             "\nD. Exit");

            convertString = kbd.next();
            userSelection = convertString.charAt(0);

            switch(userSelection)
            {
            case 'A':
                GetUserInfo();
                break;
            case 'B':
                // Stuff;
                break;
            case 'C':
                // Display all data;
                break;
            case 'D':
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Error, that is not a valid entry. Please try again.");
            }
        } while (userSelection > 'D');
    }

    // Write functions here
    public static void GetUserInfo()
    {
        String firstName;
        String lastName;
        String empID;
        double hourlyRate;
        int hoursWorked;
        double withPercent;

        Scanner kbd = new Scanner(System.in);

        System.out.println("What is your first name?");
        firstName = kbd.next();

        System.out.println("What is your last name?");
        lastName = kbd.next();

        System.out.println("What is your employee ID?");
        empID = kbd.next();

        Employee user = new Employee(empID);

        user.setFirstName(firstName);
        user.setLastName(lastName);

        System.out.println("What is your hourly rate?");
        hourlyRate = kbd.nextDouble();

        System.out.println("How many hours did you work?");
        hoursWorked = kbd.nextInt();

        System.out.println("What is your withholding percentage?");
        withPercent = kbd.nextDouble();

        Pay user1 = new Pay();

        user1.setHourlyRate(hourlyRate);
        user1.setHoursWorked(hoursWorked);
        user1.setWithPercent(withPercent);
    }
}

这是Employee类:

public class Employee 
{
    // Members of the class
    String firstName;
    String lastName;
    String employeeID;
    // remember about the pay object

    // EmployeeID constructor
    public Employee(String empID)
    {
        this.employeeID = empID;
    }

    // Below are the various getters and setters of the Employee class
    public String getFirstName() 
    {
        return firstName;
    }

    public void setFirstName(String firstName) 
    {
        this.firstName = firstName;
    }

    public String getLastName() 
    {
        return lastName;
    }

    public void setLastName(String lastName) 
    {
        this.lastName = lastName;
    }

    public String getEmployeeID() 
    {
        return employeeID;
    }

}

这是Pay类:

public class Pay 
{
    // Members of the class
    double hourlyRate;
    int hoursWorked;
    double withPercent;

    // Various getters and setters of the Pay class
    public double getHourlyRate() 
    {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate)
    {
        this.hourlyRate = hourlyRate;
    }

    public int getHoursWorked() 
    {
        return hoursWorked;
    }

    public void setHoursWorked(int hoursWorked) 
    {
        this.hoursWorked = hoursWorked;
    }

    public double getWithPercent() 
    {
        return withPercent;
    }

    public void setWithPercent(double withPercent)
    {
        this.withPercent = withPercent;
    }   

    // Calculates the raw payment
    public double CalcPayRate(double hourlyRate, int hoursWorked)
    {
        return hourlyRate * hoursWorked;
    }

    // If the employee has worked overtime, calculates the new payment
    public double CalcOvertimePay(double hourlyRate, int hoursWorked)
    {
        double rawPay = 0;

        rawPay = hourlyRate * hoursWorked;

        if (hoursWorked > 40)
        {
            rawPay *= 1.5;
        }

        return rawPay;
    }

    // Calculates final amount that the employee will be paid
    public double CalcTotalPay(double hourlyRate, int hoursWorked, double withPercent)
    {
        double rawPay = 0;
        double subTotalPay = 0;
        double finalPay = 0;

        rawPay = hourlyRate * hoursWorked;
        subTotalPay = rawPay * withPercent;
        finalPay = rawPay - subTotalPay;

        return finalPay;
    }
}

那么,请问? 最后评论:
(1)我不明白员工类下应该做什么'Pay Object'? (2)如何使用输入数据创建“支付对象”,然后创建员工对象? 这些是我不太清楚的说明中的两个部分,因此,如果你能对它有所了解,它会有所帮助!
(3)如果我的语法中有任何相似之处,请告诉我,以便我可以相应地进行更改。我还是这种语言的新手,所以任何帮助都会很棒。

编辑:我已使用评论更新了我的代码:

public class homework 
{
    public static void main(String[] args)
    {
        // ArrayList<Employee> employeeList = new ArrayList<Employee>();

        do 
        {
            // Stuff commented out for readability

            case 'A':
                PromptForInput();
                // Employee emp = PromptForInput();
                // employeeList.add(emp);
                break;
            } while (userSelection > 'D');
    }

    // public static Employee PromptInput()
    public static void PromptForInput()
    {           
        Employee user = new Employee(empID);

        // Isn't this supposed to be returned as well?          
        Pay user1 = new Pay();

        user1.setHourlyRate(hourlyRate);
        user1.setHoursWorked(hoursWorked);
        user1.setWithPercent(withPercent);

        //return user;
    }
}

这应该是怎么看的?
我仍然不理解作业的“付费对象”部分..

3 个答案:

答案 0 :(得分:4)

首先看一下Collections

如果我这样做......

我会从Employee方法返回getUserInfo并将其添加到调用方法中的ArrayList。这使getUserInfo方法专注于单一责任。

如果用户输入的值未通过验证,我还允许getUserInfo返回null,但这可能超出了要求的范围。

例如......

List<Employee> employees = // create ArrayList...
//...

Employee emp = getUserInfo();
if (emp != null) {
    employees.add(emp);
}

您还应该在创建Pay对象时设置Employee对象,这会将Pay对象与Employee相关联,以便稍后找到它/ p>

关于你的Pay对象。 CalcPayRateCalcOvertimePayCalcTotalPay不需要参数,因为这些方法所需的所有信息都应该从对象的属性中获得...

例如......

public double getRawPay() {
    return getHoursWorked() & getHourlyRate();
}

我还建议您查看Code Conventions for the Java Programming Language

更新了#1

Pay对象与Employee相关联与您对员工姓名之间的关联方式没有区别

基本上,你需要提供一个实例字段/属性来保存引用,以及一些设置和获取引用的方法,例如......

public class Employee 
{
    //...
    Pay pay;

    // EmployeeID constructor
    public Employee(String empID)
    {
        this.employeeID = empID;
    }

    // EmployeeID constructor
    public Employee(String empID, Pay pay)
    {
        this(empID);
        this.pay = pay;
    }

    public void setPay(Pay pay) {
        this.pay = pay;
    }

    public Pay getPay() {
        return pay
    }
    //...
}

我提供了第二个构造函数,你真的不需要它,但它是一个如何实现它的演示。

然后,您只需将Pay的引用传递给Employee ...

Employee emp = new Employee(...);
//...
Pay pay = new Pay();
//...
emp.setPay(pay);

更新#2

另一种方法是在创建员工时创建一个Pay实例...

public class Employee 
{
    //...
    Pay pay = new Pay();

    // EmployeeID constructor
    public Employee(String empID)
    {
        this.employeeID = empID;
    }

这表示我们不再需要setPay,但这取决于您。

当您需要获取或设置Pay的值时,您可以简单地询问Employee的实例

Employee emp = ...;
emp.getPay().setHourlyRate(hourlyRate);
//...
double hourlyRate = emp.getPay().getHourlyRate();

更新了#3

基本上,您需要将Pay的实例与Employee的实例相关联。如果它更容易,请将其命名为其他...

基于Update#1的示例

public static Employee PromptForInput()
{
    String firstName;
    String lastName;
    String empID;
    double hourlyRate;
    int hoursWorked;
    double withPercent;

    //...
    Employee user = new Employee(empID);

    user.setFirstName(firstName);
    user.setLastName(lastName);

    //...

    Pay payObj = new Pay();

    payObj .setHourlyRate(hourlyRate);
    payObj .setHoursWorked(hoursWorked);
    payObj .setWithPercent(withPercent);

    // Don't forget to give an instance of Pay to the instance
    // of Employee
    user.setPay(payObj);

    return user;
}

Employee视为一个容器,它包含员工的姓名和ID以及他们的工资细节。当你开始使用Employee的新实例时,它只是一个空容器,你需要填充它......

如果需要,您只需通过Pay

Employee请求getPay的{​​{1}}实例

更新#4

基于你的新代码,我只会提出一个小建议......

在显示所有数据选项时,您可以使用以下循环显示员工和员工的工资率,例如......

for (int i = 0; i < employeeList.size(); i++)
{
    Employee emp = employeeList.get(i);
    System.out.println(emp);
    System.out.println(emp.getPay().CalcPayRate());
}

答案 1 :(得分:2)

查看说明,我认为您应该创建一个具有泛型类型Employee的ArrayList。 GetUserInfo()应返回它刚刚创建的Employee对象,并且应该在函数调用之后将此Employee对象存储到ArrayList中,并使用其.add()函数。

编辑:

我尝试了你的三个问题。

1)应使用Employee对象中的变量引用Pay Object。您可以在Employee Object中使用create another setter方法来执行此操作。

2)我相信你已经通过以下代码做到了这一点:

user1.setHourlyRate(hourlyRate);
user1.setHoursWorked(hoursWorked);
user1.setWithPercent(withPercent);

3)语法对我来说很好。

答案 2 :(得分:1)

:一种。输入员工数据

您正在利用一个类(名词)来执行操作。操作通常转换为方法调用。即使你使用'get'(convention类不应该以get开头)也建议引入一种方法。因此,将GetUserInfo更改为

private static Employee promptForEmployeeInfo()

//or more simply
private static Employee getEmployee()

然后,您可以在收集所有必要信息(包括付款信息)后返回员工对象。然后,您需要向员工对象提供薪资信息。在最基本的形式中,这将通过实例化Pay对象并将其传递给Employee的构造函数(对于不可变对象,这是比您需要的更高级的技术)或通过setter方法来实现。

在main方法中,List将在do / while循环之前实例化。每次用户输入“A”时,您都会调用employeeList.add('在此处插入员工方法调用')。

  

(1)我不明白在Payee类

下应该做什么'Pay Object'

在面向对象编程(OOP)中,将数据和对该数据的操作封装到描述性的,适当命名的类中是OOP约定的一部分。