I'm asked to create a method called listOfWorkers in which i will create objects of each type of the 3 workers I have by reading the data from the file "workers.txt" and then store the objects into an ArrayList of type Worker and return it. I have managed to make the objects of each type of the 3 workers with by reading the data from the file, but now I don't know how to store them into an ArrayList. Any help guys?
Class i'm coding in right now
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;
}
}
Here is the code for the other classes just in case you guys need them:
Jobs
public enum Jobs {ELECTRICAL_ENGINEER, MECHANICAL_ENGINEER, ADMINISTRATIVE_SECRETARY, ADMINISTRATIVE_ASSISTANT, ENGINEERING_MANAGER, ADMINISTRATIVE_MANAGER, NONE};
Worker
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;
}
}
Engineer
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;
}
}
Administrative Personnel
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;
}
}
Management
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;
}
}
The listOfWorkers()
methods would need to create a list of workers, add each of the new worker object then return the list.
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;
}
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;
}
Before the try block, create a new ArrayList of worker objects:
ArrayList<Worker> workerList = new ArrayList<Worker>();
At the beginning of the while loop, declare a new worker variable but don't create it yet:
Worker worker = null;
Within each if block, assign the result to worker rather than assigning it to a new variable of type Engineer
, Management
, or AdministrativePersonnel
. For example, replace this:
Engineer eng = new Engineer(name,social,years,jobType,weeklyBen);
with this:
worker = new Engineer(name,social,years,jobType,weeklyBen);
This is legal because Engineer
, Management
, and AdministrativePersonnel
are all subclasses of Worker
.
Just before the end of the while loop, add your new worker to the list:
workerList.add(worker);
Finally, return workerList from the function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.