Search code examples
javaclassoverridingtext-filessuperclass

Are there any errors in my code?


Problem: Finally there should be a fourth class that contains the main method. It should read in employee information from a text file. Each line of the text file will represent the information for one employee for one year. An example of how the text file will look is shown below:

Once all the employee data is read in, a report should be displayed on the console for each of the two years. Each line of the report should contain all original data supplied for each employee together with that employee's annual salary for the year. For each of the two years, an average of all salaries for all employees for that year should be computed and displayed.

Question: Are there any errors in my code? Or things I could fix?

Super Class

public class Employee {

//Instance variables of super
private String name;
private double monthlySal;
private double annualSalary;

//Super constructor
public Employee(String name, double monthlySal) {
 this.name = name;//Initialization
 this.monthlySal = monthlySal;//Initialization
}//End of Constructor


//Method for annualSalary
public void annualSalary(double annualSalary){
    this.annualSalary = monthlySal * 12;
    System.out.println("Annual Salary: " +annualSalary);
}//End of Method


//toString Method
public String toString() {
    return "Employee Name: " + name + "\n" + "monthly Salary: " +monthlySal;
   }//End of toString Method
}//End of Employee Method

1st Subclass

public class Salesman extends Employee {
private String name;
private double monthlySal;
private double annualSales;
private double commission;
private double annualSalary;//Annual Salary

//subclass Constructor
  public Salesman(String name, double monthlySal, double annualSalary,  double   commission) {
    super(name, monthlySal);
    this.annualSales = annualSales;
    this.commission = 0.2 * annualSales;
 }//End of Constructor



//Overridden Method
@Override
public void annualSalary(double annualSalary){
this.annualSalary = commission + super.annualSalary;

}//End of Override Method

//Overriden toString Method
@Override
public String toString(){
    return "Employee Name: " + name + "\n" + "Monthly Salary: " + monthlySal  + "\n" + "Annual Sales: " +annualSales;
}

}

2nd Subclass

public class Executive extends Employee {

private double stockPrice;
private String name;
private double monthlySal;
private double annualSalary; 
private double bonus = 0;


 //Constructor
 public Executive(String name, double monthlySal, double annualSalary, double stockPrice) {
    super(name, monthlySal);
    this.stockPrice = stockPrice;
    this.annualSalary = annualSalary;

    if(stockPrice >= 50) {
        bonus = 30000;
    } else {
        bonus = 0;
    }//End of If Me
}//End of Constructor

//Override Method for annualSalary
@Override
public void annualSalary(double annualSalary){
    this.annualSalary = super.annualSalary + bonus;
}//End of Override Method

//toString Override Method
@Override
public String toString() {
return "Employee Name: " + name + "\n" + "Monthly Salary: " + monthlySal + "\n" + "Current Stock Price: " +stockPrice;
}

}

Text File - employee.txt

2014 Employee Clark, Sam 3000
2014 Salesman Samson, Jones 4000 40000
2014 Executive Brandon, Thurman 10000 70
2015 Executive Brandon, Thurman 11000 70
2015 Salesman Samson, Jones 4500 30000
2015 Employee Clark, Sam 3500


//4th Class
import java.io.File;
import java.FileNotFoundException;

public class TestEmployee {


//Start Main
   public static void main(String[] args) {

   private double yearOneAverSalary = 55000;
   private double yearTwoAverSalary = 45000;


   System.out.println("The average combined yearly salary for year 2014  employees is: " + yearOneAverSalary);
   System.out.println("The average combined yearly salary for 2015 employees is: " +yearTwoAverSalary);

}//End Main

  public static void readFile(File "src/employee.txt") throws IOException {
    String line;
    try(BufferedReader) br = Files.newBufferedReader(file.toPath()) {
        while((line = br.readLine())1=null) {
        System.out.println(line)//prints every line in file
          }//End of While
       }//End of Try
    }//End of ReadFile

Solution

  • I came up with a method that reads an individual line and the whole txt file. What it does is it goes through each line in the txt file and splits it up by " ", a space. Then because the order of the values is the same I just set values based on where they were in the line.

    One thing that I would recommend taking a look at is the constructors. I'm not sure whether or not the date value was supposed to go in the Employee class or not.

    // reads a file and returns a array of employees found in the file
    public Employee[] readEmployeeFile(String loc){
        BufferedReader read = new BufferedReader(new FileReader(loc)); // creating reader
        ArrayList<Employee> people = new ArrayList<Employee>(); // arraylist to store values of employees   
    
        // read all the lines in the file
        String line = "";
        while ((line=read.readLine())!=null){
            people.add(getEmployeeFromLine(line));
        }
    
        // close the reader
        read.close();
    
        // convert arraylist to array
        Employee[] returnvalues = new Employee[people];
        for (int q = 0; q < returnvalues.length; q++){
            returnvalues[q] = people.get(q);
        }
    
        return people;
    }
    
    // reads each individual line for the file
    public Employee getEmployeeFromLine(String line){
        // format: date + job + name(contains 1 space) + monthly earnings + salesperson:annual sales | executive:stock price
    
        Employee returnvalue = null; // this is what we will return
    
        String[] values = line.split(" "); // the txt file seems to split up the values by a space so we split up the line by spaces
    
        // get all the values from the line
        Integer date = Integer.parseInt(values[0]); // date is the first value
        String position = values[1];
        String name = value[2]+values[3]; // we need both because there is a space in between the last name and first
        double monearns = Double.parseDouble(values[4]);
    
        double lastvalue;
        if (position.equals("Salesman") || position.equals("Executive")){ // only set the value if it exists otherwise we will get a index out of bounds exception
            lastvalue = values[5];
        }
    
        // you can structure the constructors how ever you would like - currently there is no value for date in the employee class
        if (position.equals("Employee")){
            returnvalue = new Employee(name, monearns, date);
        }
        else if (position.equals("Salesman")){
            returnvalue = new Salesman(name, monearns, date, lastvalue);
        }
        else if (position.equals("Executive")){
            returnvalue = new Executive(name, monearns, date, lastvalue);
        }
        return returnvalue;
    }
    
    public static void main(String[] args){
        Employee[] employees = readEmployeeFile("file.txt"); // if the file location needs to be user input I recommend just using a scanner
    
        // print out data
    }
    

    Sorry about the confusion, this would all go in the 4th class I suspect. I also thought it mentioned that you should print out the data based on date so that's why I was wondering what you were going to do with that value.