The first programming project involves writing a program tha

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 that 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 should 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 computed 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 and 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 price 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 greater 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 employee 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: 2014 Employee Smith,John 2000 2015 Salesman Jones,Bill 3000 100000 2014 Executive 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 the 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 report 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 recommended 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 of 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 Capitals. 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 program and applying the input for the test record. Be sure to select enough different kinds of employees to completely test the program. Submission requirements Deliverables include all Java files (.java) and a single word (or PDF) document. The Java files should be named appropriately for your applications. The word (or PDF) document should include screen captures showing the successful compiling and running of each of the test cases. Each screen capture should be properly labeled clearly indicated what the screen capture represents. The test cases table should be included in your word or PDF document and properly labeled as well. Submit your files to the Project 1 assignment area no later than the due date listed in your LEO classroom. You should include your name and P1 in your word (or PDF) file submitted (e.g. firstnamelastnameP1.docx or firstnamelastnameP1.pdf)

Solution

Files:

Employee.java

/**
* Employee class to store employee name and monthly salary
* @author Anonymous
*
*/
public class Employee {
  
   private String empName;
   private int monthlySalary;
  
   public Employee(String empName,int monthlySalary){
       this.empName = empName;
       this.monthlySalary = monthlySalary;
   }
  
   /**
   * Method to calculate annual salary of employee
   * @return
   */
   public int annualSalary(){
       return this.monthlySalary*12;
   }
  
   /**
   * Overridden toString() to print employee name and monthly salary
   */
   @Override
   public String toString(){
       return \"Employee Name: \"+this.empName+\" Monthly Salary: \"+this.monthlySalary;
   }

}


Executive.java

/**
* Executive class to store Employee and current stock price
* @author Anonymous
*
*/
public class Executive extends Employee{
  
   private int stockPrice;

   public Executive(String empName, int monthlySalary, int stockPrice) {
       super(empName, monthlySalary);
       this.stockPrice = stockPrice;
   }
  
   /**
   * Overridden Method to calculate annual salary of Executive employee
   */
   @Override
   public int annualSalary(){
       int bonus=0;
       //If current stock price > 50, add $30000 to bonus
       if(stockPrice>50){
           bonus = 30000;
       }
       return super.annualSalary()+bonus;
   }
  
   /**
   * Overridden method to print Executive details
   */
   @Override
   public String toString(){
       return super.toString()+\" Current Stock Price: \"+this.stockPrice;
   }
}


Salesman.java

/**
* Salesman class to store Employee details and his annual sales
* @author Anonymous
*
*/
public class Salesman extends Employee{
  
   private int annualSales;

   public Salesman(String empName, int monthlySalary,int annualSales) {
       super(empName, monthlySalary);
       this.annualSales = annualSales;      
   }
  
   /**
   * Overridden Method to calculate annual salary of Salesman Employee
   */
   @Override
   public int annualSalary(){      
       int annualSalary = super.annualSalary();
       //Commission = 2% of annual sales
       int commission = this.annualSales*2/100;
       //Max commission a salesman can earn is $20000
       if(commission>20000){
           commission = 20000;
       }
       return annualSalary+commission;
   }
  
   /**
   * Overridden method to print Salesman details
   */
   @Override
   public String toString(){
       return super.toString()+\" Annual Sales: \"+annualSales;
   }
  
}


Main.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
* Main method to read employees from text file and print record in console
* @author Anonymous
*
*/
public class Main {
  
   public static void main(String args[]) throws FileNotFoundException{
      
       //Employee arrays for years 2014 and 2015
       Employee[] list2014 = new Employee[10];
       Employee[] list2015 = new Employee[10];
       //To store size of arrays
       int size2014=0,size2015=0;
      
       //Read file
       FileInputStream fis = new FileInputStream(\"<<Your Text file path goes here>>\");
       @SuppressWarnings(\"resource\")
       Scanner sc = new Scanner(fis);
      
       while(sc.hasNextLine()){
           //Read line
           String line = sc.nextLine();
           //Split line on basis of whitespaces
           String[] splitLine = line.split(\" \");
           Employee emp;
          
           Integer year = Integer.parseInt(splitLine[0]);
           if(year == 2014){
               emp= createEmployee(splitLine);
               //Check if emp is not null
               if(emp!=null){
                   list2014[size2014++]=emp;
               }
           }
           else if(year == 2015){
               emp= createEmployee(splitLine);
               //Check if emp is not null
                   if(emp!=null){
                       list2015[size2015++]=emp;
                   }
           }
       }
      
       //Printing record for year 2014
       System.out.println(\"Employee Record for Year 2014\");
      
       //Variable to store total salary of employees for 2014
       int totSalary2014 = 0;
       for(int k=0;k<size2014;k++){
           System.out.print(list2014[k].toString());
           totSalary2014+=list2014[k].annualSalary();
           System.out.println(\" Annual Salary: \"+list2014[k].annualSalary());
       }
      
       //Printing record for year 2015
       System.out.println(\"\ Employee Record for Year 2015\");
      
       //Variable to store total salary of employees for 2015
       int totSalary2015 = 0;
       for(int k=0;k<size2015;k++){
           System.out.print(list2015[k].toString());
           totSalary2015+=list2015[k].annualSalary();
           System.out.println(\" Annual Salary: \"+list2015[k].annualSalary());
       }
      
      
       //Printing average salary for 2014 and 2015
       System.out.println(\"\ Average salary for Year 2014: \"+totSalary2014/size2014);
       System.out.println(\"Average salary for Year 2015: \"+totSalary2015/size2015);      
      
   }

   /**
   * Private method to create Employee object
   * @param splitLine
   * @return object of Employee class
   */
   private static Employee createEmployee(String[] splitLine) {
      
       Employee emp = null;
      
       if(\"Executive\".equals(splitLine[1])){
           emp = new Executive(splitLine[2],Integer.parseInt(splitLine[3]),Integer.parseInt(splitLine[4]));          
       }
       else if(\"Salesman\".equals(splitLine[1])){
           emp = new Salesman(splitLine[2],Integer.parseInt(splitLine[3]),Integer.parseInt(splitLine[4]));
       }
       else{
           emp = new Employee(splitLine[2], Integer.parseInt(splitLine[3]));
       }
       return emp;
   }

}


EmployeeTest.java


import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* Employee Test Class
* @author Anonymous
*
*/
public class EmployeeTest {
  
   private Employee emp = null;
  
   @Before
   public void init(){
       emp = new Employee(\"Test\", 20);
   }
  
   /**
   * Test object creation
   */
   @Test
   public void testObjCreateSuccess(){
       String empName = \"abc\";
       int monthlySalary = 12;
       Employee emp1 = null;
       emp1 = new Employee(empName,monthlySalary);
       Assert.assertNotNull(emp1);
   }
  
   /**
   * Test annual salary computation
   */
   @Test
   public void testAnnualSalary(){
       Assert.assertEquals(240, emp.annualSalary());
   }
  
   /**
   * Test Employee
   */  
   @Test
   public void testToString(){
       Assert.assertEquals(\"Employee Name: Test Monthly Salary: 20\", emp.toString());
   }

}


EmployeeTest.java


import org.junit.Assert;
import org.junit.Test;

/**
* Tests for Executive Class
* @author Anonymous
*
*/
public class ExecutiveTest {
  
   private Employee emp;
  
   /**
   * Test successful object creation
   */
   @Test
   public void testObjCreateSuccess(){
       emp = new Executive(\"Test\", 20, 60);
       Assert.assertNotNull(emp);
   }
  
   /**
   * Test annual salary computation when stock price <=50
   */
   @Test
   public void testAnnualSalaryWhenStockPriceLessThan50(){
       emp = new Executive(\"Test\", 20, 30);
       Assert.assertEquals(240, emp.annualSalary());
   }
  
   /**
   * Test annual salary computation when stock price >50
   */
   @Test
   public void testAnnualSalaryWhenStockPriceGreaterThan50(){
       emp = new Executive(\"Test\", 20, 60);
       Assert.assertEquals(30240, emp.annualSalary());
   }
  
   /**
   * Test Executive employee details
   */
   @Test
   public void testToString(){
       emp = new Executive(\"Test\", 20, 60);
       Assert.assertEquals(\"Employee Name: Test Monthly Salary: 20 Current Stock Price: 60\", emp.toString());
   }

}


SalesmanTest.java


import org.junit.Assert;
import org.junit.Test;

/**
* Tests for Salesman class
* @author Anonymous
*
*/
public class SalesmanTest {
  
   private Employee emp;
  
   /**
   * Test successful object creation
   */
   @Test
   public void testObjCreateSuccess(){
       emp = new Salesman(\"Test\", 20, 100);
       Assert.assertNotNull(emp);
   }
  
   /**
   * Test annual salary computation
   */
   @Test
   public void testAnnualSalary(){
       emp = new Salesman(\"Test\", 20, 100);
       Assert.assertEquals(242, emp.annualSalary());
   }
  
   /**
   * Test annual salary computation when commission > 20000
   */
   @Test
   public void testAnnualSalaryWhenCommissionIsGreaterThan20000(){
       emp = new Salesman(\"Test\", 20, 10000000);
       Assert.assertEquals(20240, emp.annualSalary());
   }
  
   /**
   * Test Salesman employee details
   */
   @Test
   public void testToString(){
       emp = new Salesman(\"Test\", 20, 100);
       Assert.assertEquals(\"Employee Name: Test Monthly Salary: 20 Annual Sales: 100\", emp.toString());
   }

}

Sample test file used:

2014 Employee John,Smith 2000
2015 Employee John,Smith 2000
2014 Salesman John,Smit 2014 10000
2015 Salesman John,Smit 2015 10000
2014 Employee John,Smi 2014
2015 Executive John,Smi 2015 30
2014 Employee John,Sm 2014
2015 Salesman John,Sm 2015 1000
2014 Executive John,S 2014 50
2015 Executive John,S 2015 51
2014 Salesman John, 2014 100
2015 Executive John, 2015 55
2014 Employee Joh 2014
2015 Salesman Joh 2015 10000

The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site