Hello. I am creating a program that analyses and prints out different
Hello. I am creating a program that analyses and prints out different
salary data for employees based on their position. For some reason my test class is not accessing the methods from the superclass and I’m not sure why. Is there a method I need to add? I just assumed since I have all my methods the same that it would access them. The subclasses are accessing the super and the test class is accessing the subclasses, which is making it more confusing. Thanks in advance. My code is below:
/** * File: Employee.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Employee class that displays monthly and annual salary */ public class Employee { // Set data fields private int year; private String employee; private String position; private int monthlySalary; private double annualSalary; // Constructor to set variables public Employee(int year, String employee, String position, int monthlySalary, double annualSalary){ super(); this.year = year; this.employee = employee; this.position = position; this.monthlySalary = monthlySalary; this.annualSalary = annualSalary; } // Constructor to set default values public Employee(){ this.year = 2014; this.employee = “Davis, Brian”; this.position = “Sales”; this.monthlySalary = 2600; this.annualSalary = monthlySalary*12; } // Getter and setter methods public int getYear() {return this.year;} public String getEmployee() {return this.employee;} public String getPosition() {return this.position;} public int getMonthlySalary() {return this.monthlySalary;} public double getAnnualSalary() {return this.annualSalary;} public void setYear() {this.year = year;} public void setEmployee() {this.employee = employee;} public void setPosition() {this.position = position;} public void setMonthlySalary() {this.monthlySalary = monthlySalary;} public void setAnnualSalary() {this.annualSalary = annualSalary;} public String toString() { return “Employee name: ” + employee + “, \n” + “Work Year: ” + year + “, \n” + “Position: ” + position + “, \n” + “Monthly salary: $” + monthlySalary + “, \n” + “Annual salary: $” + annualSalary; } }
/** * File: Salesman.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Subclass for employee; * displays compensation for salesman */ public class Salesman extends Employee{ // Data fields for salesman private int annualSales; private double annualCommission; private double annualSalary; // Constructor to set variables and access methods from superclass public Salesman(int year, String employee, String position, int monthlySalary, double annualSalary, int annualSales, double annualCommission) { super(year, employee, position, monthlySalary, annualSalary); this.annualSales = annualSales; this.annualCommission = annualCommission; this.annualSalary = annualSalary; } // Constructor to set default values public Salesman() { this.annualSales = 100000; this.annualCommission = (annualSales * .02); this.annualSalary = (annualCommission + annualSalary); } // Getter and setter methods public int getAnnualSales() {return this.annualSales;} public double getAnnualCommission() {return this.annualCommission;} public double getAnnualSalary() {return this.annualSalary;} public void setAnnualSales() {this.annualSales = annualSales;} public void setAnnualCommission() {this.annualCommission = annualCommission;} public void setAnnualSalary() {this.annualSalary = annualSalary;} // toString() method that overrides Employee.java @Override public String toString(){ return “Employee name: ” + getEmployee() + “, \n” + “Monthly salary: $” + getMonthlySalary() + “, \n” + “Annual sales: $” + annualSales; } }
/** * File: Salesman.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Subclass for executive; * displays compensation for executive based on stock */ public class Executive extends Employee{ // Data fields for executive private double stockPrice; private int stockBonus; private double annualSalary; // Constructor to set variables and access methods from superclass public Executive(int year, String employee, String position, int monthlySalary, int annualSalary, double stockPrice, int stockBonus) { super(year, employee, position, monthlySalary, annualSalary); this.stockBonus = stockBonus; this.stockPrice = stockPrice; this.annualSalary = annualSalary; } // Constructor to set default values // Add else/if statement to calculate bonus pay based off of stock market public Executive() { this.stockPrice = 50; if(stockPrice <= 50) stockBonus = 0; else this.stockBonus = 0; if(stockPrice >= 51) stockBonus = 30000; this.annualSalary = (stockBonus + annualSalary); } // Getter and setter methods public double getStockPrice() {return this.stockPrice;} public int getStockBonus() {return this.stockBonus;} public double getAnnualSalary() {return this.annualSalary;} public void setStockPrice() {this.stockPrice = stockPrice;} public void setStockBonus() {this.stockBonus = stockBonus;} public void setAnnualSalary() {this.annualSalary = annualSalary;} // toString() method that overrides Employee.java @Override public String toString(){ return “Employee name: ” + getEmployee()+ “, \n” + “Monthly salary: $” + getMonthlySalary() + “, \n” + “Stock price: $” + stockPrice; } }
/** * File: EmployeeSalary.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Contains main method for employee data; * reads text file and displays compensation for various employees */ package Employee; import java.util.ArrayList; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; import java.io.File; import java.io.IOException; public class EmployeeSalary { private ArrayList<Employee> employeeData2014 = new ArrayList<>(); private ArrayList<Employee> employeeData2015 = new ArrayList<>(); public void EmployeeData() { try{ Scanner input = new Scanner(new File (“Employee Data.csv”)); while (input.hasNext()) { if (year == 2014){ String employee = input.next(); String position = input.next(); double monthlySalary = input.nextDouble(); if (position.equals(“Salesman”)){ double annualCommission = input.nextDouble(); employeeData2014.add(new Salesman(year, employee, monthlySalary, annualSalary, annualCommission)); } else if (position.equals(“Executive”)){ int stockBonus = input.nextInt(); employeeData2014.add(new Executive(year, employee, monthlySalary, annualSalary, stockBonus)); } else { employeeData2014.add(new Employee(year, employee, monthlySalary, annualSalary)); } } if (year == 2015){ String employee = input.next(); String position = input.next(); double monthlySalary = input.nextDouble(); if (position.equals(“Salesman”)){ double annualCommission = input.nextDouble(); employeeData2015.add(new Salesman(year, employee, monthlySalary, annualSalary, annualCommission)); } if (position.equals(“Executive”)){ int stockBonus = input.nextInt(); employeeData2015.add(new Executive(year, employee, monthlySalary, annualSalary, stockBonus)); } else { employeeData2015.add(new Employee(year, employee, monthlySalary, annualSalary)); } } input.nextLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NullPointerException e){ System.out.println(“Error: Check the input file.”); } catch (IOException e){ System.out.println(“File not found.”); } catch (NoSuchElementException e) { System.out.println(“The last line of the document is not blank”); } } public void showEmployeeData(){ double averageSalary2014 = 0; double averageSalary2015 = 0; System.out.println(“2014 Data: “); for (Employee x: employeeData2014){ System.out.println(x.toString()); averageSalary2014 += x.annualSalary(); } averageSalary2014 = averageSalary2014 / employeeData2014.size(); System.out.println(“The average salary in 2014 was: ” + averageSalary2014); System.out.println(“2015 Data: “); for (Employee x: employeeData2015){ System.out.println(x.toString()); averageSalary2015 += x.annualSalary(); } averageSalary2015 = averageSalary2015 / employeeData2015.size(); System.out.println(“The average salary in 2015 was: ” + averageSalary2015); } public static void main (String[] args){ EmployeeSalary list = new EmployeeSalary(); list.EmployeeData(); list.showEmployeeData(); } }