我被要求创建一个名为listOfWorkers的方法,在其中我将通过从文件“workers.txt”读取数据来创建我拥有的每个类型的3个工作者的对象,然后将对象存储到类型的ArrayList中工人并将其归还。我已经设法通过从文件中读取数据来制作3类工作者的每种类型的对象,但现在我不知道如何将它们存储到ArrayList中。有帮助吗?
我正在编写课程
import java.util.*;
import java.io.*;
public class WorkerBenefits
{
public ArrayList<Worker> listOfWorkers()
{
try
{
File ifile = new File("worker.txt");
Scanner scan = new Scanner(ifile);
while (scan.hasNextLine())
{
String line = scan.nextLine();
StringTokenizer st = new StringTokenizer(line,",");
String jobs = st.nextToken();
Jobs jobType = Jobs.valueOf(jobs);
//Engineer Object type
if (jobType == Jobs.ELECTRICAL_ENGINEER || jobType == Jobs.MECHANICAL_ENGINEER)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double weeklyBen = Double.parseDouble(str3);
Engineer eng = new Engineer(name,social,years,jobType,weeklyBen);
}
//Admin. Person. Object type
else if (jobType == Jobs.ADMINISTRATIVE_SECRETARY || jobType == Jobs.ADMINISTRATIVE_ASSISTANT)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double rate = Double.parseDouble(str3);
String str4 = st.nextToken();
double hours = Double.parseDouble(str4);
AdministrativePersonnel ap = new AdministrativePersonnel(name,social,years,jobType,rate,hours);
}
//Management Object type
else if (jobType == Jobs.ENGINEERING_MANAGER || jobType == Jobs.ADMINISTRATIVE_MANAGER)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double weeklyBen = Double.parseDouble(str3);
String str4 = st.nextToken();
double bonus = Double.parseDouble(str4);
Management mang = new Management(name,social,years,jobType,weeklyBen,bonus);
}
}
}
catch (IOException ioe)
{
System.out.println("Something went wrong");
}
return ArrayList;
}
}
以下是其他类的代码,以防你们需要它们:
工作
public enum Jobs {ELECTRICAL_ENGINEER, MECHANICAL_ENGINEER, ADMINISTRATIVE_SECRETARY, ADMINISTRATIVE_ASSISTANT, ENGINEERING_MANAGER, ADMINISTRATIVE_MANAGER, NONE};
工人
public abstract class Worker
{
public String name;
public int socialSecurity;
private int yearsExperience;
public Jobs et = null;
public static int id = 0;
public int currentID = 0;
public Worker ()
{
name = "AnyName";
socialSecurity = 12345;
yearsExperience = 0;
et = Jobs.NONE;
id++;
currentID = id;
}
public Worker (String n, int ss, int ye, Jobs j)
{
id++;
currentID = id;
name = n;
socialSecurity = ss;
yearsExperience = ye;
et = j;
}
public String getName()
{
return name;
}
public int getSocialSecurity()
{
return socialSecurity;
}
public int getYearsExperience()
{
return yearsExperience;
}
public Jobs getJobs()
{
return et;
}
public void setName(String n1)
{
name = n1;
}
public void setSocialSecurity(int ss1)
{
socialSecurity = ss1;
}
public void setYearsExperience(int ye1)
{
yearsExperience = ye1;
}
public void setJobs(Jobs et1)
{
et = et1;
}
public abstract double benefitsCalculation(Jobs et2);
public String toString()
{
String output = "The name is: " + name + "The Job type is: " + et + "The id is: " + currentID;
return output;
}
}
工程师
public class Engineer extends Worker
{
private double weeklyBenefits;
public Engineer ()
{
super();
weeklyBenefits = 400.00;
}
public Engineer (String n, int ss, int ye, Jobs j, double wb)
{
super(n,ss,ye,j);
weeklyBenefits = wb;
super.setName(n);
super.setSocialSecurity(ss);
super.setYearsExperience(ye);
super.setJobs(j);
}
public double benefitsCalculation(Jobs et2)
{
double benefits = 0.0;
if (et2 == Jobs.ELECTRICAL_ENGINEER)
{
benefits = weeklyBenefits+super.getYearsExperience()*120.00;
}
else if (et2 == Jobs.MECHANICAL_ENGINEER)
{
benefits = weeklyBenefits/2+super.getYearsExperience()*120.00;
}
else if (et2 != Jobs.ELECTRICAL_ENGINEER || et2 != Jobs.MECHANICAL_ENGINEER)
{
benefits = 0;
}
return benefits;
}
public double getWeeklyBenefits()
{
return weeklyBenefits;
}
public void setWeeklyBenefits(double wb)
{
weeklyBenefits = wb;
}
public String toString()
{
String output = "The benefit is " + weeklyBenefits + super.toString();
return output;
}
}
行政人员
public class AdministrativePersonnel extends Worker
{
private double rate;
private double hours;
public AdministrativePersonnel()
{
super();
rate = 10.0;
hours = 10.0;
}
public AdministrativePersonnel(String n, int ss, int ye, Jobs j, double r, double h)
{
super(n,ss,ye,j);
rate = r;
hours = h;
super.setName(n);
super.setSocialSecurity(ss);
super.setYearsExperience(ye);
super.setJobs(j);
}
public double benefitsCalculation (Jobs et2)
{
double benefits = 0.0;
if (et2 == Jobs.ADMINISTRATIVE_SECRETARY)
{
benefits = rate*hours+super.getYearsExperience()*15.00;
}
else if(et2 == Jobs.ADMINISTRATIVE_ASSISTANT)
{
benefits = rate*hours+super.getYearsExperience()*25.00;
}
else if (et2 != Jobs.ADMINISTRATIVE_SECRETARY || et2 != Jobs.ADMINISTRATIVE_ASSISTANT)
{
benefits = 0;
}
return benefits;
}
public double getRate()
{
return rate;
}
public double getHours()
{
return hours;
}
public void setRate(double r1)
{
rate = r1;
}
public void setHours(double h1)
{
hours = h1;
}
public String toString()
{
String output = "The rate is: " + rate + "The hours are: " + hours + super.toString();
return output;
}
}
管理
public class Management extends Worker
{
private double weeklyBenefits;
private double bonus;
public Management()
{
super();
weeklyBenefits = 0.0;
bonus = 0.0;
}
public Management(String n, int ss, int ye, Jobs j, double wb, double b)
{
super(n,ss,ye,j);
weeklyBenefits = wb;
bonus = b;
super.setName(n);
super.setSocialSecurity(ss);
super.setYearsExperience(ye);
super.setJobs(j);
}
public double benefitsCalculation (Jobs et2)
{
double benefits = 0.0;
if (et2 == Jobs.ENGINEERING_MANAGER)
{
benefits = weeklyBenefits+bonus;
}
else if(et2 == Jobs.ADMINISTRATIVE_MANAGER)
{
benefits = weeklyBenefits+0.5*bonus;
}
else if (et2 != Jobs.ENGINEERING_MANAGER || et2 != Jobs.ADMINISTRATIVE_MANAGER)
{
benefits = 0;
}
return benefits;
}
public double getWeeklyBenefits()
{
return weeklyBenefits;
}
public double getBonus()
{
return bonus;
}
public void setWeeklyBenefits(double wb)
{
weeklyBenefits = wb;
}
public void setBonus(double b)
{
bonus = b;
}
public String toString()
{
String output = "The weekly benefits are: " + weeklyBenefits + "The bonus is: " + bonus + super.toString();
return output;
}
}
答案 0 :(得分:1)
listOfWorkers()
方法需要创建一个worker列表,添加每个新的worker对象然后返回列表。
public List<Worker> listOfWorkers()
{
List<Worker> workers = new ArrayList<Worker>();
try
{
File ifile = new File("worker.txt");
Scanner scan = new Scanner(ifile);
while (scan.hasNextLine())
{
String line = scan.nextLine();
StringTokenizer st = new StringTokenizer(line,",");
String jobs = st.nextToken();
Jobs jobType = Jobs.valueOf(jobs);
//Engineer Object type
if (jobType == Jobs.ELECTRICAL_ENGINEER || jobType == Jobs.MECHANICAL_ENGINEER)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double weeklyBen = Double.parseDouble(str3);
Engineer eng = new Engineer(name,social,years,jobType,weeklyBen);
workers.add(eng);
}
//Admin. Person. Object type
else if (jobType == Jobs.ADMINISTRATIVE_SECRETARY || jobType == Jobs.ADMINISTRATIVE_ASSISTANT)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double rate = Double.parseDouble(str3);
String str4 = st.nextToken();
double hours = Double.parseDouble(str4);
AdministrativePersonnel ap = new AdministrativePersonnel(name,social,years,jobType,rate,hours);
workers.add(ap);
}
//Management Object type
else if (jobType == Jobs.ENGINEERING_MANAGER || jobType == Jobs.ADMINISTRATIVE_MANAGER)
{
String name = st.nextToken();
String str1 = st.nextToken();
int social = Integer.parseInt(str1);
String str2 = st.nextToken();
int years = Integer.parseInt(str2);
String str3 = st.nextToken();
double weeklyBen = Double.parseDouble(str3);
String str4 = st.nextToken();
double bonus = Double.parseDouble(str4);
Management mang = new Management(name,social,years,jobType,weeklyBen,bonus);
workers.add(mang);
}
}
}
catch (IOException ioe)
{
System.out.println("Something went wrong");
}
return workers;
}
答案 1 :(得分:1)
public ArrayList<Worker> listOfWorkers()
{
List<Worker> list=new ArrayList<Worker>();
try
{
File ifile = new File("worker.txt");
Scanner scan = new Scanner(ifile);
while (scan.hasNextLine())
{
// no change
if (jobType == Jobs.ELECTRICAL_ENGINEER || jobType == Jobs.MECHANICAL_ENGINEER)
{
// no change
Worker eng = new Engineer(name,social,years,jobType,weeklyBen);
list.add(eng);
}
else if()
{
//do same as above for other workers
}
}
return list;
}
答案 2 :(得分:1)
在try块之前,创建一个新的worker对象ArrayList:
ArrayList<Worker> workerList = new ArrayList<Worker>();
在while循环的开头,声明一个新的worker变量但是还没有创建它:
Worker worker = null;
在每个if块中,将结果分配给worker,而不是将其分配给Engineer
,Management
或AdministrativePersonnel
类型的新变量。例如,替换为:
Engineer eng = new Engineer(name,social,years,jobType,weeklyBen);
用这个:
worker = new Engineer(name,social,years,jobType,weeklyBen);
这是合法的,因为Engineer
,Management
和AdministrativePersonnel
都是Worker
的子类。
在while循环结束之前,将新工作人员添加到列表中:
workerList.add(worker);
最后,从函数返回workerList。