gpt4 book ai didi

java - 创建员工的 ArrayList

转载 作者:行者123 更新时间:2023-12-01 11:51:08 24 4
gpt4 key购买 nike

我有三门课

  • 员工
  • 生产 worker
  • 值类主管类

我的想法是让生产和轮类主管扩展员工类,然后创建另一个类EmployeeList来填充有关生产 worker 和轮类主管的信息。

如何从员工类中获取姓名和信息以迭代到数组列表中?

我如何添加一半以上是产品的员工的随机列表。 worker 和其他值类主管?

员工:

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

}

生产 worker :

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

}

轮类主管:

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

}

最佳答案

我要做的第一件事是在构造函数中设置所有必需的字段。如果 Employee 在拥有名称之前并不“存在”,那么它应该是构造函数的一部分。

然后,我建议您考虑重命名一些字段。当我第一次看到 Enum 作为一个 String[] 并突出显示为一种类型时,我花了一些时间才弄清楚到底发生了什么。将其重命名为 employeeNumbers 可以解决此问题。

接下来,我认为您应该有一个 EmployeeGenerator 类,其唯一目的是生成 Employee

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

这有望为您指明正确的方向。这不是完美的代码,还有其他方法可以重构它,以使其更易于维护和提高性能。

关于java - 创建员工的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com