0% found this document useful (0 votes)
22 views

Employee Oop Example

Uploaded by

mosesdray15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Employee Oop Example

Uploaded by

mosesdray15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming Example 1:

Problem: To write an object program the prints an employee payslip and the company's employee
wage Bill
Key Highlights.
1. Class Employee
2. All getter and setter functions
3. A Method to calculate tax of an employee
4. A Method to calculate gross earning of an employee
5. A Method to calculate print employees paySlip
6. A Method to calculate print the entire wage bill of an organization

System Architecture
MAIN
Employee Emp
Employee myEmp[]

setAllowances()
setEmployeeDetails()

printWageBill(Emp [])
printPaySlip(Emp)

What is An Employee Payslip?


• A pay slip show employee monthly earnings and deductions.
• See the attached sample from CUEA

What is An Employee wage bill?


An Employee wage bill is a list of records showing employees monthly earnings in the format

Number, Full Name Salary Allowances Gross Payee Net


1 001000 James Okongo 45000 20000 65000 12000 53000
2
3
4
5
import java.util.Scanner;
class Employee{
Scanner scan=new Scanner(System.in);
String name;
String dob;
String empnum;
String position;
String dpt;
String gender;
float salary;
float hsallowance;
float travallowance;
//Get and Set Methods
public void setAllwances(){
hsallowance=(float)0.5*salary;
travallowance=(float)0.1*salary;
}
//getters
public float getHseAllowance(){ return hsallowance; }
public float getTravAllowance(){ return travallowance; }
public float getSalary(){ return salary; }
public String getName(){ return name; }
public String getNumber(){ return empnum; }
public String getPosition(){ return position; }
public String getDpt(){ return dpt; }
public String getDob(){ return dob; }
public String getGender(){ return gender; }
//setters
public void setName(String nm){ name=nm; }
public void setSalary(float s){ salary=s; }
public void setEmpNum(String num){ empnum=num; }
public void setDpt(String dpt){ this.dpt=dpt; }
public void setDob(String db){ dob=db; }
public void setGender(String gd){ gender=gd; }
// A method to set Employee Details
public void setDetails(){
System.out.println("Enter Employee name");
name=scan.nextLine();
System.out.println("Enter Employee Number");
empnum=scan.next();
System.out.println("Enter Employee Position");
position=scan.next();
System.out.println("Enter Employee Salary");
salary=scan.nextFloat();
System.out.println("Enter Employee Department");
dpt=scan.nextLine();
System.out.println("Enter Employee Date of Birth");
dob=scan.next();
}
// A method to Print Employee Details
public void printEmployee(){
System.out.println(empnum+"\t"+name+"\t"+position+"\t"+dpt);
}
}//End of class Employee

//Main Driver Class


public class EmployeeMain{
public static void main(String [] args){
Scanner scan=new Scanner(System.in);
//First Example we illustrating with a single employee
//Employee Emp=new Employee();
//Emp.setDetails();
//Emp.setAllwances();
//printPaySlip(Emp);
//Second we illustrating with a set of employees
// Creating an array of employees myemps
Employee myemps[]=new Employee[5];
//Populating the array
for(int i=0;i<myemps.length;i++){
myemps[i]=new Employee();
myemps[i].setDetails();
myemps[i].setAllwances();
}//end of for loop
printWageBill(myemps);
}// End of main method

// A method to get Employee Gross Pay


public static float getGross(Employee E){
return E.getSalary()+E.getHseAllowance()+E.getTravAllowance();
}

// A method to get Employee Tax


public static float getTax(Employee E){
float gross=getGross(E);
float tax=0;
if(gross<=10000)
tax=0;
else
if(gross<=25000)
tax=1000+10/100*(gross-10000);
else
if(gross<=35000)
tax=1500+ 20/100*(gross-25000);
else
if(gross<=45000)
tax=3500+ 30/100*(gross-35000);
else
tax=6500+ 40/100*(gross-45000);
return tax;
}

// A method to print Employee Pay Slip


public static void printPaySlip(Employee E){
System.out.println("Catholic University of Eastern Africa");
System.out.println("Employee Monthly Pay Slip\t Month :\t"+ getMonth());
System.out.println("Employee Number"+E.getNumber()+"\tFull Name\t"+E.getName()
+"\tDate:\t"+getDate());
System.out.println("Position\t"+E.getPosition()+"\tDepartement\t"+E.getDpt());
System.out.println("Basic Salary\t\t"+E.getSalary());
System.out.println("House Allowance\t\t"+E.getHseAllowance());
System.out.println("Travelling Allowance\t\t"+E.getTravAllowance());
System.out.println("Gross Income\t\t"+getGross(E));
System.out.println("Paye\t\t"+getTax(E));
System.out.println("Net Income\t\t"+(getGross(E)-getTax(E)));
}// End of method

// A method to print Company Employee Wage Bill


//It receives an array of Employees
public static void printWageBill(Employee []Emps){
System.out.println("Catholic University of Eastern Africa");
System.out.println("Employee \t"+ getMonth()+"\t Wage Bill\tDate:\t"+getDate());
System.out.println("Employee Number\tFull Name\t Gross \tPaye\tNet");
float sumgross=0;
float sumtax=0;
for(int i=0;i<Emps.length;i++){
float net=getGross(Emps[i])-getTax(Emps[i]);
sumgross+=getGross(Emps[i]);
sumtax+=getTax(Emps[i]);
System.out.println(Emps[i].getNumber()+"\t"+Emps[i].getName()+"\t"+getGross(Emps[i])
+"\t"+getTax(Emps[i])+"\t"+net);
}
System.out.println("\t\tTotal Gross\t"+sumgross+"Total Paye\t"+sumtax);
}//End of printWageBill

// A method to Return Current Month


public static String getMonth(){
String
months[]={"January","February","March","April","May","June","July","August","September","Octobe
r","November","December"};
Date dt= new Date();
GregorianCalendar gc=new GregorianCalendar();
int year=gc.get(Calendar.YEAR);
int month=gc.get(Calendar.MONTH);
int dte=gc.get(Calendar.DATE);
String mon=months[month];
return mon;
}

// A method to Return Current Date


public static String getDate(){
Date dt= new Date();
GregorianCalendar gc=new GregorianCalendar();
int year=gc.get(Calendar.YEAR);
int month=gc.get(Calendar.MONTH);
int dte=gc.get(Calendar.DATE);
String date=dte+"/"+month+"/"+year;
return date;
}

// A method to Return Time now


public static String getTime(){
Date dt= new Date();
GregorianCalendar gc=new GregorianCalendar();
int hour=gc.get(Calendar.HOUR);
int min=gc.get(Calendar.MINUTE);
int sec=gc.get(Calendar.SECOND);
String tm=hour+":"+min+":"+sec;
return tm;
}
}// End of class EmployeeMain

You might also like