MemberMain
package memberFile;
import java.util.Scanner;
public class MemberMain {
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("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);
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 memberFile;
import java.io.FileWriter;
import java.io.IOException;
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();
String member = id;
member += ("," + pw);
member += ("," + name);
member += ("," + age);
member += ("\r\n");
FileWriter fw = null;
try {
fw = new FileWriter("members.txt", true); // true : append
fw.write(member);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ListAction
package memberFile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ListAction implements Action{
@Override
public void execute(Scanner sc) {
FileReader fr = null;
BufferedReader bfr = null;
StringTokenizer st = null;
try {
fr = new FileReader("members.txt");
bfr = new BufferedReader(fr);
String member = "";
while((member = bfr.readLine()) != null){
st = new StringTokenizer(member, ",");
String fm = "ID : %s, PW : %s, Name : %s, Age : %s %n";
System.out.printf(fm, st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}