I am working on an assignment and I am stuck.
Hello,<br/><br/><br/>I am working on an assignment and I am stuck.
My subclasses and test class are not inheriting the superclass, I am also having issues with my getter methods, setter methods, and toString method not working in my Salesman subclass. I am not sure what I did b/c the variables from my superclass were being recognized and then they weren’t. Any help would be greatly appreciated:
/** * 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 protected int year; protected String employee; protected String position; protected int monthlySalary; // Constructor to set variables public Employee(int year, String employee, String position, int monthlySalary){ super(); this.year = year; this.employee = employee; this.position = position; this.monthlySalary = monthlySalary; } // Constructor to set default values public Employee(){ this.year = 2014; this.employee = “Davis, Brian”; this.position = “Sales”; this.monthlySalary = 2600; } public double annualSalary() { return this.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 void setYear(int year) {this.year = year;} public void setEmployee(String employee) {this.employee = employee;} public void setPosition(String position) {this.position = position;} public void setMonthlySalary(int monthlySalary) {this.monthlySalary = monthlySalary;} public String toString() { return “Employee name: ” + getEmployee() + “, \n” + “Work Year: ” + getYear() + “, \n” + “Position: ” + getPosition() + “, \n” + “Monthly salary: $” + getMonthlySalary() + “, \n” + “Annual salary: $” + annualSalary(); } }
/** * File: Salesman.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Subclass for employee; * displays compensation for salesman */ package Employee; public class Salesman extends Employee{ // Data fields for salesman private double annualSales; // Constructor to set variables and access methods from superclass public Salesman(int year , String employee , String position , int monthlySalary , double annualSales) { super(year , employee , position , monthlySalary); this.annualSales = annualSales; } // Constructor to set default values public Salesman() { this.annualSales = 100000; } public String annualSalary() { double commission = this.annualSales * .02; if (commission > 20000) { commission = 20000; } return annualSalary() + commission; // Getter and setter methods public double getAnnualSales() { return this.annualSales; } public void setAnnualSales(double annualSales){ this.annualSales = annualSales; } // toString() method that overrides Employee.java public String toString() { return super.toString() + “Annual sales: $” + getAnnualSales; } } }
/** * File: Salesman.java * Author: Lutz, Zechariah * Date: 2 November 2019 * Purpose: Subclass for executive; * displays compensation for executive based on stock */ package Employee; public class Executive extends Employee { // Data fields for executive private double stockPrice; private int stockBonus; // Constructor to set variables and access methods from superclass public Executive(int year, String employee, String position, int monthlySalary, double stockPrice, int stockBonus) { super(year, employee, position, monthlySalary); this.stockBonus = stockBonus; this.stockPrice = stockPrice; } // Constructor to set default values // Add else/if statement to calculate bonus pay based off of stock market public Executive() { this.stockPrice = 50; } public double annualSalary() { this.stockPrice = 50; if(stockPrice <= 50) stockBonus = 0; else this.stockBonus = 0; if(stockPrice >= 51) stockBonus = 30000; return annualSalary() + stockBonus; } // Getter and setter methods public double getStockPrice() {return this.stockPrice;} public int getStockBonus() {return this.stockBonus;} public void setStockPrice(double stockPrice) {this.stockPrice = stockPrice;} public void setStockBonus(int stockBonus) {this.stockBonus = stockBonus;} // toString() method that overrides Employee.java public String toString(){ return super.toString() + “Stock price: $” + getStockPrice(); } }
/** * 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()) { int year = 0; 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, annualSales)); } else if (position.equals(“Executive”)){ int stockBonus = input.nextInt(); employeeData2014.add(new Executive(year, employee, monthlySalary, stockPrice)); } else { employeeData2014.add(new Employee(year, employee, monthlySalary)); } } 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, annualSales)); } if (position.equals(“Executive”)){ int stockBonus = input.nextInt(); employeeData2015.add(new Executive(year, employee, monthlySalary, stockPrice)); } else { employeeData2015.add(new Employee(year, employee, monthlySalary)); } } 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(); } }
Employee Data.csv format:
Year,Position,Name,Monthly Pay,Compensation 2014,Employee,”Smith, John”,2000, 2015,Salesman,”Jones, Bill”,3000,100000 2014,Executive,”Bush, George”,5000,55