Task 10E: If the user selects ‘Add’, the application must be modified to prompt
ID: 3704778 • Letter: T
Question
Task 10E: If the user selects ‘Add’, the application must be modified to prompt the user for appropriate employee information and set the specific information using the mutator methods you created in Task 8D.
Task 10F: Run the EmployeeApp, choose . The object references are displayed on the screen. Add a toString method to the Employee class which returns a formatted string pertaining to the employee information. Play with returning different formatted strings, this choose when running the application. This will demonstrate how this method controls the format of the employee object. String toString()
Task 10G: If you choose ‘Edit’ from the EmployeeApp, the application will prompt for a last name. The application will do a comparison of this last name to each employee object. Add a equals method to the Employee class which accepts the last name String that was entered from the menu and return either true or false. This method will compare the object last name variable (this.lastName) with the last name that was prompted for in the application. If the names match, return true. If the names do not match, return false. public Boolean equals( String name )
Im having trouble in task 10e where I have to prompt and set the objects.
here is the code i have so far:
package employeeapp;
import java.util.Scanner;
class Employee
{
private String firstName;
private String lastName;
private int employeeId;
private double salary;
/* Task 10C: Add three constructors */
public Employee(){
}
public Employee( String last, String first ) {
}
public Employee( String last, String first, int id, double wage ){
}
/* Task 10D: Add set (mutator) and get (accessor) meethods*/
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
/*Task 10F: Add toString method */
@Override
public String toString() {
return "Employee{" + '}';
}
/* Task 10G: Add equals method */
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
return true;
}
}public class EmployeeApp
{
public static final int MAX_EMPLOYEES = 5;
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
Employee[] employees = new Employee[MAX_EMPLOYEES];
Employee currentEmployee;
String inputString;
int inputInt;
double inputDouble;
char choice;
int empCount=0;
employees[empCount++] = new Employee( "Mitchum", "Robert", 120402, 34000.0 );
employees[empCount++] = new Employee( "Ryan", "Cornelius" );
employees[empCount++] = new Employee( "Asimov", "Isaac" );
do
{System.out.println( " Enter Selection ===============" );
System.out.println( "A> Add new Employee" );
System.out.println( "E> Edit Employee" );
System.out.println( "L> List Employees" );
System.out.println( "Q> Quit" );
System.out.print( " Select: " );
inputString = keyboard.nextLine();
choice = inputString.toUpperCase().charAt( 0 );
System.out.println();
switch( choice )
{case 'A':
if ( empCount < MAX_EMPLOYEES-1 )
{
// Create object for new Employee employees[empCount] = new Employee();
employees[empCount] = new Employee();
// Task 8E: Prompt for user information and set the object
System.out.print("Enter First name" );
System.out.print("Enter Last name" );
System.out.print("Enter Employee Id" );
// parameters via the mutator methods
empCount++;
}
keyboard.nextLine();
break;
case 'E':
System.out.printf( "Enter Last Name of Employee to Edit: " );
inputString = keyboard.nextLine();
for ( int lp=0; lp<MAX_EMPLOYEES; lp++ ){
// Verify that the employee entry has been allocated before Editing
if ( employees[lp] != null ){
if ( employees[lp].equals( inputString ) == true ){
System.out.print( "Enter Employee ID : " );
inputInt = keyboard.nextInt();
employees[lp].setEmployeeId( inputInt );
System.out.print( "Enter Employee Salary: " );
inputDouble = keyboard.nextDouble();
employees[lp].setSalary( inputDouble );
keyboard.nextLine();}}}
break;
case 'L':
for ( int lp=0; lp<MAX_EMPLOYEES; lp++ ){
System.out.println( employees[lp] );
}
break;
}
keyboard.reset(); }
while( choice != 'Q' );
}
}
Explanation / Answer
Employee.java
class Employee
{
private String firstName;
private String lastName;
private int employeeId;
private double salary;
/* Task 10C: Add three constructors */
public Employee(){
}
public Employee( String last, String first ) {
this.firstName=first;
this.lastName=last;
}
public Employee( String last, String first, int id, double wage ){
this.firstName=first;
this.lastName=last;
this.employeeId=id;
this.salary=wage;
}
/* Task 10D: Add set (mutator) and get (accessor) meethods*/
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
/*Task 10F: Add toString method */
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName
+ ", employeeId=" + employeeId + ", salary=" + salary + "]";
}
/* Task 10G: Add equals method */
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
return true;
}
}
________________
EmployeeApp.java
import java.util.Scanner;
public class EmployeeApp {
public static final int MAX_EMPLOYEES = 5;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Employee[] employees = new Employee[MAX_EMPLOYEES];
Employee currentEmployee;
String fname,lname,inputString;
int empId,inputInt;
double salary,inputDouble;
char choice;
int empCount = 0;
employees[empCount++] = new Employee("Mitchum", "Robert", 120402,
34000.0);
employees[empCount++] = new Employee("Ryan", "Cornelius",120403,45000.0);
employees[empCount++] = new Employee("Asimov", "Isaac",120404,42000.0);
do {
System.out.println(" Enter Selection ===============");
System.out.println("A> Add new Employee");
System.out.println("E> Edit Employee");
System.out.println("L> List Employees");
System.out.println("Q> Quit");
System.out.print(" Select: ");
inputString = keyboard.nextLine();
choice = inputString.toUpperCase().charAt(0);
System.out.println();
switch (choice) {
case 'A':
if (empCount < MAX_EMPLOYEES - 1) {
// Create object for new Employee employees[empCount] = new
// Employee();
employees[empCount] = new Employee();
// Task 8E: Prompt for user information and set the object
System.out.print("Enter First name :");
fname=keyboard.next();
System.out.print("Enter Last name:");
lname=keyboard.next();
System.out.print("Enter Employee Id:");
empId=keyboard.nextInt();
System.out.print("Enter Salary :");
salary=keyboard.nextDouble();
// parameters via the mutator methods
employees[empCount++]=new Employee(fname,lname,empId,salary);
}
keyboard.nextLine();
break;
case 'E':
System.out.printf("Enter Last Name of Employee to Edit: ");
inputString = keyboard.nextLine();
for (int lp = 0; lp < MAX_EMPLOYEES; lp++) {
// Verify that the employee entry has been allocated before
// Editing
if (employees[lp] != null) {
if (employees[lp].getLastName().equals(inputString) == true) {
System.out.print("Enter Employee ID : ");
inputInt = keyboard.nextInt();
employees[lp].setEmployeeId(inputInt);
System.out.print("Enter Employee Salary: ");
inputDouble = keyboard.nextDouble();
employees[lp].setSalary(inputDouble);
keyboard.nextLine();
}
}
}
break;
case 'L':
for (int lp = 0; lp < empCount; lp++) {
System.out.println(employees[lp]);
}
break;
}
keyboard.reset();
}
while (choice != 'Q');
}
}
____________________
Output:
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: L
Employee [firstName=Robert, lastName=Mitchum, employeeId=120402, salary=34000.0]
Employee [firstName=Cornelius, lastName=Ryan, employeeId=120403, salary=45000.0]
Employee [firstName=Isaac, lastName=Asimov, employeeId=120404, salary=42000.0]
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: A
Enter First name :Kane
Enter Last name:Williams
Enter Employee Id:120405
Enter Salary :36000.0
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: L
Employee [firstName=Robert, lastName=Mitchum, employeeId=120402, salary=34000.0]
Employee [firstName=Cornelius, lastName=Ryan, employeeId=120403, salary=45000.0]
Employee [firstName=Isaac, lastName=Asimov, employeeId=120404, salary=42000.0]
Employee [firstName=Williams, lastName=Kane, employeeId=120405, salary=36000.0]
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: E
Enter Last Name of Employee to Edit: Ryan
Enter Employee ID : 120444
Enter Employee Salary: 23000.0
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: L
Employee [firstName=Robert, lastName=Mitchum, employeeId=120402, salary=34000.0]
Employee [firstName=Cornelius, lastName=Ryan, employeeId=120444, salary=23000.0]
Employee [firstName=Isaac, lastName=Asimov, employeeId=120404, salary=42000.0]
Employee [firstName=Williams, lastName=Kane, employeeId=120405, salary=36000.0]
Enter Selection
===============
A> Add new Employee
E> Edit Employee
L> List Employees
Q> Quit
Select: Q
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.