The first programming project involves writing a program that computes the salar
ID: 3786912 • Letter: T
Question
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. 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 constructor that allows the name and monthly salary to be initialized. A method named annualSalary that returns the salary for a whole year. A toString method that returns a string containing the name and monthly salary, appropriately labeled. The Employee class has two subclasses: Salesman and Executive. The Salesman class has an additional instance variable that contains the annual sales in whole dollars for that salesman. It should have the same three methods: A constructor that allows the name, monthly salary and annual sales to be initialized. An overridden method annualSalary that returns the salary for a whole year. The salary for a salesman consists of the base salary computed from the monthly salary plus a commission. The commission is computed as 3% of that salesman's annual sales. The maximum commission a salesman can earn is $25,000. An overridden toString method that returns a string containing the name, monthly salary and annual sales, appropriately labeled. The Executive class has an additional instance variable that reflects the current stock price. It should have the same three methods: A constructor that allows the name, monthly salary and stock price to be initialized. 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 $20,000 if the current stock price is greater than $100 and nothing otherwise. An overridden toString method that returns a string containing the name, monthly salary and stock price, appropriately labeled. Finally there should be a fourth class that contains the main method. It should read in employee information from a text file input.txt. 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: 2014 Employee Smithson,John 2000 2015 Salesman Jokey,Will 3000 100000 2014 Executive Bush,George 5000 150 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 stored in an array depending upon the year (there should be two arrays, one corresponding to 2014 and one corresponding to year 2015). You may assume that the file will contain no more than 20 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 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. Your program should compile without errors. Be sure to follow good programming style, which means making all instance variables private, naming all constants, avoiding the duplication of code and using Java code conventions. Furthermore you must select enough different kinds of employees to completely test the program.
Explanation / Answer
Please find the required solution:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//employee test class
public class EmployeeTest {
public static void main(String[] args) throws FileNotFoundException {
// declaration of two arrays for 2014, 2015
Employee[] emp2014 = new Employee[20];
Employee[] emp2015 = new Employee[20];
int index1 = 0, index2 = 0;
// read input from file
Scanner inputfile = new Scanner(new File("input.txt"));
// iterate throught each line
while (inputfile.hasNextLine()) {
String row = inputfile.nextLine();
String[] word = row.split("\s+");
// create corresponding employee objects
Employee emp = null;
if (word[1].equals("Employee")) {
emp = new Employee(word[2], Integer.parseInt(word[3].trim()));
}
if (word[1].equals("Salesman")) {
emp = new Salesman(word[2], Integer.parseInt(word[3].trim()),
Integer.parseInt(word[4].trim()));
}
if (word[1].equals("Executive")) {
emp = new Executive(word[2], Integer.parseInt(word[3].trim()),
Integer.parseInt(word[4].trim()));
}
int year = Integer.parseInt(word[0]);
if (year == 2014) {
emp2014[index1++] = emp;
}
if (year == 2015) {
emp2015[index2++] = emp;
}
}
// finally print the result
for (int i = 0; i < emp2014.length && emp2014[i] != null; i++) {
System.out.println(emp2014[i]);
}
for (int i = 0; i < emp2015.length && emp2015[i] != null; i++) {
System.out.println(emp2015[i]);
}
inputfile.close();
}
}
// Employee class
class Employee {
// instance variables declared
private String employeeName;
private int monthlySalary;
// constructor
public Employee(String employeeName, int monthlySalary) {
super();
this.employeeName = employeeName;
this.monthlySalary = monthlySalary;
}
// annual salary method
public int annualSalary() {
return 12 * monthlySalary;
}
// toString representation of class
@Override
public String toString() {
return "Employee [employeeName=" + employeeName + ", monthlySalary="
+ monthlySalary + "]";
}
// getter methods
public String getEmployeeName() {
return employeeName;
}
public int getMonthlySalary() {
return monthlySalary;
}
}
// Salesman class inherited from Employee
class Salesman extends Employee {
// instance variables declared
public int annualSales;
// constructor
public Salesman(String employeeName, int monthlySalary, int annualSales) {
super(employeeName, monthlySalary);
this.annualSales = annualSales;
}
// Overridden annualsalary method
@Override
public int annualSalary() {
int commission = annualSales * 3 / 100;
return super.annualSalary() + commission > 25000 ? 25000 : commission;
}
// toString representation of class
@Override
public String toString() {
return "Salesman [annualSales=" + annualSales + ", employeeName="
+ getEmployeeName() + ", monthlySalary=" + getMonthlySalary()
+ "]";
}
}
class Executive extends Employee {
// instance variables declared
int stockPrice;
// constructor
public Executive(String employeeName, int monthlySalary, int stockPrice) {
super(employeeName, monthlySalary);
this.stockPrice = stockPrice;
}
// Overridden annualsalary method
@Override
public int annualSalary() {
return super.annualSalary() + (stockPrice > 100 ? 20000 : 0) * 12;
}
// roString representation of class
@Override
public String toString() {
return "Executive [stockPrice=" + stockPrice + ", employeeName="
+ getEmployeeName() + ", monthlySalary=" + getMonthlySalary()
+ "]";
}
}
input.txt:
2014 Employee Smithson,John 2000
2015 Salesman Jokey,Will 3000 100000
2014 Executive Bush,George 5000 150
Sample output:
Employee [employeeName=Smithson,John, monthlySalary=2000]
Executive [stockPrice=150, employeeName=Bush,George, monthlySalary=5000]
Salesman [annualSales=100000, employeeName=Jokey,Will, monthlySalary=3000]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.