In: Computer Science
Create a class called employee which has the following instance variables:
Employee ID number
Salary
Years at the company
Your class should have the following methods:
New employee which reads in an employee’s ID number, salary and years of service
Anniversary which will up the years of service by 1
You got a raise which will read in how much the raise was (a percent) and then calculate the new salary
You get a bonus which gives a yearly bonus based on years of service so an employee gets a 5% bonus if they’ve been there 0-10 years and a 10% bonus if they’ve been there for over 10 years
You should also write main which should allow have an instance of your employee class, call the method that reads in the info and then allow the user to select between the anniversary, you got a raise and you got a bonus method and do the appropriate thing based on their selection. You may wish to loop this last part.
Code:
import java.util.*;
class Employee{
String employeeId;
double salary;
int years;
public static Scanner sc = new Scanner(System.in);
public void newEmployee(){
//taking user input
System.out.print("Enter employee ID : ");
this.employeeId = sc.nextLine();
System.out.print("Enter salary : ");
this.salary = sc.nextDouble();
System.out.print("Enter years : ");
this.years = sc.nextInt();
}
public void anniversary(){
this.years++;//updating years worked
}
public void raise(double percentage){
//calculating new salary based on percentage raise
this.salary = this.salary + this.salary*percentage/100.0;
}
public void bonus(){
//calculating and printing bonus based on the years worked
double bonus;
if(this.years <= 10){
bonus = 0.05 * this.salary;
}
else{
bonus = 0.10 * this.salary;
}
System.out.println("Bonus is "+ bonus);
}
public static void main(String args[]){
Employee e = new Employee();
e.newEmployee();
int n = 1;//loop control variable
while(n!=0){
System.out.print("Enter 1 for raise and 2 for anniversary,3 to see bonus, 0 to quit : ");
n = sc.nextInt();
if(n == 1){
System.out.print("Enter raise percentage : ");
double percentage = sc.nextDouble();
e.raise(percentage);
}
else if(n == 2){
e.anniversary();
}
else if(n == 3){
e.bonus();
}
else if(n != 0){
System.out.println("Wrong input choice");
}
System.out.println("Current details : "+e.employeeId+ " " + e.salary+" " +e.years);
}
}
}
output:
Code screenshot: