0

我必须编写一个模拟员工的程序。员工有员工编号、名字和姓氏、由街道、城市、州和 5 位邮政编码组成的地址,以及由月、日和年组成的雇用日期。它必须使用雇员类、姓名类、地址类和日期类。每个类都必须记录用户输入的信息。这是我写的:

import javax.swing.JOptionPane;

public class AssignmentTen
{
    public static void main (String[] args)
    {   
        System.out.println();       

        int input1 = getInt ("Enter Employee Number:");
        Employee e1 = new Employee(input1);
        System.out.println("#" + e1.number);

        String input2 = getString ("Enter Employee First Name:");
        String input3 = getString ("Enter Employee Last Name:");
        Name n1 = new Name(input2, input3);
        System.out.println(n1.firstName + " " + n1.lastName);

        String input4 = getString ("Enter Employee Street:");
        String input5 = getString ("Enter Employee City:");
        String input6 = getString ("Enter Employee State (Initials):");
        int input7 = getInt ("Enter Employee Zip Code (5 Digits):");
        Address a1 = new Address (input4, input5, input6, input7);
        System.out.println(a1.eStreet + " " + a1.eCity + " " + a1.eState + " " + a1.eZipCode);

        int input8 = getInt ("Enter Employee Hire Month (MM):");
        int input9 = getInt ("Enter Employee Hire Day (DD):");
        int input10 = getInt ("Enter Employee Hire Year(YYYY):");
        Date d1 = new Date (input8, input9, input10);
        System.out.println("Hire Date: " + d1.month + "/" + d1.day + "/" + d1.year);
    }

    public static int getInt(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return Integer.parseInt(str);
    }

    public static String getString(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return str;
    }
}

class Employee
{
    int number;

    Employee(int newNumber)
    {
        number = newNumber;
    }
}

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }
}

class Address
{
    String eStreet;
    String eCity;
    String eState;
    int eZipCode;

    Address(String street, String city, String state, int zipCode)
    {
        eStreet = street;
        eCity = city;
        eState = state;
        eZipCode = zipCode;
    }
}

class Date
{
    int month;
    int day;
    int year;

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }
}

但是,我仍然需要一些我不知道如何实现的东西。我的问题是我怎么能:

  • 使状态字符串不显示任何长于或短于两个字母的内容
  • 使地址类中的邮政编码变量仅显示任何长度超过五个字符的输入的前五个字符
  • 能够为多个员工存储数据
  • 能够指定将在命令行中存储多少员工信息
  • 将单个员工的所有信息存储在另一个类中,并为每个员工创建包含所有信息的对象

任何有关如何清理代码的帮助或建议将不胜感激。

4

1 回答 1

1

首先,类Date, Name, Address应该用作Employee. 还要考虑变量名称,例如zipCode而不是input7.

能够为多个员工存储数据

您可以将员工存储在集合中,例如:

 Set<Employee> employess = new HashSet<>(); // field
 ...
 employess.add(e1);

能够指定将在命令行中存储多少员工信息

int howmany = getInt ("Howmany emp you want to put");
for(int i = 0 ; i < howmany; i++) {
    //invoke here extracted method getting all employee data
    //example: employees.add(getEmployeeData());
}

将单个员工的所有信息存储在另一个类中,并为每个员工创建包含所有信息的对象

我猜在将日期、姓名和地址放入员工问题后,问题将得到解决。

使 Address 类中的 zip code 变量仅显示 >any 长度超过五个字符的输入的前五个字符

int input7 = getInt ("Enter Employee Zip Code (5 Digits):"); //wrong varible name!
String zipCode = String.valueOf(input7);
if(zipCode.lenght() > 5) {
    zipCode= zipCode.subString(0,4);
}

使状态字符串不显示任何长于或短于两个字母的内容

String input6 = getString ("Enter Employee State (Initials):");
if(input6.lenght != 2) {
     input6 = "";
}
于 2013-09-28T21:22:50.010 回答