Tuesday, January 25, 2011

OOP… Get to the actual work now. Part-II (Using import statement)

In this example we will use the import statement to access the classes declared inside the package. With this we can directly use the class name as its in the same directory (without pakage_name.class_name).


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 BasicOOPTest2 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;

}

}


BasicOOPTest2.java

import payroll.Employee;

import payroll.SalesPerson;


class BasicOOPTest2{

private static double averageIncome(Employee[] employees){

double total = 0;

for(Employee emp : employees){

total += emp.getNetIncome();

}

return total / employees.length;

}


private static double totalSales(Employee[] employees){

double total = 0;

for(Employee emp : employees){

if(emp instanceof SalesPerson){

SalesPerson sp = (SalesPerson) emp;

total += sp.getSales();

}

}

return total;

}


public static void main(String[] args){

Employee[] dept = {

new Employee(185, 52),

new Employee(205, 225),

new SalesPerson(180, 50, 60000),

new Employee(170, 95),

new SalesPerson(165, 45, 40000)

};

System.out.printf("Average Income: %.2f%n", averageIncome(dept));

System.out.printf("Total Sales: %.2f%n", totalSales(dept));

}

}

To compile:

javac BasicOOPTest2.java

To run:

java BasicOOPTest2

No comments: