java - How do I store objects created into an ArrayList and return it? -
i'm asked create method called listofworkers in create objects of each type of 3 workers have reading data file "workers.txt" , store objects arraylist of type worker , return it. have managed make objects of each type of 3 workers reading data file, don't know how store them arraylist. 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 code other classes in case 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 " + 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; } }
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 above other workers } } return list; }
Comments
Post a Comment