member list
MemberMain
package memberArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MemberMain {
// member 저장 배열
static List<MemberVo> members = new ArrayList<MemberVo>();
public static void main(String[] args) {
MemberService ms = new MemberService();
Action action = null;
Scanner sc = new Scanner(System.in);
boolean isStop = false;
do {
System.out.println("메뉴를 선택하세요.");
System.out.println("1. 회원 가입");
System.out.println("2. 회원 목록보기");
System.out.println("3. 회원 정보수정");
System.out.println("4. 회원 정보삭제");
System.out.println("5. 종료");
String command = sc.next();
switch (command) {
case "1":
System.out.println("회원 가입");
action = new AddAction();
ms.process(action, sc);
break;
case "2":
System.out.println("회원 목록보기");
action = new ListAction();
action.execute(sc);
//ms.process(action, sc);
break;
case "3":
System.out.println("회원 정보수정");
action = new UpdateAction();
ms.process(action, sc);
break;
case "4":
System.out.println("회원 정보삭제");
System.out.println(members);
action = new DeleteAction();
ms.process(action, sc);
System.out.println(members);
break;
case "5":
System.out.println("5");
isStop = true;
}
}while(!isStop);
}
}
MemberService
- 요청을 제어하는 서비스
- 실질적인 비즈니스 로직을 정의하는 Service
import java.util.Scanner;
public class MemberService {
void process(Action action,Scanner sc){
action.execute(sc);
}
}
MemberVo
package memberArray;
public class MemberVo {
private String id;
private String pw;
private String name;
private int age;
public MemberVo() {
}
public MemberVo(String id, String pw, String name, int age) {
super();
this.id = id;
this.pw = pw;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "MemverVo [id=" + id + ", pw=" + pw + ", name=" + name + ", age=" + age + "]";
}
}
Action
- 요청 처리 규격을 정의(인터페이스)
- 다형성 지원을 위해 사용
import java.util.Scanner;
public interface Action {
void execute(Scanner sc);
}
각 액션들
AddAction
package memberArrayList;
import java.util.Scanner;
public class AddAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("회원 정보 입력");
System.out.print("ID :");
String id = sc.next();
System.out.print("PW :");
String pw = sc.next();
System.out.print("NAME :");
String name = sc.next();
System.out.print("AGE :");
int age = sc.nextInt();
MemberVo member = new MemberVo();
member.setId(id);
member.setPw(pw);
member.setName(name);
member.setAge(age);
MemberMain.members.add(member);
}
}
ListAction
package memberArrayList;
import java.util.List;
import java.util.Scanner;
public class ListAction implements Action{
@Override
public void execute(Scanner sc) {
List<MemberVo> members = MemberMain.members;
System.out.printf("=== Member Count %s명 === \n",members.size());
for(int i=0;i<members.size();i++) {
System.out.print("ID: " + members.get(i).getId() +" / ");
System.out.print("PW: " + members.get(i).getPw() +" / ");
System.out.print("Name: " + members.get(i).getName() +" / ");
System.out.print("Age: " + members.get(i).getAge() );
System.out.println();
}
System.out.printf("=== Member Count %s명 ===\n",members.size());
for(MemberVo m: members) {
System.out.print("ID: " + m.getId() +" / ");
System.out.print("PW: " + m.getPw() +" / ");
System.out.print("Name: " + m.getName() +" / ");
System.out.print("Age: " + m.getAge() );
System.out.println();
}
}
}
UpdateAction
package memberArrayList;
import java.util.List;
import java.util.Scanner;
public class UpdateAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("회원 정보 수정");
// 수정할 id 찾기
System.out.print("수정할 ID :");
String id = sc.next(); // 1111
MemberVo member = null;
List<MemberVo> members = MemberMain.members;
for(MemberVo m:members) {
if(m.getId().equals(id)) {
member = m;
break;
}
}
// id 있으면 PW,NAME,AGE 수정
if(member != null) {
System.out.println("기존 PW : " + member.getPw() );
System.out.print("수정 PW :");
String pw = sc.next();
System.out.printf("기존 NAME : %s%n", member.getName());
System.out.print("수정 NAME :");
String name = sc.next();
System.out.printf("기존 AGE %s%n", member.getAge());
System.out.print("수정 AGE :");
int age = sc.nextInt();
// 1. setXXX()
member.setPw(pw);
member.setName(name);
member.setAge(age);
}else {
System.out.println("없는 ID입니다.");
}
}
}
DeleteAction
package memberArrayList;
import java.util.List;
import java.util.Scanner;
public class DeleteAction implements Action{
@Override
public void execute(Scanner sc) {
// id 찾기
System.out.print("삭제할 ID :");
String id = sc.next(); // 1111
MemberVo member = null;
List<MemberVo> members = MemberMain.members;
int deleteIndex = -1;
for(int i=0;i<members.size();i++) {
if(members.get(i).getId().equals(id)) {
deleteIndex = i;
break;
}
}
if(deleteIndex != -1) {
//add(), get(), remove(int index), remove(Object o)
members.remove(deleteIndex);// 0, 0
}else {
System.out.println("삭제할 ID가 없습니다.");
}
}
}