In: Computer Science
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS
TASK:
You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee.
|
Field |
Type |
Salaried |
Hourly |
|
id |
int |
Yes |
Yes |
|
name |
String |
Yes |
Yes |
|
title |
String |
Yes |
No |
|
position |
String |
No |
Yes |
|
salary |
int |
Yes |
No |
|
hourlyRate |
double |
No |
Yes |
Create a NetBeans project with the prefix Lab101.
Use the same naming convention used in the Lab100 assignment, i.e. Lab101-LastFM.
In this project create three classes named Employee, Salaried and Hourly such that:
Create a fourth class name Client that will be used to test your other classes.
In the Client class:
//Java code
public abstract class Employee {
/**
* Attributes common to all
*/
protected int id;
protected String name;
//Constructor
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
//Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
*
* @return description of employee
*/
@Override
public String toString() {
return "Employee Id: "+id+" Employee's Name: "+name;
}
@Override
public boolean equals(Object obj) {
return this== ((Employee)obj);
}
}
//==========================================
public class Salaried extends Employee {
private String title;
private int salary;
private static int countSalariedEmployee =0;
//Constructor
public Salaried(int id, String name, String title, int salary) {
super(id, name);
this.title = title;
this.salary = salary;
countSalariedEmployee++;
}
//getters and setters
public static int getCountSalariedEmployee() {
return countSalariedEmployee;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
/**
*
* @return description of salaried employee
*/
@Override
public String toString() {
return super.toString()+" Title: "+title+" salary: $"+salary;
}
}
//==================================================
public class Hourly extends Employee {
private String position;
private double hourlyRate;
private static int countHourlyEmployee=0;
//Constructor
public Hourly(int id, String name, String position, double hourlyRate) {
super(id, name);
this.position = position;
this.hourlyRate = hourlyRate;
countHourlyEmployee++;
}
//getters and setters
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
/**
*
* @return description of the hourly Employee
*/
@Override
public String toString() {
return super.toString()+ " Position: "+position+" Hourly Rate: $"+String.format("%.2f",hourlyRate);
}
}
//=======================================
import java.util.Scanner;
public class Client {
public static void main(String[] args)
{
/**
* Create an array employeeList of type Employee of length 10
*/
int length = 10;
Employee[] employeeList = new Employee[length];
/**
* Add three Salaried contacts to the array
*/
String name,position, title ;
int id, salary;
double hourlyRate;
//Scanner for keyboard entry
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i <6 ; i++) {
if(i%2==0) {
System.out.print("Enter Employee's Id: ");
id = keyboard.nextInt();
System.out.print("Enter Employee's name: ");
name = keyboard.nextLine();
keyboard.nextLine();
System.out.print("Enter Employee's title: ");
title = keyboard.nextLine();
System.out.print("Enter Employee's salary: ");
salary = keyboard.nextInt();
employeeList[i] = new Salaried(id, name, title, salary);
}
/**
* Add three Hourly employees contacts to the array
*/
else
{
System.out.print("Enter Employee's Id: ");
id = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter Employee's name: ");
name = keyboard.nextLine();
System.out.print("Enter Employee's position: ");
position = keyboard.nextLine();
System.out.print("Enter Employee's hourly rate: ");
hourlyRate = keyboard.nextDouble();
employeeList[i] = new Hourly(id,name,position,hourlyRate);
}
}
/**
* print out the contents of the array using a loop.
*/
for(Employee e: employeeList)
{
System.out.println(e);
}
/**
* Now give everyone in the employeeList a 10% raise
*/
for (int i = 0; i <employeeList.length ; i++) {
if(employeeList[i]!=null)
{
if(employeeList[i] instanceof Salaried)
{
salary = ((Salaried)employeeList[i]).getSalary();
salary +=salary*.1;
((Salaried)employeeList[i]).setSalary(salary);
}
else
{
hourlyRate = ((Hourly)employeeList[i]).getHourlyRate();
hourlyRate+=hourlyRate*.1;
((Hourly)employeeList[i]).setHourlyRate(hourlyRate);
}
}
}
/**
* print without null
*/
for(Employee e: employeeList)
{
if(e==null)
break;
System.out.println(e);
}
//Check equality
System.out.println(employeeList[0].equals(employeeList[2]));
System.out.println(employeeList[1].equals(employeeList[4]));
}
}
//Output

//If you need any help regarding this solution ............ please leave a comment ........ thanks