Tuesday, January 25, 2011

OOP… Get to the actual work now.

The main reason why the java is invented or I could say the soul of java. Its Object oriented nature. Let’s look now at Object Oriented Programming. Also now we will see how to create package in java.

This example will demonstrate simple payroll system.

  1. Create folder with name “payroll”
  2. Create class “Employee” in payroll folder which will have logic to calculate salary for Employee.
  3. Create class “SalesPerson” in payroll folder which will have logic to calculate salary for Sales Person.
  4. Now come out of the payroll folder. And create BasicOOPTest1 class to test the code.

Employee.java

package payroll;

public class Employee{
private int id;
float rate;
protected int hours;
private static int count;

public Employee(int h, float r){
id = 101 + count++;
hours = h;
rate = r;
}

public Employee(){
this(0, 50);
}

public Employee(int i, int h, float r){
id = i;
hours = h;
rate = r;
}

public int getHours(){
return hours;
}

public void setHours(int value){
hours = value;
}

public float getRate(){
return rate;
}

public void setRate(float value){
rate = value;
}

public int getId(){
return id;
}

public double getNetIncome(){
double income = hours * rate;
int ot = hours - 180;
if(ot > 0)
income += 50 * ot;
return income;
}

public static int countEmployees(){
return count;
}

public int hashCode(){
return id;
}

public boolean equals(Object other){
if(other instanceof Employee){
Employee that = (Employee) other;
return (id == that.id) && (hours == that.hours) && (rate == that.rate);
}
return false;
}

public String toString(){
return id + " " + hours + " " + rate;
}

public void finalize(){
System.out.printf("Employee:%d finalized%n", id);
}
}

SalesPerson.java

package payroll;

public class SalesPerson extends Employee{
private double sales;

public SalesPerson(int h, float r, double s){
super(h, r);
sales = s;
}

public double getSales(){
return sales;
}

public void setSales(double value){
sales = value;
}

public double getNetIncome(){
double income = super.getNetIncome();
if(sales >= 25000)
income += 0.05 * sales;
return income;
}
}

BasicOOPTest1.java

class BasicOOPTest1{
private static double incomeTax(payroll.Employee emp){
double i = emp.getNetIncome();
return (i <= 10000) ? 0 : 0.15 * (i - 10000);
}

public static void main(String[] args){
payroll.Employee jill = new payroll.Employee();
jill.setHours(185);
jill.setRate(52);
payroll.SalesPerson jack = new payroll.SalesPerson(185, 52, 80000);
System.out.printf("Jill's ID is %d, Income is %.2f and Tax is %.2f%n",
jill.getId(), jill.getNetIncome(), incomeTax(jill));
System.out.printf("Jack's ID is %d, Income is %.2f and Tax is %.2f%n",
jack.getId(), jack.getNetIncome(), incomeTax(jack));
System.out.printf("Number of Employees: %d%n",
payroll.Employee.countEmployees());
}
}

When we compile BasicOOPTest1.java the java compiler will automatically compiles Employee.java and SalesPerson.java files for you.

To compile:

javac BasicOOPTest1.java

To run:

java BasicOOPTest1

No comments: