2015-03-02 37 views
0

我有三個類創建員工的ArrayList

  • 員工
  • 生產工人
  • 班長類

我的想法是,以使生產和班長延長Employee類和然後創建另一個班級EmployeeList,以填寫有關生產工人和班長的信息。

如何從僱員類中獲取姓名和信息以迭代到arraylist?

我如何添加一半以上員工的隨機名單。工人和其他輪班主管?

員工:

public class Employee { 

public String EmployeeName; 
public String EmployeeNumber; 
public int hireyear; 
public double WeeklyEarning; 

public Employee() 
{ 
    EmployeeName = null; 
    EmployeeNumber = null; 
    hireyear = 0; 
    WeeklyEarning = 0; 

} 

public static final String[] Enum = new String[] { 
     "0001-A", "0002-B","0003-C","0004-D","0002-A", 
     "0003-B","0004-C","0005-D","0011-A", "0012-B", 
     "0013-C","0014-D","0121-A", "0122-B","0123-C" }; 

public static final String[] Ename = new String[] { 
     "Josh", "Alex", "Paul", "Jimmy", "Josh", "Gordan", "Neil", "Bob", 
     "Shiv", "James", "Jay", "Chris", "Michael", "Andrew", "Stuart"}; 

public String getEmployeeName() 
{ 
    return this.EmployeeName; 
} 

public String getEmployeeNumber() 
{ 
    return this.EmployeeNumber; 
} 

public int gethireyear() 
{ 
    return this.hireyear; 
} 

public double getWeeklyEarning() 
{ 
    return this.WeeklyEarning; 
} 

public String setEmployeeName(String EName) 
{ 
    return this.EmployeeName = EName; 
} 

public String setEmployeeNumber(String ENumber) 
{ 
    return this.EmployeeNumber = ENumber; 
} 

public int setEmployeehireyear(int Ehireyear) 
{ 
    return this.hireyear = Ehireyear; 
} 

public double setEmployeeweeklyearning(double Eweeklyearning) 
{ 
    return this.WeeklyEarning = Eweeklyearning; 
} 

} 

ProductionWorker:

import java.util.Random; 
public class ProductionWorker extends Employee { 
public double HourlyRate; 
public ProductionWorker() 
{ 
    super(); 
    HourlyRate = 0; 
} 

public static void main(String[] args) { 
    ProductionWorker pw = new ProductionWorker(); 
    Random rnd = new Random(); 
    int count =0; 
    // adding random Employees..... 
    while(count<5) 
    { 
     int num= rnd.nextInt(Enum.length); 
     int decimal = rnd.nextInt(10); 
     double dec = decimal/10; 

     pw.setEmployeeName(Ename[num]); 
     pw.setEmployeeNumber(Enum[num]); 
     pw.setEmployeehireyear(rnd.nextInt(35) + 1980); 
     pw.setEmployeeweeklyearning(rnd.nextInt(5000) + 5000); 
     pw.setHourlyRate(rnd.nextInt(44) + 6 + dec); 

     System.out.println("EmployeeName: " + pw.getEmployeeName() + "\nEmployeeNumber: " + pw.getEmployeeNumber() + 
      "\nHireYear: " + pw.gethireyear() + "\nWeeklyEarning: " +  pw.getWeeklyEarning() + 
      "\nHourlyRate: " + pw.getHourlyRate() +"\n"); 
     count++; 
    } 

} 

public double getHourlyRate() 
{ 
    return this.HourlyRate; 
} 

public void setHourlyRate(double hourlyrate) 
{ 
    this.HourlyRate = hourlyrate; 
} 

} 

ShiftSupervisor:

import java.util.Random; 
public class ShiftSupervisor extends Employee{ 
public double YearlySalary; 
public int GoalsCleared; 

public ShiftSupervisor() 
{ 
    super(); 
    YearlySalary = 0; 
} 

public static void main(String[] args) { 

    ShiftSupervisor S = new ShiftSupervisor(); 
    Random rnd = new Random(); 
    int count =0; 
    // adding random Employees..... 
    System.out.println("Adding Employees.."); 
    while(count<5) 
    { 
     int num= rnd.nextInt(Enum.length);   

     S.setEmployeeName(Ename[num]); 
     S.setEmployeeNumber(Enum[num]); 
     S.setEmployeehireyear(rnd.nextInt(35) + 1980); 
     S.setEmployeeweeklyearning(rnd.nextInt(100) * 100); 
     S.setYearlySalary(rnd.nextInt(40000) + 40000); 

     System.out.println("EmployeeName:" + S.getEmployeeName() + "\nEmployeeNumber: " + S.getEmployeeNumber() + 
      "\nHireYear: " + S.gethireyear() + "\nWeeklyEarning: " + S.getWeeklyEarning() + 
      "\nearlySalary: " + S.getYearlySalary() +"\n"); 
     count++; 
    } 

} 
// returns yearly salary 
public double getYearlySalary() 
{ 
    return this.YearlySalary; 
} 

// returns goals cleared 
public int getGoalsCleared() 
{ 
    return this.GoalsCleared; 
} 

// set yearly salary 
public void setYearlySalary(double yearlysalary) 
{ 
    this.YearlySalary = yearlysalary; 
} 

} 
+0

我澄清了你的介紹性陳述,將你的第二個問題從上到下提出來。我還編輯了你的標題,以更好地反映你的問題。你絕對應該嘗試從你的示例中刪除一些代碼,這對調試有吸引力,並且澄清你的兩個問題是什麼(儘量保持簡短) – 2015-03-06 22:59:29

回答

1

我會做的第一件事是在構造函數中設置的所有必要的字段。如果Employee只有具有名稱纔會「存在」,那麼它應該是構造函數的一部分。

然後,我建議你考慮重命名你的一些領域。當我第一次看到EnumString[]並突出顯示爲一種類型時,我花了一些時間才弄清楚究竟發生了什麼。將其重命名爲employeeNumbers可以解決此問題。

接下來,我認爲你應該有一個EmployeeGenerator類,其唯一目的是生成Employee s。

public class EmployeeGenerator { 

    public ProductionWorker generateProductionWorker() { 
     Random rng = new Random(); 
     int numberOfEmployeeNames = employeeNames.length; 
     String employeeName = employeeNames[rng.nextInt(numberOfEmployeeNames)]; 
     int numberOfEmployeeNumbers = employeeNumbers.length; 
     String employeeNumber = employeeNumbers[rng.nextInt(numberOfEmployeeNumbers)]; 
     ProductionWorker worker = new ProductionWorker(employeeName, employeeNumber); 

     int yearHired = rng.nextInt(100) + 1900; 
     worker.setHireYear(yearHired); 

     int hourlyRate = rng.nextInt(20) + 10; 
     worker.setHourlyRate(hourlyRate); 

     // any other fields... 
     return worker; 
    } 

    // method to generate shift supervisor 
} 

然後你就可以簡單地做

public static void main(String[] args) { 
    Random rng = new Random(); 
    int numberOfEmployeesToGenerate = 1000; 
    int minimumNumberOfProductionWorkers = numberOfEmployeesToGenerate/2; 

    int numberOfProductionWorkersToGenerate = 
     minimumNumberOfProductionWorkers + rng.nextInt(100); 

    int numberOfSupervisorsToGenerator = 
     numberOfEmployeesToGenerate - numberOfProductionWorkersToGenerate; 

    List<Employee> employees = new ArrayList<>(); 
    EmployeeGenerator generator = new EmployeeGenerator(); 
    for (int i = 0; i < numberOfProductionWorkersToGenerate; i++) { 
     ProductionWorker worker = generator.generateProductionWorker(); 
     employees.add(worker); 
    } 

    for (int i = 0; i < numberOfSupervisorsToGenerate; i++) { 
     Supervisor supervisor = generator.generateSupervisor(); 
     employees.add(supervisor); 
    } 
} 

這樣應該可以給你一個正確的方向點。這不是完美的代碼,並且還有其他方法來重構此代碼,使其更易於維護和高性能。

+0

這正是我需要的,謝謝了解我來自哪裏 – 2015-03-02 19:16:11

0

當你說你要添加員工的隨機列表,你的意思是什麼呢?

當前您只實例化一個ProductionWorker和一個ShiftSupervisor,更改成員變量的值並將一些文本打印到StdOut

是否要將實例存儲在列表中或者控制檯輸出是否足夠?

此外,您有兩個main-方法。哪一個將被執行?將一個Main類作爲應用程序的入口點並從那裏創建實例可能會更好。

一般來說,你可以做這樣的事情:

public class Main { 
    public static void main(String[] args) { 
     List<Employee> emps = new ArrayList<>(); 
     for (int i = 0; i < 5; i++) { 
      //create new employee 
      emps.add(newEmployee); 
     } 
     //do something with list 
    } 
}