<문제>
- 직원의 정보를 추가, 삭제, 갱신, 목록 조회, 조회하는 간단한 프로그램 작성
- Employee 클래스 : id, pwd, 이름, 이메일, 전화번호, 주민번호를 가짐.
- ManageService(Main) 클래스 : 직원 객체가 만들어지면 그 객체들을 다루는 메인 클래스 (추가, 삭제, 추가, 삭제, 갱신, 목록 조회, 선택조회의 기능을 수행)
public class Employee {
private String id;
private String pwd;
private String name;
private String email;
private String phone;
private String jumin;
//생성자
public Employee(String id, String pwd, String name, String email, String phone, String jumin) {
super();
this.id = id;
this.pwd = pwd;
this.name = name;
this.email = email;
this.phone = phone;
this.jumin = jumin;
}
//게터 세터
public String getId() {
return id;
}
public String getPwd() {
return pwd;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getJumin() {
return jumin;
}
public void setId(String id) {
this.id = id;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setJumin(String jumin) {
this.jumin = jumin;
}
//toString메서드
@Override
public String toString() {
return "Employee [id=" + id + ", pwd=" + pwd + ", name=" + name + ", email=" + email + ", phone=" + phone
+ ", jumin=" + jumin + "]";
}
//hashCode메서드
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
//equals메서드
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
import java.util.ArrayList;
public class ManageService {
private static ArrayList<Employee> emps = new ArrayList<Employee>();
public static void main(String[] args) {
//직원 객체 생성
Employee kth = new Employee("0001", "1234", "kim",
"k@gmail.com", "01033334444", "811225-1234567");
Employee k = new Employee("0002", "1200", "min",
"m@gmail.com", "01032234444", "821225-7654321");
//직원 등록
insert(kth);
insert(k);
String id = "0002";
//직원 조회
Employee emp1 = select(id);
System.out.println(emp1);
//직원 정보 갱신
//폰 번호 갱신
emp1.setPhone("01011112222");
boolean result = update(id, emp1);
System.out.println("직원["+id+"] 정보가 수정되었습니다."+"\n결과 : "+result);
emp1 = select(id);
//데이터는 다시 불러와서 확인해야 함. (웹서비스에서 데이터값이 어떻게 변화할지 모르기 때문)
System.out.println("\n 바뀐 정보 : "+emp1);
//직원 삭제
result = delete(id);
System.out.println("직원 삭제 수행 완료 :: "+result);
//총 직원 목록
ArrayList<Employee> someones = selectAll();
System.out.println("회사의 총 인원 수 : ["+emps.size()+"명]");
System.out.println("검색한 인원 수 : ["+someones.size()+"명]");
}
//사원 정보 업데이트
private static boolean update(String id, Employee emp1) {
int i=0;
for(i=0; i<emps.size(); i++) {
Employee emp = emps.get(i);
if(emp.getId().equals(id)) {
emps.set(i,emp1);
return true;
}else
continue;
}
return false;
}
// 전체 사원 수 목록 보여주기
private static ArrayList<Employee> selectAll() {
return emps;
}
//사원 정보 삭제
private static boolean delete(String id) {
int i=0;
for(i=0; i<emps.size(); i++) {
Employee emp = emps.get(i);
if(emp.getId().equals(id)) {
emps.remove(i);
return true;
}else
continue;
}
return false;
}
//사원 조회
private static Employee select(String id) {
int i=0;
for(i=0; i<emps.size(); i++) {
Employee emp = emps.get(i);
if(emp.getId().equals(id)) {
System.out.println("select("+id+") :: emp : "+emp);
System.out.println("\t emp's id : "+emp.getId());
return emp;
}else {
continue;
}
}
return null;
}
//사원 등록
private static void insert(Employee kth) {
emps.add(kth);
}
}
'연습문제 및 실습 자료' 카테고리의 다른 글
스프링 MVC 예제 - Mouse목록 조회, 생성 (0) | 2023.10.25 |
---|---|
Socket 흐름 예제 (0) | 2023.08.24 |
재귀함수 호출 연습문제 (0) | 2023.08.22 |
정렬 만들기 (0) | 2023.08.22 |
쓰레드 2 (0) | 2023.08.21 |