In Java, Write an application that allows a user to enter the employee names and
ID: 3601214 • Letter: I
Question
In Java,
Write an application that allows a user to enter the employee names and their hire dates of up to 10 employees for Company A.
Continue to prompt the user for employee names and their hire dates until the user enters the sentinel value “STOP” for an employee name or has entered 10 employee names, whichever comes first.
When the user is done entering employee names, calculate how many employee names were entered, and then show the names entered.
In a loop, continuously ask the user to type one of the employee names and display the hire date for the employee or display a message if the employee name was not entered before.
The loop continues until the user enters “STOP” for the employee name. Save the application as EmployeeHireDate.java.
(please indent the code too)
Explanation / Answer
Implemented the code as per the requirement. Please comment if any modification required.
Code:
========
import java.util.Scanner;
public class EmployeeHireDate {
static String[] names = new String[10]; //Array to store name
static String[] dates = new String[10]; //Array to store name
static int num=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scanner to read input
String name ="";
boolean quit = false;
while(num<10){ //Loop to enter upto 10 employee records
System.out.print("Enter employee name or Stop: ");
name = sc.nextLine();
if(num==0 && name.equalsIgnoreCase("STOP")) {
System.exit(0);
}
else if(name.equalsIgnoreCase("STOP")){ //Stop condition
break;
}
else if(name.equals("")) {
System.out.println("Please enter valid employee name... ");
continue;
}
else{ //Storing in the arrays if stop condition not met
names[num] = name;
System.out.print("Enter hire date in the format MM/DD/YYYY: ");
dates[num] = sc.nextLine();
num++;
}
}
while(!quit){ //Loop to find hire dates of employees
System.out.print("Enter employee name to display hire date or stop: ");
name = sc.nextLine(); //REading input
boolean found = false;
if(name.equalsIgnoreCase("STOP")){ //Loop termination condition
break;
}
int i;
for(i=0; i<num; i++){ //For loop to search the employee hire date
if(names[i].equalsIgnoreCase(name)){ //Comparing the user input and stored names
found = true;
break;
}
}
if(found){ //Success condition
System.out.println("Hire date of "+ name + " is "+dates[i]);
}
else{//Failure condition
System.out.println("Employee data not found");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.