2013-09-30 31 views
0

我對Java很陌生。我必須創建一個模擬員工的程序。該員工擁有員工編號,姓名和姓名,地址包含街道,城市和州,以及包含月份,年份和日期的僱用日期。這是我的代碼:在循環中創建對象並使用其他對象作爲類構造函數

import javax.swing.JOptionPane; 

public class AssignmentTen 
{ 
    public static void main (String[] args) 
    { 
     System.out.println(); 
     int num = Integer.parseInt(args[0]); 
     int eNumber; 
     String input2; 
     String input3; 
     String input4; 
     String input5; 
     String input6; 
     int input7; 
     int input8; 
     int input9; 
     int input10; 

     Employee[] employee = new Employee[num] 
     for (int i = 0; i < num, i++) 
     employee[i] = new Employee() 

     input2 = getString ("Enter Employee First Name:"); 
     input3 = getString ("Enter Employee Last Name:"); 
     input4 = getString ("Enter Employee Street:"); 
     input5 = getString ("Enter Employee City:"); 
     input6 = getString ("Enter Employee State (Initials):"); 
     input7 = getInt ("Enter Employee Zip Code (5 Digits):"); 
     input8 = getInt ("Enter Employee Hire Month (MM):"); 
     input9 = getInt ("Enter Employee Hire Day (DD):"); 
     input10 = getInt ("Enter Employee Hire Year(YYYY):"); 

     eNumber = getInt ("Enter Employee Number:"); 
     System.out.println("#" + eNumber); 

     Name n1 = new Name(input2, input3); 
     System.out.println(n1.firstName + " " + n1.lastName); 

     Address a1 = new Address (input4, input5, input6, input7); 
     System.out.println(a1.eStreet + " " + a1.eCity + " " + a1.eState + " " + a1.eZipCode); 

     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 
{ 

} 

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

我怎樣才能讓Employee類使用的名稱,地址和日期類作爲構造函數的所有員工的信息存儲在一個對象?另外,我的程序中還有一個單獨的Employee,Name,Address和Date類。另外,如何創建一個for循環來創建一個等於num整數(從命令行輸入)的僱員對象,以便每個對象都有自己的Name,Date和Address?

我知道這段代碼可能不會編譯,我只需要完成它。

回答

0

員工都有一個地址,姓名和僱傭日期,所以你必須聲明類型的實例變量地址,姓名和日期。然後你可以寫一個構造函數的地址,姓名和日期作爲構造函數參數,並將其分配給員工的實例變量


Class Employee{ 
    private Name name; 
    private Address address; 
    private Date hireDate; 
    public Employee(Name name,Address address,Date hireDate){ 
      this.name = name; 
      this.address = address; 
      this.hireDate = hireDate; 
     } 
} 

for循環

Employee[] employees = new Employee[noOfEmployees] 

for(int i = 0; i < noOfEmployees; i++){ 
    Name name = new Name(String first, String last); 
    Address address = new Address(String street, String city, String state, int zipCode); 
    Date hireDate = new Date(int eMonth, int eDay, int eYear); 
    employees[i] = new Employee(name,address,hireDate); 
} 

其中noOfEmployees是您想要創建的員工數量。 ,您可以使用java的內建Date類java.util.Date for hireDate。

0

你需要創建一個非默認構造函數爲您Employee類,並允許您NameAddressDate參數,通過創建匹配的實例變量與它相關聯。

(在下面,你的代碼也不得不爲每個字段取值方法)

class Employee 
{ 
    private Name name; 
    private Address address; 
    private Date date; 

    public Employee(Name name, Address address, Date date) 
    { 
    this.name = name; 
    this.address = address; 
    this.date = date; 
    } 
} 
+0

你是什麼意思「允許我的姓名,地址和日期參數與它關聯?」 – user2709168

+0

@ user2709168我的意思是,如果您沒有爲Employee類定義實例變量,那麼其他類的實例就「存在」,並且它們與員工的關係沒有正確封裝。 – Catchwa

相關問題