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