Consider the following scenario:
A company wants to create a program to manage their employee records. They have decided to use variables and constants to store important information about each employee. The program should be able to store and retrieve the employee's name, age, employee ID, and job title. In addition, the program should be able to calculate and store the employee's salary based on their job title.
Define and initialize the following variables and constants based on the requirements:
employeeName
to store the employee's nameemployeeAge
to store the employee's ageemployeeID
to store the employee's IDjobTitle
to store the employee's job titlesalary
as a constant to store the employee's salaryWrite a code snippet that prompts the user to input values for the variables employeeName
, employeeAge
, employeeID
, and jobTitle
. Store the user inputs in the respective variables.
Now, write a code snippet that calculates and assigns the appropriate salary based on the following criteria:
Assume that the necessary imports and declarations have already been done.
Provide your code solution below.
Note: Remember to include all necessary steps and explanations in your answer.
String employeeName = "";
int employeeAge = 0;
int employeeID = 0;
String jobTitle = "";
final int salary;
In the code above, employeeName
, employeeAge
, employeeID
, and jobTitle
are variables to store the employee's information. salary
is declared as a final constant as it needs to be assigned based on job title but will remain constant once assigned.
employeeName
, employeeAge
, employeeID
, and jobTitle
. Store the user inputs in the respective variables.Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name: ");
employeeName = scanner.nextLine();
System.out.print("Enter employee age: ");
employeeAge = scanner.nextInt();
System.out.print("Enter employee ID: ");
employeeID = scanner.nextInt();
System.out.print("Enter job title: ");
jobTitle = scanner.nextLine();
In the code above, a Scanner
is used to receive user input. The user is prompted to enter values for employeeName
, employeeAge
, employeeID
, and jobTitle
. The Scanner.nextLine()
method is used after nextInt()
to consume the remaining newline character left in the buffer.
switch (jobTitle) {
case "Manager":
salary = 80000;
break;
case "Engineer":
salary = 70000;
break;
case "Assistant":
salary = 50000;
break;
default:
salary = 40000;
break;
}
In the code above, a switch statement is used to determine the job title and assign the appropriate salary value to the salary
variable. If the job title matches "Manager", the salary is set to $80,000. If the job title matches "Engineer", the salary is set to $70,000. If the job title matches "Assistant", the salary is set to $50,000. For any other job title, the salary is set to $40,000.
The reason for using a switch statement is to handle different job titles easily and ensure that the correct salary is assigned based on the job title entered by the user.
Overall, this program allows the company to manage employee records by storing their information using variables and constants, and calculates the salary based on the job title using a decision-making statement.