Q:

full OOP example program using c# programming : HR information system OOP example

0

in this example we will use most of OOP features in c#, like:

  • methods
  • class and object
  • instance and constructor
  • 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)

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

also build a test Class and create an array of 5 employees, read employees data from keyboard and then print total salary of each employee.

All Answers

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

Employee class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Object_Oreineted_Example
{
    internal class Employee
    {
        private int id;
        private string name;
        private int basicSalary;
        private int accomodationAllowance;
        private int foodAllowance;
        private bool 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 Console.WriteLine("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()
        {
            return Convert.ToInt32(DateTime.Now.Year.ToString()) - this.bornyear;
        }
        public virtual string toString()
        {
            return "Employee infomation: \n============\nId: "+id+"\nName: "+name+"\nBasic salary: "+basicSalary+"\nGender: "+getGender();
        }
        public virtual double calculateTotalSalary()
        {
            return basicSalary + accomodationAllowance + foodAllowance;
        }
    }
}

HourlyEmployee class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Object_Oreineted_Example
{
    internal class HourlyEmployee: 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) : base(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:
        public override string toString()
        {
            return base.toString() + "\nHourly Rate: " + getHourlyRate()+"\nExtra worked hours: "+getExtraWorkedHours();
        }
        //overriding the calculateTotalSalary() method:
        public override double calculateTotalSalary()
        {
            double totalSalary = base.calculateTotalSalary()+hourlyRate*extraWorkedHours;
            return totalSalary;
        }
    }
}

BonusEmployee Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Object_Oreineted_Example
{
    internal class BonusEmployee: Employee
    {
        private int employeeClassification;
        public BonusEmployee() { }
        public BonusEmployee(int id, string name, int basicSalary, int accomodationAllowance, int foodAllowance, string gender, int bornyear,int employeeClassification) : base(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:
        public override string toString()
        {
            return base.toString()+"\nClassification: "+getEmployeeClassification();
        }
        //overriding the calculateTotalSalary() method:
        public override double calculateTotalSalary()
        {
            double totalSalary = base.calculateTotalSalary();

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

        }
    }
}

Program Class for testing:

using Object_Oreineted_Example;

internal class Program
{
    private static void Main(string[] args)
    {
        Employee []empARR = new Employee[5];
        Console.WriteLine("welcome, now you will start entering data of 5 employees:");
        for (int i = 0; i < empARR.Length; i++)
        {
            Console.WriteLine("what is employee type? press 1 for bonus employee, press 2 for hourly employee:");
            Employee emp=new Employee();
            int choice = Convert.ToInt32(Console.ReadLine());
            if (choice == 1)
            {
                BonusEmployee bemp = new BonusEmployee(); 
                Console.WriteLine("enter employee classification(press 1 for manager, 2 for moderate manager, 3 for operater:");
                bemp.setEmployeeClassification(Convert.ToInt32(Console.ReadLine()));
                emp = bemp;
            }
            else if (choice == 2)
            {
                HourlyEmployee hemp = new HourlyEmployee(); 
                Console.WriteLine("enter employee hourly rate:");
                hemp.setHourlyRate(Convert.ToInt32(Console.ReadLine()));
                Console.WriteLine("enter employee extra worked hours:");
                hemp.setExtraWorkedHours(Convert.ToInt32(Console.ReadLine()));
                emp= hemp;
            }
            else
            {
                Console.WriteLine("invalid entry...you have to retry again...");
                i--;
                continue;
            }
            Console.WriteLine("enter employee id:");
            emp.setId(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("enter employee name:");
            emp.setName(Console.ReadLine());
            Console.WriteLine("enter basic salary:");
            emp.setBasicSalary(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("enter accomodation allowance:");
            emp.setAccomodationAllowance(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("enter food allowance:");
            emp.setfoodAllowance(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("enter employee's gender press m for a male or f for a female");
            string gender = Console.ReadLine();
            if (gender == "m")
                emp.setGender("male");
            else if (gender == "f")
                emp.setGender("female");
            else
            {
                Console.WriteLine("invalid entry...you have to retry again...");
                i--;
                continue;
            }
            Console.WriteLine("enter employee's born year: ");
            emp.setBornyear(Convert.ToInt32(Console.ReadLine()));
            empARR[i] = emp;
        }

        //printing the employees info including total salaries
        Console.WriteLine("Now printing employees information:\n======================");
        foreach (Employee emp in empARR)
        {
            Console.WriteLine(emp.toString());
            Console.WriteLine("\ntotal salary is : "+emp.calculateTotalSalary());
            Console.WriteLine("---------------------------");
        }
        Console.ReadKey();
    }
}

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