자바 ArrayList와 HashMap의 CRUD

ednadev·2020년 7월 16일
0

자바

목록 보기
18/22

VO

MyDate 클래스

public class MyDate {
    private int year;
    private int month;
    private int day;
    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public String toString() {
        return year + "-" + month + "-" + day;
    }
}

Employee 클래스

public class Employee {
    private int ssn;
    private String name;
    private MyDate birthDate;
    private double salary;
    
    //ArrayList
    public Employee(String name, MyDate birthDate, double salary) {
        this.name = name;
        this.birthDate = birthDate;
        this.salary = salary;
    }
    //HashMap
    public Employee(int ssn, String name, MyDate birthDate, double salary) {
        this.ssn = ssn;
        this.name = name;
        this.birthDate = birthDate;
        this.salary = salary;
    }
    
    public int getSsn() {
        return ssn;
    }
    public void setSsn(int ssn) {
        this.ssn = ssn;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public MyDate getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(MyDate birthDate) {
        this.birthDate = birthDate;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    @Override
    public String toString() {
        return name + " " + birthDate + " " + salary;
    }
}

Manager 클래스

public class Manager extends Employee {
    private String dept;
    private int deptno;
    
    //ArrayList
    public Manager(String name, MyDate birthDate, 
    		double salary, String dept, int deptno) {
        super(name, birthDate, salary);
        this.dept = dept;
        this.deptno = deptno;
    }
    //HashMap
    public Manager(int ssn, String name, MyDate birthDate, 
    		double salary, String dept, int deptno) {
        super(ssn, name, birthDate, salary);
        this.dept = dept;
        this.deptno = deptno;
    }
    
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    
    @Override
    public String toString() {
        return super.toString() + " " + dept + " " + deptno;
    }
}

Engineer 클래스

public class Engineer extends Employee{
    private String tech;
    private double bonus;

    //ArrayList
    public Engineer(String name, MyDate birthDate, double salary, 
    			String tech, double bonus) {
	super(name, birthDate, salary);
	this.tech = tech;
	this.bonus = bonus;
    }
    //HashMap
    public Engineer(int ssn, String name, MyDate birthDate, 
    			double salary, String tech, double bonus) {
	super(ssn, name, birthDate, salary);	
	this.tech = tech;
	this.bonus = bonus;
    }
		
    public String getTech() {
	return tech;
    }
    public void setTech(String tech) {
	this.tech = tech;
    }
    public double getBonus() {
	return bonus;
    }
    public void setBonus(double bonus) {
	this.bonus = bonus;
    }

    @Override
    public String toString() {		
	return super.toString() + " " + tech + " " + bonus;
    }
}

Service

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class EmployeeService {
    //추가...
    //ArrayList
    //private ArrayList<Employee> list;
    //HashMap
    private HashMap<Integer, Employee> map;
	
    //싱글톤
    private static EmployeeService service = new EmployeeService();	
    private EmployeeService(){
        //ArrayList
        //list = new ArrayList<>();
        //HashMap
        map = new HashMap<>();
    }
    public static EmployeeService getInstance() {
	return service;
    }
	
    //추가
    public void getEmployee() {
        //ArrayList
        //for(Employee e : list) System.out.println(e);
	//HashMap
        Set<Integer> set = map.keySet();
	/*for(int key : set) {
	    System.out.println(map.get(key));
	}*/
	Iterator<Integer> it = set.iterator();
	while(it.hasNext()) {
	    Integer key = it.next();
	    System.out.println(map.get(key));
	}
    }	
	
    //추가 조건
    public void addEmployee(Employee e) {	
        //ArrayList
        /*boolean flag = false;
	for(Employee emp : list) {
            //이미 그런 사람이 있다면..
	    if(e.getName().equals(emp.getName())) { 
		flag = true;
		System.out.println(e.getName()
        			+ " 님은 이미 회원이십니다.");
		return;
	    }
	}
	if(flag==false) { //추가하고자 하는 사람은 기존의 리스트에는 없다
	    list.add(e);
	    System.out.println(e.getName()
        			+ " 님이 회원으로 가입되셨습니다.");
	}*/
        //HashMap
	/*boolean flag = false;
	Set<Integer> set = map.keySet();
	for(int key : set) {
	    if(map.get(key).getName().equals(e.getName())) {
		flag = true;
		System.out.println(e.getName()
        			+ " 님은 이미 회원이십니다.");
		return;
	    }
	}
	if(flag==false) {
	    map.put(e.getSsn(), e);
	    System.out.println(e.getName()
        			+ " 님이 회원으로 가입되셨습니다.");
	}*/
	if(map.containsKey(e.getSsn())) {
	    System.out.println(e.getName() + " 님은 이미 회원이십니다");
	    return;
	}else {
	    map.put(e.getSsn(), e);
	    System.out.println(e.getName()
        			+ " 님이 회원으로 가입되셨습니다.");
	}
    }	
	
    //삭제 조건
    //ArrayList
    public void deleteEmployee(String name) {
        /*boolean find = false;
	for(Employee e : list) {
	    if(e.getName().equals(name)) { //삭제하려는 대상이 있다면...
		find = true;
		System.out.println(e.getName()
        			+ " 님이 삭제되셨습니다.");
		list.remove(e);
		break;
	    }
	}
	if(find==false) {
		System.out.println("삭제할 대상이 존재하지 않습니다.");
	}*/
    }
    //HashMap
    public void deleteEmployee(int ssn) {	
	/*boolean flag = false;
	Set<Integer> set = map.keySet();
	for(int key : set) {
	    if(key == ssn) {
		flag = true;
		System.out.println(map.get(key).getName()
        			+ " 님이 삭제되셨습니다.");
		map.remove(key);
		break;
	    }
	}
	if(flag == false) {
	    System.out.println("삭제할 대상이 존재하지 않습니다.");
	}*/
	Employee emp = map.remove(ssn);
	if(emp == null) { //삭제할 대상이 존재하지 않는다.
	    System.out.println("삭제할 대상이 존재하지 않습니다.");
	    return;
	}else {
	    System.out.println(emp.getName()
        			+ " 님이 삭제되셨습니다.");
	}
    }	
	
    //수정 조건
    //ArrayList
    public void updateEmployee(Employee e) {
	/*boolean flag = false;
	for(int i=0; i<list.size(); i++) {
	    if(list.get(i).getName().equals(e.getName())) {
		list.set(i, e);
		System.out.println(e.getName()
        			+ " 님의 정보가 수정되었습니다.");
		flag = true;
	    } 
	}
	if(flag==false) {
	    System.out.println("수정할 대상이 존재하지 않습니다.");
	    return;
	}*/
    }
    //HashMap
    public void updateEmployee(Employee e, int ssn) {
	if(map.containsKey(ssn)) {
	    //map.put(ssn, e);
	    map.replace(ssn, e);
	    System.out.println(e.getName()
        			+ " 님의 정보가 수정되었습니다.");
	}else {
	    System.out.println("수정할 대상이 존재하지 않습니다.");
	    return;
	}
    }	
	
    public ArrayList<Employee> findEmployee(double salary) {
	ArrayList<Employee> temp = new ArrayList<>();
        //ArrayList
        /*for(Employee em : list) {
	    if(em.getSalary() == salary) {
		temp.add(em);
	    }
	}*/
        //HashMap
	Set<Integer> set = map.keySet();
	/*for(int key : set) {
	    if(map.get(key).getSalary() == sal) {
		temp.add(map.get(key));
	    }
	}*/
	Iterator<Integer> it = set.iterator();
	while(it.hasNext()) {
	    int key = it.next();
	    Employee e = map.get(key);
	    if(e.getSalary()==salary) temp.add(e);
	}
	return temp;
    }	
}

TEST

public class EmployeeServiceTest {
    public static void main(String[] args) {
	//1. Service 생성된 객체를 하나 받아온다..
	EmployeeService service = EmployeeService.getInstance();

	//2. method를 각각 호출..
	System.out.println("======================== 1. addEmployee ================================== ");
    
        //ArrayList
        /*Manager m1 = new Manager("이수근", new MyDate(1978, 1, 1), 20000.0, "개발부", 10);
	Manager m2 = new Manager("강호동", new MyDate(1975, 2, 1), 34000.0, "기획부", 20);
	Manager m3 = new Manager("강하늘", new MyDate(1980, 3, 3), 20000.0, "교육부", 30);
	Engineer eg1 = new Engineer("피오", new MyDate(1995,3,4), 350000.0, "Java", 200.0);
	Engineer eg2 = new Engineer("김동현", new MyDate(1995,7,2), 350000.0, "Java", 200.0);*/
        
        //HashMap
	Manager m1 = new Manager(111,"이수근", new MyDate(1978, 1, 1), 20000.0, "개발부", 10);
	Manager m2 = new Manager(222, "강호동", new MyDate(1975, 2, 1), 34000.0, "기획부", 20);
	Manager m3 = new Manager(333, "강하늘", new MyDate(1980, 3, 3), 20000.0, "교육부", 30);
	Engineer eg1 = new Engineer(444,"피오", new MyDate(1995,3,4), 350000.0, "Java", 200.0);
	Engineer eg2 = new Engineer(555, "김동현", new MyDate(1995,7,2), 350000.0, "Java", 200.0);

	service.addEmployee(m1);
	service.addEmployee(m2);
	service.addEmployee(m2);	
	service.addEmployee(m3);
	service.addEmployee(eg1);
	service.addEmployee(eg2);

	service.getEmployee();

	System.out.println("======================== 2. deleteEmployee =============================== ");
	
        //ArrayList
        //service.deleteEmployee("피오");
        
        //HashMap
        service.deleteEmployee(444);
	service.getEmployee();

	System.out.println("======================== 3. updateEmployee =============================== ");
	service.updateEmployee(new Manager(222,"강호동", new MyDate(1975, 2, 1), 34000.0, "총무부", 200), 222);
	service.getEmployee();

	System.out.println("======================== 4. findEmployee ================================= ");
	ArrayList<Employee> retList = service.findEmployee(20000.0);
	System.out.println(retList);
    }
}

정렬이 반대라는 점을 제외하고 ArrayList와 HashMap 출력 결과는 동일하다.

0개의 댓글