Ad

Enum in java (Lab Task)

                                Enumeration

What is enum:
   The Enum in Java is an information type which contains a fixed arrangement of constants. 

It very well may be utilized for quite a long time of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY) , headings (NORTH, SOUTH, EAST, and WEST), season (SPRING, SUMMER, WINTER, and AUTUMN or FALL), hues (RED, YELLOW, BLUE, GREEN, WHITE, and BLACK) and so forth. As indicated by the Java naming shows, we ought to have all constants in capital letters. Along these lines, we have enum constants in capital letters. 

Java Enums can be thought of as classes which have a fixed arrangement of constants (a variable that doesn't change). The Java enum constants are static and last verifiably. It is accessible since JDK 1.5.

Practice:

Task 1:
Make an enum of Gender, which has two possible values, “Female” and “Male”.
Make a class Employee that has four attributes, id, name, gender and basicSalary. Use above enum for gender. Make a fully parameterized constructor.
a.       This class has a method calculateBonus() that calculate bonus using following rules:
                                                              i.      If employee is male, he will get 10% bonus on his basic salary.
                                                            ii.      If the employee is female, she will get 12% bonus on her basic salary.
Make a class EmployeeTest and create an array of Employee to input information of all employees. Ask user to input number of employees. All attributes values of Employee (id,name,basicSalary and gender) shall be taken from user.

//Employee Class
/**
 *
 * @author hashmi
 */
public class Employee {
    public enum Gender
    {
        Female,
        Male,
    }
    private long id;
    private String name;
    private int basicSalary;
    private Gender gender;

    public Employee(long id,String name, int basicSalary, Gender gender) {
        this.id = id;
        this.name = name;
        this.basicSalary = basicSalary;
        this.gender = gender;
    }
     public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }
    public int getBasicSalary() {
        return basicSalary;
    }

    public Gender getGender() {
        return gender;
    }
    public void calculateBonus(){
        int totalSalary,inc;
        System.out.println("id:"+id);
        System.out.println("name:"+name);
        System.out.println("basic salary:"+basicSalary);
        System.out.println("gender:"+gender);
        if(gender == Gender.Male){
            inc=(basicSalary*10)/100;
            totalSalary=basicSalary+inc;
            System.out.println("Basic Salary:"+basicSalary);
             System.out.println("Bouns:"+inc);
            System.out.println("New Salary:"+totalSalary);
           
        }
        else if(gender == Gender.Female){
            inc=basicSalary*(12/100);
            totalSalary=basicSalary+inc;
            System.out.println("Basic Salary:"+basicSalary);
             System.out.println("Bouns:"+inc);
            System.out.println("New Salary:"+totalSalary);
        }
        else{
            System.out.println("Invalid");
        }
    }
}


//Test Class:

import java.util.*;
public class EmployeeTest {

        public static void main(String args[]){
            int i;
            Scanner input= new Scanner(System.in);
             System.out.println("Enter the employee id:");
             long id=input.nextLong();
            System.out.println("Enter the employee name:");
             String name=input.nextLine();
           
            System.out.println("Enter the  Salary:");
             int basicSalary=input.nextInt();
            System.out.println("Enter Gender(press 1 for Male/ press 2 for Female)");
            i=input.nextInt();
        if(i==1) {
            Employee obj = new Employee(id, name, basicSalary, Employee.Gender.Male);
           obj.calculateBoonus();}
        else {
            Employee obj = new Employee(id, name, basicSalary, Employee.Gender.Female);
            obj.calculateBonus();
        }
          }
    }


Output:





Post a Comment

0 Comments