member array
MemberMain
package memberArray;
import java.util.Scanner;
public class MemberMain {
// member 저장 배열
static MemberVo[] members = new MemberVo[0];
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("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();
ms.process(action, sc);
break;
case "3":
System.out.println("회원 정보수정");
action = new UpdateAction();
ms.process(action, sc);
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 memberArray;
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(id, pw, name, age);
MemberVo[] temp = new MemberVo[MemberMain.members.length+1];
for(int i=0;i deleteIndex){
temp[i-1] = members[i];
}else {
temp[i] = members[i];
}
}
MemberMain.members = temp;
}else {
System.out.println("삭제할 ID가 없습니다.");
}
}
}