java - 记录数据、在命令行中输入数据、限制整数长度

标签 java class object command-line

我必须编写一个对员工进行建模的程序。员工拥有员工编号、名字和姓氏、由街道、城市、州和 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;
    }
}

但是,我还有一些需要但不确定如何实现的东西。我的问题是我怎样才能:

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

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

最佳答案

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

Be able to store data for multiple Employees

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

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

Be able to specify how many employees information will be stored for in the command line

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());
}

Store all of the information for a single employee in another class, and make objects for >each employee containing all of their information

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

Make the zip code variable in the Address class only display the first five characters of >any inputs that are longer than five characters

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);
}

Make the state string not display anything that is longer or shorter than two letters

String input6 = getString ("Enter Employee State (Initials):");
if(input6.lenght != 2) {
     input6 = "";
}

关于java - 记录数据、在命令行中输入数据、限制整数长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19071723/

相关文章:

java - 使用驱动程序中的构造函数

c++ - 如何为我在 C++ 中创建的对象构建迭代器

Javascript 从 ajax 响应访问嵌套对象

java - NetCDF 4.5 Java NetCDF 文件版本 4 的问题 + HDF 的旧代码不起作用

java - 在父类(super class)中调用父类(super class)方法

java - 在启用 Spring 的 Velocity 模板内绑定(bind)变量集合

CSS类名参数?

c++ - 静态成员函数访问静态私有(private)变量时链接器错误

java - 无法在方法之后打印 TestNg 中的语句

javascript - 如何在 JavaScript 中使用对象?