In addition to what has been covered in previous assignments, the use of the fol
ID: 639879 • Letter: I
Question
In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
Inheritance
The protected modifier
The super Reference
Abstract class
NumberFormat/DecimalFormat
Wrapper classes
ArrayList
In Assignment #5, you will need to make use of inheritance by creating a class hierarchy for students.
Employee class
Employee is an abstract class, which represents the basic attributes of any employee in a company . It is used as the root of the company employee hierarchy. It has the following attributes (should be protected):
The following constructor method should be provided to initialize the instance variables.
public Employee(String, String, String)
The firstName, lastName, and employeeId are initialized to the value of the first parameter, the second parameter, and the third parameter, respectively. The pay should be initialized to 0.0.
The following accessor method should be provided for employeeId :
public String getEmployeeId()
The class Employee also has an abstract method (which should be implemented by its child classes, Volunteer, FullTime, and PartTime classes) to compute the pay of the employee:
public abstract void computePay();
The following public method should be provided:
public String toString()
toString method returns a string of the following format:
First name: Elvis
Last name: Presley
Employee ID: 000001
Pay: $30.00
Volunteer class
Volunteer is a subclass of Employee class. It represents a volunteer that works without any pay. There is no additional instance variable, but there are additional methods to the inherited ones.
The following constructor method should be provided:
public Volunteer(String, String, String)
This constructor calls the constructor of its parent using the super reference.
The following method should be implemented:
public void computePay()
It computes the pay for the volunteer. The pay is 0.0.
Also, the following method should be implemented:
public String toString()
The toString() method inherited from Employee class should be used to create a new string, and display a volunteer's information using the following format:
Volunteer:
First name: Elvis
Last name: Presley
Employee ID: 000001
Pay: $0.00
This toString method should make use of the toString method of the parent class.
FullTime class
FullTime is a subclass of Employee class. It represents a full time employee that can also receive bonus in addition to his/her salary.
It has the following attribute in addition to the inherited ones:
The following constructor method should be provided:
public FullTime(String, String, String, double, double)
The firstName, lastName, and employeeId are initialized to the value of the first parameter, the second parameter, and the third parameter, respectively. The pay should be initialized to 0.0. Thus they are done by calling the constructor of the parent. The rate and bonus are initialized to the value of the forth and fifth parameters, respectively.
The following method should be implemented:
public void computePay()
It computes the pay for the full time employee by adding rate and bonus.
Also, the following method should be implemented:
public String toString()
The toString() method inherited from Employee class should be used to create a new string, and display a full time employee's information using the following format:
Full Time Employee:
First name: Elvis
Last name: Presley
Employee ID: 000001
Pay: $350.00
Rate: $300.00
Bonus: $50.00
This toString method should make use of the toString method of the parent class.
PartTime class
PartTime is a subclass of Employee class. It represents a part time employee in a company. It has the following additional attributes:
The following constructor method should be provided:
public PartTime(String, String, String, double, int)
The firstName, lastName, and employeeId are initialized to the value of the first parameter, the second parameter, and the third parameter, respectively. The pay should be initialized to 0.0. Thus they are done by calling the constructor of the parent. The rate and hoursWorked are initialized to the value of the forth and fifth parameters, respectively.
The following method should be implemented:
public void computePay()
It computes the pay of the part time employee. (computed by rate*hoursWorked)
Also, the following method should be implemented:
public String toString()
The toString() method inherited from the Employee class should be used to create a new string, and display a part time employee's information using the following format:
Part Time Employee:
First name: Elvis
Last name: Presley
Employee ID: 000001
Pay: $150.00
Rate: $30.00
Hours: 5
EmployeeParser class
The EmployeeParser class is a utility class that will be used to create an employee object (one of a volunteer object, a full time employee object, and a part time employee object) from a parsable string. The EmployeeParser class object will not be instantiated. It must have the following method:
public static Employee parseStringToEmployee(String lineToParse)
The parseStringToEmployee method's argument will be a string in the following format:
For a volunteer,
type/firstName/lastName/employeeId
For a full time employee,
type/firstName/lastName/employeeId/rate/bonus
For a part time employee,
type/firstName/lastName/employeeId/rate/hoursWorked
A real example of this string would be:
Volunteer/Elvis/Presley/000001
OR
FullTime/Marilyn/Monroe/200005/400000/50000
OR
PartTime/Albert/Einstein/000002/25.25/10
The EmployeeParser will parse this string, pull out the information, create/instantiate a new object of one of Volunteer, FullTime, or PartTime using their constructor with attributes of the object (depending on whether the first substring is Volunteer, FullTime, or PartTime), and return it to the calling method. The type will always be present and always be one of Volunteer, FullTime, and PartTime. (It can be lower case or upper case) You may add other methods to the Volunteer, FullTime, and PartTime classes in order to make your life easier.
Assignment5 class
In this assignment, download Assignment5.java file by clicking the link, and use it for your assignment. You need to add code to this file. The parts you need to add are written in the Assignment5.java file, namely for the four cases "Add Employee", "Compute Pay", "Search for Employee", and "List Employees".
All input and output should be handled here. The main method should start by displaying this updated menu in this exact format:
Choice Action
------ ------
A Add Employee
C Compute Pay
D Search for Employee
L List Employees
Q Quit
? Display Help
Next, the following prompt should be displayed:
What action would you like to perform?
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase.
Add Employee
Your program should display the following prompt:
Please enter some employee information to add:
Read in the information and parse it using the employee parser.
Then add the new object of one of volunteer, full time employee, or part time employee (created by employee parser) to the employee list.
Compute Pay
Your program should compute pay for all employees created so far by calling computePay() method.
After computing pay, display the following:
pay computed
Search for Employee
Your program should display the following prompt:
Please enter an employeeID to search:
Read in the string and look up the employee list, if there exists an employee object with the same employee ID, then display the following:
employee found
Otherwise, display this:
employee not found
List Employees
List the employees in the employee list. Make use of toString method defined in child classes of the Employee class.
A real example is looked like this:
Volunteer:
First name: Elvis
Last name: Presley
Employee ID: 000001
Pay: $0.00
Full Time Employee:
First name: Albert
Last name: Einstein
Employee ID: 123456
Pay: $160,000.00
Rate: $150,000.00
Bonus: $10,000.00
If there is no employee in the employee list, then display following:
no employee
Quit
Your program should stop executing and output nothing.
Display Help
Your program should redisplay the "choice action" menu.
Invalid Command
If an invalid command is entered, display the following line:
Unknown action
Attribute name Attribute type Description firstName String The first name of the employee lastName String The last name of the employee employeeId String The id of the employee pay double The pay of the employeeExplanation / Answer
Assignment5.java
import java.io.*; //to use InputStreamReader and BufferedReader
import java.util.*; //to use ArrayList
public class Assignment5
{
public static void main (String[] args)
{
char input1;
String inputInfo = new String();
String line = new String();
boolean found = false;
// ArrayList object is used to store member objects
ArrayList <StaffMember> memberList = new ArrayList <StaffMember>();
try
{
printMenu(); // print out menu
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.println("What action would you like to perform?");
line = stdin.readLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
switch (input1)
{
case 'A': //Add Member
System.out.print("Please enter a member information to add: ");
inputInfo = stdin.readLine().trim();
StaffMember m = StaffMemberParser.StringToMember(inputInfo); //creates staffmember object and passes string from inputinfo into StringToMember method
memberList.add(m); //adds object from staffmember to memberList array
/***********************************************************************************
*** ADD your code here to create an object of one of child classes of StaffMember class
*** and add it to the memberList
***********************************************************************************/
break;
case 'C': //Compute Pay
for(int i=0; i<memberList.size(); i++) //computes pay for different objects that are held in memberList
{
memberList.get(i).computePay();
}
/***********************************************************************************
*** ADD your code here to compute the pay for all members the memberList.
***********************************************************************************/
System.out.print("pay computed ");
break;
case 'D': //Search for Member
found=false;
System.out.print("Please enter a memberID to search: ");
inputInfo = stdin.readLine().trim();
for(int i=0; i<memberList.size(); i++) //reads string from user and compares it against known member ids
{
if(memberList.get(i).getMemberId().equalsIgnoreCase(inputInfo))
{
found=true ; break; //sets found to true if member id is found
}
}
/***********************************************************************************
*** ADD your code here to search a given memberID. If found, set "found" true,
*** and set "found" false otherwise.
***********************************************************************************/
if (found == true)
System.out.print("member found ");
else
System.out.print("member not found ");
break;
case 'L': //List Members
for(int i=0; i<memberList.size(); i++) //prints all details about member objects
{
System.out.print(memberList.get(i));
}
/***********************************************************************************
*** ADD your code here to print out all member objects. If there is no member,
*** print "no member "
***********************************************************************************/
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q'); // stop the loop when Q is read
}
catch (IOException exception)
{
System.out.println("IO Exception");
}
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Member " +
"C Compute Pay " +
"D Search for Member " +
"L List Members " +
"Q Quit " +
"? Display Help ");
}
}
FullTimeEmployee.java
import java.io.*; //to use InputStreamReader and BufferedReader
import java.util.*; //to use ArrayList
import java.text.NumberFormat;
public class FullTimeEmployee extends StaffMember
{
private double rate;
private double bonus;
NumberFormat money = NumberFormat.getCurrencyInstance();
public FullTimeEmployee(String a, String b, String c, double d, double e)
{
super(a,b,c); //uses parent constructor as well as local variables
rate = d;
bonus= e;
}
public void computePay() //method for fulltimeemployee to compute pay
{
pay=rate+bonus;
}
public String toString()
{
return (" Full Time Employee:"+super.toString()+"Rate: "+money.format(rate)+" "+
"Bonus: "+money.format(bonus)+" ");
}
}
HourlyEmployee.java
StaffMember.java
StaffMemberParser.java
Volunteer.java
}
}
}
StaffMember.java
import java.io.*; //to use InputStreamReader and BufferedReader import java.util.*; //to use ArrayList import java.text.NumberFormat; public abstract class StaffMember { protected String firstName; protected String lastName; protected String memberId; protected double pay=0.0; NumberFormat money = NumberFormat.getCurrencyInstance(); public StaffMember(String fname, String lname, String id) //constructor that will be used by every other child class { firstName=fname; lastName=lname; memberId=id; } public String getMemberId() //gets the member id and returns it { return memberId; } public abstract void computePay(); //computepay method that will be used by child classes public String toString() //prints details of staffmember object { return (" First name: "+firstName+" " + "Last name: "+lastName+" " + "Member ID: "+memberId+" " + "Pay: "+money.format(pay)+" "); }StaffMemberParser.java
import java.io.*; //to use InputStreamReader and BufferedReader import java.util.*; //to use ArrayList public class StaffMemberParser { public static StaffMember StringToMember(String lineToParse) { StaffMember m = null; //creates staffmember object to hold child classes String []info = new String[6]; //string array to hold StringToMember argument info = lineToParse.split("/"); if(info[0].equalsIgnoreCase("Volunteer")) m = new Volunteer(info[1], info[2], info[3]); //creates and adds volunteer object to staffmember object if(info[0].equalsIgnoreCase("fulltimeemployee")) m = new FullTimeEmployee(info[1], info[2], info[3], Double.parseDouble(info[4]), Double.parseDouble(info[5])); //creates and adds fulltimeemployee object to staffmember object if(info[0].equalsIgnoreCase("hourlyemployee")) m = new HourlyEmployee(info[1], info[2], info[3], Double.parseDouble(info[4]), Integer.parseInt(info[5])); //creates and adds hourlyemployee object to staffmember object return m; }Volunteer.java
import java.io.*; //to use InputStreamReader and BufferedReader import java.util.*; //to use ArrayList public class Volunteer extends StaffMember { public Volunteer(String a, String b, String c) { super(a,b,c); //uses parent class constructor } public void computePay() //computepay method for volunteer { pay = 0.0; } public String toString() { return (" Volunteer:" +super.toString()+" "); } }}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.