java - Unsure if I should split my subclasses -


i have text file reads in information of various employees. i've created 2 array list based on years of employee object. employee has 2 subclasses, salesman , executive. i'm wondering how should split commas if there's 2 subclasses, since can't call super class

here's employee class

public class employee  { private string name; private double monthlysalary;  public employee(string line) {     string[]split=line.split(",");     name=split[0];     monthlysalary=double.parsedouble(split[1]); }    public string getname() {     return name; }    public void setname(string name) {     this.name = name; }    public double getmonthlysalary() {     return monthlysalary; }    public void setmonthlysalary(double monthlysalary) {     this.monthlysalary = monthlysalary; }    public double annualsalary() {     return monthlysalary*12; }   public string tostring() {     string str;     str="name: "+name;     str+="\nmonthly salary: "+monthlysalary;     return str; } } 

the salesman class

public class salesman extends employee { private double annualsales;  public salesman(string name, double monthlysalary, double annualsales) {     super(name,monthlysalary);     this.annualsales=annualsales; }    public double getannualsales() {     return annualsales; }    public void setannualsales(double annualsales) {     this.annualsales = annualsales; }    public double annualsalary() {     double commision=annualsales*0.02;     if (commision>20000)     {         commision=20000;     }     double totalsalary=super.annualsalary()+commision;     return totalsalary; }   public string tostring() {     return super.tostring()+"\nannual sales: "+annualsales; } } 

the executive class

public class executive extends employee { private double stockprice;  public executive(string name, double monthlysalary, double stockprice) {     super(name,monthlysalary);     this.stockprice=stockprice; }   public double getstockprice() {     return stockprice; }   public void setstockprice(double stockprice) {     this.stockprice = stockprice; }   public double annualsalary() {     double bonus=0;     if(stockprice>50)     {         bonus=30000;     }     double totalsalary=super.annualsalary()+bonus;     return totalsalary; }   public string tostring() {     return super.tostring()+"\nstock price: "+stockprice; } } 

and driver class

import java.io.*; import java.util.*; public class employeedriver { public static void main(string[] args) {     string input;     scanner readinput=null;     scanner readfile = null;      try     {         readfile=new scanner(new file("c:\\mywork\tester.txt"));     }     catch(filenotfoundexception e)     {         system.out.println("the file can't opened");         system.exit(0);     }     list<employee> firstlist= new arraylist<>();     list<employee> secondlist= new arraylist<>();       while(readfile.hasnextline())     {       //to inserted     } }  } 

i split method mean parse file, think should place login in class, have nothing employee.

this parseemployeefile or scanempoyeefile, method parse() or scan().

this method should return standard record, list or map, depends on needs.

from record should able create proper employee.

to that, need called factory class.

this class contains logic create proper employee subclass object.

for example:

class employeefactory {      public static employee create(record record) {          if (record.get("annualsales") != null) {             return new salesman(record.get("name"),                                 record.get("monthlysalary"),                                 record.get("annualsales"));         }          if (record.get("stockprice") != null) {             return new executive(record.get("name"),                                 record.get("monthlysalary"),                                 record.get("stockprice"));         }          return new employee(record.get("name"),                            record.get("monthlysalary"));     } } 

this can improved needed.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -