The first programming project involves writing a program that computes the salar
ID: 3681842 • 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:
1, A constructor that allows the name and monthly salary to be initialized.
2. A method named annualSalary that returns the salary for a whole year.
3. 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:
1. A constructor that allows the name, monthly salary and annual sales to be initialized.
2. 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.
3. 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:
1. A constructor that allows the name, monthly salary and stock price to be initialized.
2. 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.
3. 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.
A Word document (PDF is also acceptable) of 3 pages, font size 12, that contains the documentation for the project, which should include the following: (2.1) assumptions, main design decisions, errors handling; (2.2) UML class diagram. Do not include predefined classes. You need only to include the class name for each individual class, not the variables or methods; (2.3) a test plan that includes a set of test cases that you have created indicating what aspect of the program each one is testing; (2.4) two relevant screenshots of program execution; (2.5) lessons learned from the project.
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Test{ public static void main(String[] args) throws IOException { Employee[] employees2014 = new Employee[20]; Employee[] employees2015 = new Employee[20]; int i2014 = 0, i2015 = 0; BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader(new FileReader("C:\input.txt")); while ((sCurrentLine = br.readLine()) != null) { String data[] = sCurrentLine.split(" "); int year = Integer.parseInt(data[0]); String type = data[1]; String name = data[2]; double monthlySalary = Double.parseDouble(data[3]); Employee employee = null; if(type.equalsIgnoreCase("Employee")){ employee = new Employee(name,monthlySalary); }else if(type.equalsIgnoreCase("Salesman")){ int annualSales = Integer.parseInt(data[5]); employee = new Salesman(name,monthlySalary,annualSales); }else if(type.equalsIgnoreCase("Executive")){ int stockPrice = Integer.parseInt(data[5]); employee = new Executive(name,monthlySalary,stockPrice); } if(year==2014) employees2014[i2014++] = employee; else employees2015[i2015++] = employee; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } double total2014 = 0; for(Employee e : employees2014){ System.out.println(e); total2014 = total2014 + e.annualSalary(); System.out.println("Annual salary = "+e.annualSalary()); } System.out.println("Average annual salary of all employees for the year 2014 = "+ (total2014/i2014)); double total2015 = 0; for(Employee e : employees2015){ System.out.println(e); total2015 = total2015 + e.annualSalary(); System.out.println("Annual salary = "+e.annualSalary()); } System.out.println("Average annual salary of all employees for the year 2015 = "+ (total2015/i2015)); } } class Employee{ String name; double monthlySalary; public Employee(String name, double monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public double annualSalary(){ return 12*monthlySalary; } @Override public String toString() { return "Employee{" + "name='" + name + ''' + ", monthlySalary=" + monthlySalary + '}'; } } class Salesman extends Employee{ int annualSales; public Salesman(String name, double monthlySalary, int annualSales) { super(name, monthlySalary); this.annualSales = annualSales; } public double annualSalary() { double commision = 0.03 * annualSales > 25000 ? 25000 : 0.03 * annualSales; return super.annualSalary() + commision; } @Override public String toString() { return "Salesman{" + "name='" + name + ''' + ", monthlySalary=" + monthlySalary + ", annualSales=" + annualSales+ '}'; } } class Executive extends Employee{ int stockPrice; public Executive(String name, double monthlySalary, int stockPrice) { super(name, monthlySalary); this.stockPrice = stockPrice; } public double annualSalary() { double bonus = stockPrice > 100 ? 20000 : 0; return super.annualSalary() + bonus; } @Override public String toString() { return "Executive{" + "name='" + name + ''' + ", monthlySalary=" + monthlySalary + ", stockPrice=" + stockPrice+ '}'; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.