The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee’s name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be initialized. b. A method named annualSalary that returns the salary for a whole year. c. A toString method tha t returns a string containing the name and monthly salary, appropriately labeled. 2. The Employee class has two subclasses. The first is Salesman. It has an additional instance variable that contains the annual sales in whole dollars for that salesman. It sho uld have the same three methods: a. A constructor that allows the name, monthly salary and annual sales to be initialized. b. An overridden method annualSalary that returns the salary for a whole year. The salary for a salesman consists of the base salary comput ed from the monthly salary plus a commission. The commission is computed as 2% of that salesman’s annual sales. The maximum commission a salesman can earn is $20,000. c. An overridden toString method that returns a string containing the name, monthly salary a nd annual sales, appropriately labeled. 3. The second subclass is Executive. It has an additional instance variable that reflects the current stock price. It should have the same three methods: a. A constructor that allows the name, monthly salary and stock pric e to be initialized. b. An overridden method annualSalary that returns the salary for a whole year. The salary for an executive consists of the base salary computed from the monthly salary plus a bonus. The bonus is $30,000 if the current stock price is great er than $50 and nothing otherwise. c. An overridden toString method that returns a string containing the name, monthly salary and stock price, appropriately labeled. 4. Finally there should be a fourth class that contains the main method. It should read in emplo yee 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: 2 014 Employee Smith,John 2000 2015 Salesman Jones,Bill 3000 100000 2014 Ex ecutive Bush,George 5000 55 The year is the first data element on the line. The file will contain employee information for only two years: 2014 and 2015. Next is the type of the employee followed by the employee name and the monthly salary. For salesmen, the final value is their annual sales and for executives the stock price. As the employees are read in, Employee objects of the appropriate type should be created and they should be stored in one of two arrays depending upon the year. You may assume that t he file will contain no more than ten employee records for each year and that the data in the file will be formatted correctly. 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 r eport should contain all original data supplied for each employee together with 2 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. The google r ecommended Java style guide, provided as link in the week 2 content, should be used to format and document your code. Specifically, the following style guide attributes should be addressed: Header comments include filename, author, date and brief purpose o f the program. In – line comments used to describe major functionality of the code. Meaningful variable names and prompts applied. Class names are written in UpperCamelCase. Variable names are written in lowerCamelCase. Constant names are in written in All C apitals. Braces use K&R style . In addition the following design constraints should be followed: Declare all instance variables private Avoid the duplication of code Test cases should be supplied in the form of table with columns indicating the input values , expected output, actual output and if the test case passed or failed. This table should contain 4 columns with appropriate labels and a row for each test case. Note that the actual output should be the actual results you receive when running your progra m and applying the input for the test record. Be sure to select enough different kinds of employees to completely test the program .
Expert Answer
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
//These fields are private and represent seperate arrays for the seperate
//years of employee info.
private ArrayList<Employee> arrayFor2014 = new ArrayList<>();
private ArrayList<Employee> arrayFor2015 = new ArrayList<>();
//This is a private DecimalFormat object to format method calls.
private static DecimalFormat df = new DecimalFormat(“$,000.00”);
public void create() {
try {
Scanner input = new Scanner(new File(“data.txt”));
//Loop will terminate when method has no more data to read.
while (input.hasNext()) {
int year = input.nextInt();
//Checks if first element is 2014.
if (year == 2014) {
String type = input.next();
String name = input.next();
double monthlyPay = input.nextDouble();
//Checks second element for Salesman value and takes in data if so.
if (type.equals(“Salesman”)) {
double finalElement = input.nextDouble();
arrayFor2014.add(new Salesman(year, name, monthlyPay, finalElement));
//Checks second element for Executive value and takes in data if so.
}
else if (type.equals(“Executive”)) {
double finalElement = input.nextDouble();
arrayFor2014.add(new Executive(year, name, monthlyPay, finalElement));
//Element will be Employee because failed test otherwise.
}
else {
arrayFor2014.add(new Employee(year, name, monthlyPay));
}
}
//Checks if first element is 2015.
else if (year == 2015) {
String type = input.next();
String name = input.next();
double monthlyPay = input.nextDouble();
//Checks second element for Salesman value and takes in data if so.
if (type.equals(“Salesman”)) {
double finalElement = input.nextDouble();
arrayFor2015.add(new Salesman(year, name, monthlyPay, finalElement));
}
//Tests for Executive in the second element.
else if (type.equals(“Executive”)) {
double finalElement = input.nextDouble();
arrayFor2015.add(new Executive(year, name, monthlyPay, finalElement));
}
//Element will be Employee because failed test otherwise.
else {
arrayFor2015.add(new Employee(year, name, monthlyPay));
}
}
//Scanner consumes blank line and terminates.
input.nextLine();
}
}
catch (IOException e) {
System.out.println(“File not found.”);
e.printStackTrace();
}
catch (NullPointerException e) {
System.out.println(“A null pointer was found, please check the input file.”);
e.printStackTrace();
}
catch (NoSuchElementException e) {
System.out.println(“The last line of the document is not blank.”);
e.printStackTrace();
}
}
public void displayOutput() {
//Temporary variables for annual salary average.
double avg2014 = 0;
double avg2015 = 0;
//Refer to array, calculate, and display employee’s annual salary for 2014.
System.out.println(“Output for 2014:”);
for (Employee employee : arrayFor2014) {
System.out.println(employee.toString());
avg2014 += employee.annualSalary();
}
//Total average salary for 2014 must be divided by total number of employees.
avg2014 = avg2014 / arrayFor2014.size();
System.out.println(“n” + “Average Employee’s Salary for 2014: ” + df.format(avg2014));
//Refer to array, calculate, and display employee’s annual salary for 2015.
System.out.println(“n” + “Output for 2015:”);
for (Employee em : arrayFor2015) {
System.out.println(em.toString());
avg2015 += em.annualSalary();
}
//Total average salary for 2014 must be divided by total number of employees.
avg2015 = avg2015 / arrayFor2015.size();
System.out.println(“n” + “Average Employee’s Salary for 2015: ” + df.format(avg2015));
}
public static void main(String[] args) {
Main list = new Main();
list.create();
list.displayOutput();
}
}
Employee.java
import java.text.DecimalFormat;
public class Employee {
private String name;
private double monthlyPay;
private int year;
static DecimalFormat df = new DecimalFormat(“$,000.00”);
//A constructor that allows the name and monthly salary to be initialized.
public Employee() {
}
public Employee(int year, String name, double monthlyPay) {
this.year = year;
this.name = name;
this.monthlyPay = monthlyPay;
}
//A method named annualSalary that returns the salary for a whole year.
public double annualSalary() {
return this.monthlyPay * 12;
}
//A toString method that returns a string containing the name and monthly
//salary, appropriately labeled.
public String toString() {
return “n” + “Employee’s Name: tt” + this.name +
“n” + “Salary for the Year: tt” + df.format(this.annualSalary());
}
//Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMonthlyPay() {
return monthlyPay;
}
public void setMonthlyPay(double monthlySalary) {
this.monthlyPay = monthlySalary;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
Salesman.java
public class Salesman extends Employee {
private double yearlySales;
//A constructor that allows the name and monthly salary to be initialized.
public Salesman() {
}
public Salesman(int year, String name, double monthlyPay, double yearlySales) {
super(year, name, monthlyPay);
this.yearlySales = yearlySales;
}
public double annualSalary() {
double commission = this.yearlySales * .02;
if (commission > 20000) {
commission = 20000;
}
return super.annualSalary() + commission;
}
public String toString() {
return super.toString() + “n” + “Sales for The Year: tt” +
df.format(this.yearlySales);
}
//Getter and Setter methods
public double getYearlySales() {
return yearlySales;
}
public void setAnnualSales(double annualSales) {
this.yearlySales = annualSales;
}
public String getName() {
return super.getName();
}
public void setName(String name) {
super.setName(name);
}
public double getMonthlySalary() {
return super.getMonthlyPay();
}
public void setMonthlySalary(double monthlyPay) {
super.setMonthlyPay(monthlyPay);
}
public int getYear() {
return super.getYear();
}
public void setYear(int year) {
super.setYear(year);
}
}
Executive.java
public class Executive extends Employee{
private double stockValue;
//A constructor that allows the name, monthly salary and annual sales to be initialized.
public Executive() {
}
public Executive(int year, String name, double monthlyPay, double stockValue) {
super(year, name, monthlyPay);
this.stockValue = stockValue;
}
//A method named annualSalary that returns the salary for a whole year.
public double annualSalary() {
double bonus = 0;
//The bonus is $30,000 if the current stock price is greater than $50 and
//nothing otherwise.
if (this.stockValue > 50) {
bonus = 30000;
}
return super.annualSalary() + bonus;
}
//A toString method that returns a string containing the name and monthly
//salary, appropriately labeled.
public String toString() {
return super.toString() + “nStock Price: ttt” +
df.format(this.stockValue);
}
//Getter and Setter methods
public double getStockPrice() {
return stockValue;
}
public void setStockPrice(double stockPrice) {
this.stockValue = stockPrice;
}
public String getName() {
return super.getName();
}
public void setName(String name) {
super.setName(name);
}
public double getMonthlyPay() {
return super.getMonthlyPay();
}
public void setMonthlyPay(double monthlyPay) {
super.setMonthlyPay(monthlyPay);
}
public int getYear() {
return super.getYear();
}
public void setYear(int year) {
super.setYear(year);
}
}
data.txt
2015 Employee Doe,John 1800
2015 Salesman Kramer,Scott 3000 90000
2015 Executive Kilpatrick,Jane 6000 165
2015 Employee Patterson,Olivia 3800
2014 Salesman Washington,Michael 3600 205000
2015 Employee McEvers,Lela 1100
2014 Executive Real,Felicia 7600 142
2015 Employee Davis,Carol 9200
2014 Employee Walton,Amy 2400
2014 Salesman Brandy,James 5100 990000
2014 Salesman Amantha,Ben 27000 300000
2014 Executive Jefferson,Leonard 2700 149
2014 Executive Clinton,Max 20000 151
2015 Employee Jameson,Don 6400
2014 Executive Tan,Jill 4500 175
2015 Salesman Baxter,Reed 51800 5000
2014 Employee Doe,Jane 1100
2014 Executive Reeves,Damien 5200 340
2014 Salesman Huntington,Dexter 9900 21000
2015 Employee Montgomery,Madeline 7900