Wednesday, March 11, 2009

Value List Handler

Client - Displays the result
=============================
package com.ValueList.client;

import com.ValueList.controller.EmpController;


public class EmpClient {
public void result()
{
EmpController e = new EmpController();
e.getAllEmpInfo();
}

public static void main(String args[])
{
EmpClient e = new EmpClient();
e.result();
}

}
===============================================
Controller :
===============================================
Client Calls controller :

package com.ValueList.controller;

import com.ValueList.common.ValueListHandler;
import com.ValueList.model.EmpDBManager;

public class EmpController {

public void getAllEmpInfo()
{
EmpDBManager objEmpDBManager = new EmpDBManager();
ValueListHandler allempInfo = objEmpDBManager.empInfo();
Object empbean = allempInfo.getCurrentElement();
System.out.println("CURRENT ELEMENT ");
System.out.println("CURRENT ELEMENT "+allempInfo.getCurrentElement());
System.out.println("SIZE "+allempInfo.getSize());
System.out.println("HAS NEXT "+allempInfo.hasNext());
}
}
============================
package com.ValueList.common;

// Import List class
import java.util.List;

public interface ValueListHandler {
public void setList(List list);
public int getSize();
public boolean hasNext();
public Object nextElement() ;
public Object getCurrentElement();
public void resetIndex() ;
}
====================================
Employee Value List Handler:
===================================
package com.ValueList.handler;

// Import List class
import java.util.List;

import com.ValueList.common.ValueListHandler;

/**
* This class holds the list of orders. The presentation layer uses the
* methods from this class to display the Order information.
* This class implements the ValueListHandler interface, which provides the
* necessary methods to iterate through the list.
*/
public class EmployeeHandler implements ValueListHandler {

private List ordersList = null;
private int listIndex = -1;
private int allRecordsSize = -1;

/**
* Sets the Orders list
* @param list - Order List
*/
public void setList(List list) {
ordersList = list;
}

/**
* Returns the size of the list
* @return - Size
*/
public int getSize() {
return ordersList.size();
}

/**
* Returns true if there are more elements in the list
* @return - true/false
*/
public boolean hasNext() {
return ( (listIndex+1) < ordersList.size() && ordersList.size() > 0 ) ;
}

/**
* Returns the next element in the list
* @return Next element
*/
public Object nextElement() {
return ordersList.get(++listIndex) ;
}

/**
* Returns the current element in the list
* @return Current element
*/
public Object getCurrentElement() {
if ( listIndex < 0 || ordersList.size() < 1 ) {
return null;
}
return ordersList.get(listIndex) ;
}

/**
* Resets the index
*/
public void resetIndex() {
listIndex = -1;
}
}
=========================================================
/*
* Created on Oct 5, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.ValueList.model;

import java.util.ArrayList;

import com.ValueList.bean.EmployeeBean;
import com.ValueList.common.ValueListHandler;
import com.ValueList.handler.EmployeeHandler;

/**
* @author tzg136
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class EmpDBManager {

EmployeeHandler empValueListHandler = new EmployeeHandler();
public ValueListHandler empInfo()
{

ArrayList orderList = new ArrayList();

EmployeeBean oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("3939");
oEmployeeBean.setEmp_Name("ANDREW");
oEmployeeBean.setEmp_Sal("5.5LKS");

orderList.add(oEmployeeBean);

oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("1 3939");
oEmployeeBean.setEmp_Name("1 ANDREW");
oEmployeeBean.setEmp_Sal("1 5.5LKS");

orderList.add(oEmployeeBean);

oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("3 3939");
oEmployeeBean.setEmp_Name("3 ANDREW");
oEmployeeBean.setEmp_Sal("3 5.5LKS");

orderList.add(oEmployeeBean);


oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("4 3939");
oEmployeeBean.setEmp_Name("4 ANDREW");
oEmployeeBean.setEmp_Sal("4 5.5LKS");

orderList.add(oEmployeeBean);



oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("5 3939");
oEmployeeBean.setEmp_Name("5 ANDREW");
oEmployeeBean.setEmp_Sal("5 5.5LKS");

orderList.add(oEmployeeBean);


oEmployeeBean = new EmployeeBean();
oEmployeeBean.setEmp_ID("6 3939");
oEmployeeBean.setEmp_Name("6 ANDREW");
oEmployeeBean.setEmp_Sal("6 5.5LKS");

orderList.add(oEmployeeBean);


empValueListHandler.setList(orderList);

return empValueListHandler;
}

}
=======================================================

No comments: