2013-09-28 56 views
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; 
    } 
} 

但是,我還有一些我需要的東西,我不知道如何實現。我的問題是我怎麼能:

  • 使國家字符串不顯示任何東西,是長於或短於兩個字母
  • 請在地址類郵政編碼變量僅顯示任何輸入的第一個五個字符在超過五個字符
  • 能夠存儲數據的多個員工
  • 可以指定有多少員工信息將在命令行
  • 存儲所有信息,在其他類中的一個員工被保存,併爲eac製作對象h僱員包含他們的所有信息

任何關於如何清理我的代碼的幫助或建議將不勝感激。

回答

1

首先,類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 = ""; 
} 
+0

我欣賞的迴應,但是這並不完全符合我要找的。製作員工的日期,姓名和地址字段聽起來像是個好主意,但我正在編寫這個任務;其參數指定它們都必須位於不同的類中。我也不熟悉館藏,我不能在我的程序中使用它們。最後,我需要一種方法來在啓動程序時指定員工數量,而不是通過輸入窗口。再次感謝您的輸入。 – user2709168

+0

所以傳遞員工數量作爲java程序的一個參數,並且:if(args.lenght!= 1){System.exit(0);} else {int howMany = Integer.parseString(args [0]);}'。而不是Collections,你可以在Array中存儲員工 - > Employee [] employyes = new Employee [howMany](); ...僱員[i] =新員工(...);'。如果你不想把這個類放到EMployee中,只需創建'EmployeeData'或類似的東西來包含所有這些類。 – smajlo

+0

如果您不知道如何在java中傳遞程序參數,請查看http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html。和數組教程http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – smajlo