官术网_书友最值得收藏!

Implementing the EmployeeStoreManager class

The EmployeeStoreManager class is responsible for connecting with the data source as well as reading and writing employee data:

import com.packt.javaee8.entity.Employee;

import java.util.HashMap;
import java.util.Map;

public class EmployeeStoreManager {

private Map<Object, Employee> dataSource = new HashMap<>();

public void storeNew ( Employee employee ) throws Exception {

if( dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data already exist" );

dataSource.put( employee.getId(), employee );
}

public void update ( Employee employee ) throws Exception {

if( !dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data not exist" );

dataSource.put( employee.getId(), employee );
}

public void delete ( Employee employee ) throws Exception {

if( !dataSource.containsKey( employee.getId() ) ) throw new Exception( "Data not exist" );

dataSource.remove( employee.getId() );

}

public Employee load(Object key){

return dataSource.get( key );

}


}

In the preceding code block, we have the EmployeeStoreManager class, which is responsible for connecting with the data source and reading and writing employee data. This class works as a DAO and encapsulates all the data source complexity. It also has the dataSource attribute, which is a map that represents a data source. Furthermore, this class has the storeNew method, which is used to write new employee data represented by the object model. It also has the update method, used to write existing employee data represented by an object model. This method is used to update stored data. The delete method is also used to delete existing employee data, and the load method is used to read employee data in an object model.

主站蜘蛛池模板: 广南县| 抚州市| 色达县| 淅川县| 五大连池市| 民权县| 合阳县| 稷山县| 新昌县| 青河县| 台州市| 新郑市| 万州区| 江孜县| 喀喇沁旗| 金湖县| 芒康县| 城固县| 霍城县| 刚察县| 右玉县| 溧阳市| 宜昌市| 张家界市| 成都市| 玉林市| 临邑县| 大连市| 永登县| 宁阳县| 惠来县| 旬阳县| 广饶县| 巢湖市| 于田县| 道真| 渝中区| 文昌市| 穆棱市| 玛纳斯县| 吉水县|