Your company has asked you to develop a report detailing pay

Your company has asked you to develop a report detailing payroll information with the following format:

Acme Corporation

Number of Employees:         3

Average Salary:          53,502.32

Annual Total:           160,506.95

Name             Rank      Salary

Gator, Allie       A2    48,800.00

Mander, Sally      A1    62,123.89

Baer, Teddy        B4    53,006.95

Develop an Employee class that contains the attributes first name, last name, rank, and salary. The class should have the following methods:

parameterized constructor

getters necessary for printing the report

formatName() method that returns a String with the name in the format last, first

Employee information is stored in a sequential text file named employees.txt. The file has the format:

employees.txt file:   text file with the following fields separated by spaces:

(TEXT FILE for program: employees.txt )

Fields

Description

First Name

string

Last Name

string

Rank

string

Salary

double

Note: Names will not contain spaces.

Write a program named Prog5 that reads employee information from the file and places a corresponding Employee object into an array. After the file has been read, print the report shown above using proper formatting.

Challenge

For those who would like a challenge to increase the difficulty of the assignment, implement the following static methods to be used to print the average salary and annual total, both of which receive an array of doubles and the number of elements in the array:

averageSalary( Employee arr[], int num )        returns the average salary of employees in the array

totalSalary( Employee arr[], int num )             returns the sum of all salaries of employees in the array

Fields

Description

First Name

string

Last Name

string

Rank

string

Salary

double

Solution

import java.io.File;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Employee {

   String firstName, lastName, rank;
   double salary;

   /**
   * @param firstName
   * @param lastName
   * @param rank
   * @param salary
   */
   public Employee(String firstName, String lastName, String rank,
           double salary) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.rank = rank;
       this.salary = salary;
   }

   /**
   * @return the firstName
   */
   public String getFirstName() {
       return firstName;
   }

   /**
   * @param firstName
   * the firstName to set
   */
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   /**
   * @return the lastName
   */
   public String getLastName() {
       return lastName;
   }

   /**
   * @param lastName
   * the lastName to set
   */
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   /**
   * @return the rank
   */
   public String getRank() {
       return rank;
   }

   /**
   * @param rank
   * the rank to set
   */
   public void setRank(String rank) {
       this.rank = rank;
   }

   /**
   * @return the salary
   */
   public double getSalary() {
       return salary;
   }

   /**
   * @param salary
   * the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }

   public String formatName() {
       return lastName + \", \" + firstName;

   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       DecimalFormat format = new DecimalFormat(\"#.00\");
       return formatName() + \"\\t\" + rank + \"\\t\" + format.format(salary);
   }

   public static double averageSalary(Employee arr[], int num) {
       return totalSalary(arr, num) / num;

   }

   public static double totalSalary(Employee arr[], int num) {
       double sumSal = 0;
       for (int i = 0; i < num; i++) {
           sumSal += arr[i].getSalary();
       }
       return sumSal;
   }

   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File(\"employees.txt\"));
           Employee[] employees = new Employee[100];
           int noOfEmployees = 0;
           while (scanner.hasNext()) {
               Employee employee = new Employee(scanner.next(),
                       scanner.next(), scanner.next(), scanner.nextDouble());
               employees[noOfEmployees] = employee;
               noOfEmployees++;
           }

           System.out.println(\"Acme Corporation\");
           System.out.println(\"Average Salary: \"
                   + averageSalary(employees, noOfEmployees));
           System.out.println(\"Annual Total: \"
                   + totalSalary(employees, noOfEmployees));
           System.out
                   .println(\"Number of Employees: \" + noOfEmployees);
           System.out.println(\"Name\\t\\tRank\\tSalary\");
           for (int i = 0; i < noOfEmployees; i++) {
               System.out.println(employees[i]);
           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

employees.txt

Teddy Baer B4 53000
Allie Gator A2 48000
Sally Mander A1 62123.89
Jed Night B2 42500
Sebastian Celery A3 65000
Amy Aspargus A1 100050
Henry Huckleberry B1 41234.5
Ryan Rutabaga B2 32987.2
Tom Tomato A1 83768.95

OUTPUT:

Acme Corporation
Average Salary: 58740.50444444445
Annual Total: 528664.54
Number of Employees: 9
Name       Rank   Salary
Baer, Teddy   B4   53000.00
Gator, Allie   A2   48000.00
Mander, Sally   A1   62123.89
Night, Jed   B2   42500.00
Celery, Sebastian   A3   65000.00
Aspargus, Amy   A1   100050.00
Huckleberry, Henry   B1   41234.50
Rutabaga, Ryan   B2   32987.20
Tomato, Tom   A1   83768.95

Your company has asked you to develop a report detailing payroll information with the following format: Acme Corporation Number of Employees: 3 Average Salary:
Your company has asked you to develop a report detailing payroll information with the following format: Acme Corporation Number of Employees: 3 Average Salary:
Your company has asked you to develop a report detailing payroll information with the following format: Acme Corporation Number of Employees: 3 Average Salary:
Your company has asked you to develop a report detailing payroll information with the following format: Acme Corporation Number of Employees: 3 Average Salary:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site