Q:

HR Employees system | simple OOP example convert uml diagram to java code

0

convert the following uml class diagram to equavalent java programming code:

in this example we will use most of OOP features in java programming, like:

  • methods
  • class and object
  • instance and constructor
  • static and non static
  • access modifiers ( public, private, default, protected).
  • encapsulation (set and get)
  • inheritence
  • method overriding
  • arrays of objects
  • polymorphism

----------------------------------------------------

develop a simple HR information system to manage employees salaries,

any employee has basic properties like: id, name, born date, gender, basic salary, accomodation allowance, food allowance

there are 2 types of employees:

  • Hourly Employee

         this employee gets his salary upon hourly basis, the normal work hours             in the month are 174 hours, any extra work hours will be added to basic           salary. 

  • Bonus Employee

         this employee gets his salary without calculation of worked hours, and             takes a monthly bonus added to his basice salary, the bonus is calculated based on employee classification ( class 1 for managers: bonus is 20% out of basic salary, class 2 for moderate managers: bonus is 15% out of basic salary, class 3 for operators: bonus is 10% out of basic salary)

----------------------------------

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Date;
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
	    Employee []empARR = new Employee[5];
        System.out.println("welcome, now you will start entering data of 5 employees:");
        for (int i = 0; i < empARR.length; i++)
        {
            System.out.println("what is employee type? press 1 for bonus employee, press 2 for hourly employee:");
            Employee emp=new Employee();
            int choice = input.nextInt();
            if (choice == 1)
            {
                BonusEmployee bemp = new BonusEmployee(); 
                System.out.println("enter employee classification(press 1 for manager, 2 for moderate manager, 3 for operater:");
                bemp.setEmployeeClassification(input.nextInt());
                emp = bemp;
            }
            else if (choice == 2)
            {
                HourlyEmployee hemp = new HourlyEmployee(); 
                System.out.println("enter employee hourly rate:");
                hemp.setHourlyRate(input.nextInt());
                System.out.println("enter employee extra worked hours:");
                hemp.setExtraWorkedHours(input.nextInt());
                emp= hemp;
            }
            else
            {
                System.out.println("invalid entry...you have to retry again...");
                i--;
                continue;
            }
            System.out.println("enter employee id:");
            emp.setId(input.nextInt());
            System.out.println("enter employee name:");
            emp.setName(input.next());
            System.out.println("enter basic salary:");
            emp.setBasicSalary(input.nextInt());
            System.out.println("enter accomodation allowance:");
            emp.setAccomodationAllowance(input.nextInt());
            System.out.println("enter food allowance:");
            emp.setfoodAllowance(input.nextInt());
            System.out.println("enter employee's gender press m for a male or f for a female");
            String gender = input.next();
            if (gender.equals("m"))
                emp.setGender("male");
            else if (gender.equals("f"))
                emp.setGender("female");
            else
            {
                System.out.println("invalid entry...you have to retry again...");
                i--;
                continue;
            }
            System.out.println("enter employee's born year: ");
            emp.setBornyear(input.nextInt());
            empARR[i] = emp;
        }

        //printing the employees info including total salaries
        System.out.println("Now printing employees information:\n======================");
        for (Employee emp : empARR)
        {
            System.out.println(emp.toString());
            System.out.println("\ntotal salary is : "+emp.calculateTotalSalary());
            System.out.println("---------------------------");
        }
	}
}


class Employee
{
        private int id;
        private String name;
        private int basicSalary;
        private int accomodationAllowance;
        private int foodAllowance;
        private boolean gender;
        private int bornyear;
        public Employee() { }   
        public Employee(int id, String name, int basicSalary, int accomodationAllowance, int foodAllowance, String gender, int bornyear)
        {
            this.id = id;
            this.name = name;
            this.basicSalary = basicSalary;
            this.accomodationAllowance = accomodationAllowance;
            this.foodAllowance = foodAllowance;
            setGender(gender);
            this.bornyear = bornyear;
        }
        public void setId(int id)
        {
            this.id = id;
        }
        public void setName(String name)
        {
            this.name = name;
        }
        public void setBasicSalary(int basicSalary)
        {
            this.basicSalary = basicSalary;
        }
        public void setAccomodationAllowance( int accomodationAllowance)
        {
            this.accomodationAllowance=accomodationAllowance;  
        }
        public void setfoodAllowance(int foodAllowance)
        {
            this.foodAllowance=foodAllowance;
        }
        public void setGender(String gender)
        {
            if (gender == "male")
                this.gender = true;
            else if (gender == "female")
                this.gender = false;
            else System.out.println("invalid entry");
        }
        public void setBornyear(int bornyear)
        {
            this.bornyear = bornyear;
        }
        public int getId()
        {
            return id;
        }
        public String getName()
        {
            return name;
        }
        public int getBasicSalary()
        {
            return basicSalary;
        }
        public int getAccomodationAllowance()
        {
            return accomodationAllowance;
        }
        public int getFoodAllowance()
        {
            return this.foodAllowance;
        }
        public String getGender()
        {
            if (this.gender == true)
                return "male";
            else return "female";
        }
        public int getBornyear()
        { 
            return this.bornyear;
        }
        public int getAge()
        {
            Date d=new Date();
            return d.getYear() - this.bornyear;
        }
        public String toString()
        {
            return "Employee infomation: \n============\nId: "+id+"\nName: "+name+"\nBasic salary: "+basicSalary+"\nGender: "+getGender();
        }
        public double calculateTotalSalary()
        {
            return basicSalary + accomodationAllowance + foodAllowance;
        }
    }

class HourlyEmployee extends Employee
{
        private int hourlyRate;
        private int extraWorkedHours;

        public HourlyEmployee() { }
        public HourlyEmployee(int id, String name, int basicSalary, int accomodationAllowance, int foodAllowance, String gender, int bornyear, int hourlyRate, int extraWorkedHours)
        {
            super(id, name,basicSalary, accomodationAllowance, foodAllowance, gender, bornyear);
            this.hourlyRate= hourlyRate;
            this.extraWorkedHours= extraWorkedHours;
        }

        public void setHourlyRate(int hourlyRate)
        {
            this.hourlyRate = hourlyRate;
        }
        public int getHourlyRate() { return hourlyRate; }
        public void setExtraWorkedHours(int extraWorkedHours)
        {
            this.extraWorkedHours = extraWorkedHours;
        }
        public int getExtraWorkedHours() { return extraWorkedHours; }

        //overriding the toString() method:
        @Override
        public String toString()
        {
            return super.toString() + "\nHourly Rate: " + getHourlyRate()+"\nExtra worked hours: "+getExtraWorkedHours();
        }
        //overriding the calculateTotalSalary() method:
        @Override
        public double calculateTotalSalary()
        {
            double totalSalary = super.calculateTotalSalary()+hourlyRate*extraWorkedHours;
            return totalSalary;
        }
}

class BonusEmployee extends Employee
    {
        private int employeeClassification;
        public BonusEmployee() { }
        public BonusEmployee(int id, String name, int basicSalary, int accomodationAllowance, int foodAllowance, String gender, int bornyear,int employeeClassification)
        {
            super(id, name,basicSalary,accomodationAllowance, foodAllowance, gender,bornyear);
            this.employeeClassification = employeeClassification;
        }
        public String getEmployeeClassification() 
        {
            if (employeeClassification == 1)
                return "manager";
            else if (employeeClassification == 2)
                return "moderate manager";
            else //if (employeeClassification == 3)
                return "operator";
        }
        public void setEmployeeClassification(int employeeClassification) 
        { this.employeeClassification = employeeClassification; }

        //overriding the toString() method:
        @Override
        public String toString()
        {
            return super.toString()+"\nClassification: "+getEmployeeClassification();
        }
        //overriding the calculateTotalSalary() method:
        @Override
        public double calculateTotalSalary()
        {
            double totalSalary = super.calculateTotalSalary();

            //adding the bonus value:
            if (employeeClassification == 1)//manager
                totalSalary = totalSalary + super.getBasicSalary() * 0.2;
            else if (employeeClassification == 2)//moderate manager
                totalSalary = totalSalary + super.getBasicSalary() * 0.15;
            else if (employeeClassification == 3)//operater
                totalSalary = totalSalary + super.getBasicSalary() * 0.1;
            return totalSalary;
        }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now