****Program using Java**** write main program to perform following tasks. (Staff
ID: 3668678 • Letter: #
Question
****Program using Java****
write main program to perform following tasks. (Staff, StaffMemeber, Executive, Hourly and Volunteer class files are provided).
A. Print their employee information with their employment type and their pay of all staff members from Staff class.
B. Accept name from keyboard.
C. If the name was found, delete the record from employee list. If not, display “not found” perform (e) step. Executive record is not allowed to delete.
D. Print the confirmation after the deletion.
E. Accept the another name until terminated by user or the list is empty.
F. Accept name from keyboard to allow user to modify the staff member Address if it match the record on the list.
G. Continue (a) until terminate by user.
H. Print the final list with their object type and pay amount.
*******All the required classes.*****
//********************************************************************
// Employee.java
//
// Represents a general paid employee.
public class Employee extends StaffMember
{
protected String socialSecurityNumber;
protected double payRate;
//-----------------------------------------------------------------
// Constructor: Sets up this employee with the specified
// information.
//-----------------------------------------------------------------
public Employee (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
//-----------------------------------------------------------------
// Returns information about an employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
result += " Social Security Number: " + socialSecurityNumber;
return result;
}
//-----------------------------------------------------------------
// Returns the pay rate for this employee.
//-----------------------------------------------------------------
public double pay()
{
return payRate;
}
}
//********************************************************************
// Executive.java
//
// Represents an executive staff member, who can earn a bonus.
public class Executive extends Employee
{
private double bonus;
//-----------------------------------------------------------------
// Constructor: Sets up this executive with the specified
// information.
//-----------------------------------------------------------------
public Executive (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
bonus = 0; // bonus has yet to be awarded
}
//-----------------------------------------------------------------
// Awards the specified bonus to this executive.
//-----------------------------------------------------------------
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
//-----------------------------------------------------------------
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
//-----------------------------------------------------------------
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//********************************************************************
// Hourly.java
//
// Represents an employee that gets paid by the hour.
public class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// Constructor: Sets up this hourly employee using the specified
// information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
result += " Current hours: " + hoursWorked;
return result;
}
}
//********************************************************************
// Staff.java
//
// Represents the personnel staff of a particular business.
public class Staff
{
private StaffMember[] staffList;
//-----------------------------------------------------------------
// Constructor: Sets up the list of staff members.
//-----------------------------------------------------------------
public Staff ()
{
staffList = new StaffMember[6];
staffList[0] = new Executive ("Sam", "123 Main Line",
"555-0469", "123-45-6789", 2423.07);
staffList[1] = new Employee ("Carla", "456 Off Line",
"555-0101", "987-65-4321", 1246.15);
staffList[2] = new Employee ("Woody", "789 Off Rocker",
"555-0000", "010-20-3040", 1169.23);
staffList[3] = new Hourly ("Diane", "678 Fifth Ave.",
"555-0690", "958-47-3625", 10.55);
staffList[4] = new Hourly ("Norm", "987 Suds Blvd.",
"555-8374","456-78-9000", 11.2);
staffList[5] = new Volunteer ("Cliff", "321 Duds Lane",
"555-7282");
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40);
((Hourly)staffList[4]).addHours (30);
}
//-----------------------------------------------------------------
// Pays all staff members.
//-----------------------------------------------------------------
public void payday ()
{
double amount;
for (int count=0; count < staffList.length; count++)
{
System.out.println (staffList[count]);
amount = staffList[count].pay(); // polymorphic
if (amount == 0.0)
System.out.println ("Thanks!");
else
System.out.println ("Paid: " + amount);
System.out.println ("-----------------------------------");
}
}
public StaffMember getStaff(int i)
{
StaffMember st = staffList[i];
return st;
}
public int getSize()
{
int sz = staffList.length;
return sz;
}
//********************************************************************
// StaffMember.java
//
// Represents a generic staff member.
abstract public class StaffMember
{
protected String name;
protected String address;
protected String phone;
//-----------------------------------------------------------------
// Constructor: Sets up this staff member using the specified
// information.
//-----------------------------------------------------------------
public StaffMember (String eName, String eAddress, String ePhone)
{
name = eName;
address = eAddress;
phone = ePhone;
}
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
public String toString()
{
String result = "Name: " + name + " ";
result += "Address: " + address + " ";
result += "Phone: " + phone;
return result;
}
//-----------------------------------------------------------------
// Derived classes must define the pay method for each type of
// employee.
//-----------------------------------------------------------------
public abstract double pay();
}
}
//********************************************************************
// Volunteer.java
//
// Represents a staff member that works as a volunteer.
public class Volunteer extends StaffMember
{
//-----------------------------------------------------------------
// Constructor: Sets up this volunteer using the specified
// information.
//-----------------------------------------------------------------
public Volunteer (String eName, String eAddress, String ePhone)
{
super (eName, eAddress, ePhone);
}
//-----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
//-----------------------------------------------------------------
public double pay()
{
return 0.0;
}
}
*************************************************************************
Thank you.
Explanation / Answer
//StaffMember.java
abstract public class StaffMember {
protected String name;
protected String address;
protected String phone;
// -----------------------------------------------------------------
// Constructor: Sets up this staff member using the specified
// information.
// -----------------------------------------------------------------
public StaffMember(String eName, String eAddress, String ePhone) {
name = eName;
address = eAddress;
phone = ePhone;
}
// -----------------------------------------------------------------
// Returns a string including the basic employee information.
// -----------------------------------------------------------------
public String toString() {
String result = "Name: " + name + " ";
result += "Address: " + address + " ";
result += "Phone: " + phone;
return result;
}
// -----------------------------------------------------------------
// Derived classes must define the pay method for each type of
// employee.
// -----------------------------------------------------------------
public abstract double pay();
}
//Staff.java
public class Staff {
private StaffMember[] staffList;
// -----------------------------------------------------------------
// Constructor: Sets up the list of staff members.
// -----------------------------------------------------------------
public Staff() {
staffList = new StaffMember[6];
staffList[0] = new Executive("Sam", "123 Main Line", "555-0469",
"123-45-6789", 2423.07);
staffList[1] = new Employee("Carla", "456 Off Line", "555-0101",
"987-65-4321", 1246.15);
staffList[2] = new Employee("Woody", "789 Off Rocker", "555-0000",
"010-20-3040", 1169.23);
staffList[3] = new Hourly("Diane", "678 Fifth Ave.", "555-0690",
"958-47-3625", 10.55);
staffList[4] = new Hourly("Norm", "987 Suds Blvd.", "555-8374",
"456-78-9000", 11.2);
staffList[5] = new Volunteer("Cliff", "321 Duds Lane", "555-7282");
((Executive) staffList[0]).awardBonus(500.00);
((Hourly) staffList[3]).addHours(40);
((Hourly) staffList[4]).addHours(30);
}
// -----------------------------------------------------------------
// Pays all staff members.
// -----------------------------------------------------------------
public void payday() {
double amount;
for (int count = 0; count < staffList.length; count++) {
System.out.println(staffList[count]);
amount = staffList[count].pay(); // polymorphic
if (amount == 0.0)
System.out.println("Thanks!");
else
System.out.println("Paid: " + amount);
System.out.println("-----------------------------------");
}
}
public StaffMember getStaff(int i) {
StaffMember st = staffList[i];
return st;
}
public int getSize() {
int sz = staffList.length;
return sz;
}
public void setNewStaffList(StaffMember[] staffList){
this.staffList = staffList;
}
}
//Volunteer.java
public class Volunteer extends StaffMember {
// -----------------------------------------------------------------
// Constructor: Sets up this volunteer using the specified
// information.
// -----------------------------------------------------------------
public Volunteer(String eName, String eAddress, String ePhone) {
super(eName, eAddress, ePhone);
}
// -----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
// -----------------------------------------------------------------
public double pay() {
return 0.0;
}
}
//Hourly.java
public class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// Constructor: Sets up this hourly employee using the specified
// information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
result += " Current hours: " + hoursWorked;
return result;
}
}
//Executive.java
public class Executive extends Employee {
private double bonus;
// -----------------------------------------------------------------
// Constructor: Sets up this executive with the specified
// information.
// -----------------------------------------------------------------
public Executive(String eName, String eAddress, String ePhone,
String socSecNumber, double rate) {
super(eName, eAddress, ePhone, socSecNumber, rate);
bonus = 0; // bonus has yet to be awarded
}
// -----------------------------------------------------------------
// Awards the specified bonus to this executive.
// -----------------------------------------------------------------
public void awardBonus(double execBonus) {
bonus = execBonus;
}
// -----------------------------------------------------------------
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
// -----------------------------------------------------------------
public double pay() {
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//Employee.java
public class Employee extends StaffMember {
protected String socialSecurityNumber;
protected double payRate;
// -----------------------------------------------------------------
// Constructor: Sets up this employee with the specified
// information.
// -----------------------------------------------------------------
public Employee(String eName, String eAddress, String ePhone,
String socSecNumber, double rate) {
super(eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
// -----------------------------------------------------------------
// Returns information about an employee as a string.
// -----------------------------------------------------------------
public String toString() {
String result = super.toString();
result += " Social Security Number: " + socialSecurityNumber;
return result;
}
@Override
public double pay() {
return payRate;
}
}
//Main.java (driver class)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String arg[]) throws Exception {
Staff staff = new Staff();
staff.payday();
int choice = 1;
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
List<StaffMember> listOfMemebrs = new ArrayList<>();
for (int i = 0; i < staff.getSize(); i++) {
listOfMemebrs.add(staff.getStaff(i));
}
while (choice == 1 && listOfMemebrs.size() > 0) {
System.out
.println("Please enter name to be delete (case insensitive): ");
String name = reader.readLine();
Iterator<StaffMember> listIterator = listOfMemebrs.iterator();
while (listIterator.hasNext()) {
StaffMember sm = listIterator.next();
if (sm.name.equalsIgnoreCase(name)) {
if (sm instanceof Executive) {
System.out.println("Sorry Cannot delete Executive!.");
} else {
listIterator.remove();
System.out.println("The StaffMember " + name
+ " has been deleted successfully!.");
}
}
}
System.out.println(" Continue? Enter Your Choice (Yes=1/No=0) :");
choice = Integer.parseInt(reader.readLine());
}
choice = 1;
while (listOfMemebrs.size() > 0 && choice == 1) {
System.out
.println("Please enter name to modify (case insensitive): ");
String name = reader.readLine();
Iterator<StaffMember> listIterator = listOfMemebrs.iterator();
while (listIterator.hasNext()) {
StaffMember sm = listIterator.next();
if (sm.name.equalsIgnoreCase(name)) {
System.out.println("Enter address : ");
String address = reader.readLine();
sm.address = address;
System.out.println("Address changed successfully!.");
}
}
System.out.println("Continue? Enter Your Choice (Yes=1/No=0) :");
choice = Integer.parseInt(reader.readLine());
}
staff.setNewStaffList(listOfMemebrs
.toArray(new StaffMember[listOfMemebrs.size()]));
System.out.println(" ******************After Changes************************");
staff.payday();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.