input output
MemberMain
package hellomemberfile;
import java.util.Scanner;
public class MemberMain {
// 저장 > File ./data/member.txt
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
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();
MemberService ms = new MemberService();
Action action = null;
switch (command) {
case "1":
action = new AddAction();
ms.process(action, sc);
break;
case "2":
action = new ListAction();
ms.process(action, sc);
break;
case "3":
action = new UpdateAction();
ms.process(action, sc);
break;
case "4":
action = new DeleteAction();
ms.process(action, sc);
break;
}
if(command.equals("5")) break;
}
}
}
MemberService
- 요청을 제어하는 서비스
- 실질적인 비즈니스 로직을 정의하는 Service
package hellomemberfile;
import java.util.Scanner;
public class MemberService {
void process(Action action, Scanner sc) {
action.execute(sc);
}
}
MemberVo
package hellomemberfile;
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 id+"," + pw + "," + name + "," + age ;
}
}
Action
- 요청 처리 규격을 정의(인터페이스)
- 다형성 지원을 위해 사용
package hellomemberfile;
import java.util.Scanner;
public interface Action {
void execute(Scanner sc);
}
각 액션들
AddAction
package hellomemberfile;
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("AddAction");
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();
//System.out.printf("%s,%s,%s,%s\n",id,pw,name,age);
String data = "";
//1. MemberVO 를 사용한 저장 data
MemberVO member = new MemberVO(id, pw, name, age);
data = member.toString()+"\n";
//System.out.println(data);
//2. 문자열 덧셈 data
String de = ",";
data = id + de + pw + de + name + de + age +"\n" ;
//System.out.println(data);
//3. String.join()
String[] datas = new String[4];
datas[0] = id;
datas[1] = pw;
datas[2] = name;
datas[3] = age+"";
data = String.join(",", datas)+"\n";
//System.out.println(data);
// File Save
String file = "./data/member.txt";
FileWriter fw = null;
try {
fw = new FileWriter(file, true); //true : append
fw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ListAction
package hellomemberfile;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ListAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("ListAction");
// File Read -> List
// File 회원 정보 -> List
FileReader fr = null;
BufferedReader br = null;
String file = "./data/member.txt";
List members = new ArrayList();
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = null;
while((str = br.readLine()) != null) {
members.add(str);
}
} catch (IOException e) {
e.printStackTrace();
}
// List -> Print
System.out.println("현재 회원수 : " + members.size());
// ID:hong, PW:1111, Name:홍길동, Age:11
for(String data : members) {
System.out.println(data);
}
for(String data: members) {
String[] dArr = data.split(",");
String id = "ID:" + dArr[0];
String pw = "PW:" + dArr[1];
String name = "Name:" + dArr[2];
String age= "Age:" + dArr[3];
String[] mem = {id,pw,name,age};
String sMem = String.join("*",mem);
System.out.println(sMem);
}
}
}
UpdateAction
package hellomemberfile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UpdateAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("UpdateAction");
System.out.println("== 회원 정보 수정 ==");
// File 회원 정보 -> List
FileReader fr = null;
BufferedReader br = null;
String file = "./data/member.txt";
List rData = new ArrayList();
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = null;
while((str = br.readLine()) != null) {
rData.add(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//List rData ==> List members
List members = new ArrayList();
MemberVO member = null;
// String ==> MemberVO ==> add
for(String data: rData) {
MemberVO m = null;
String[] dArr = data.split(",");
String id = dArr[0];
String pw = dArr[1];
String name = dArr[2];
int age = Integer.parseInt(dArr[3]);
String email = dArr[4];
String tel = dArr[5];
m = new MemberVO(id, pw, name, age, email, tel);
members.add(m);
}
// 수정 ID 찾기
System.out.print("수정할 ID >> "); // 33
String id = sc.next();
for(MemberVO m : members) {
if(m.getId().equals(id)) {;
member = m;
}
}
if(member == null) {
System.out.println("Member Not Found!!");
}else {
// 수정할 데이터 입력
System.out.println("기존 PW >> " + member.getPw());
System.out.print("PW >> ");
String pw = sc.next();
System.out.println("기존 Name >> " + member.getName());
System.out.print("Name >> ");
String name = sc.next();
System.out.println("기존 Age >> " + member.getAge());
System.out.print("Age >> ");
int age = sc.nextInt();
//System.out.printf("%s,%s,%s,%s\n",id,pw,name,age);
// 수정 작업
member.setPw(pw);
member.setName(name);
member.setAge(age);
// membesr List => File Write
FileWriter fw = null;
try {
fw = new FileWriter(file);
for(MemberVO m : members) {
String data = m.toString()+"\n";
fw.write(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
DeleteAction
package hellomemberfile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DeleteAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("DeleteAction");
System.out.println("== 회원 정보 삭제 ==");
// File 회원 정보 -> List
FileReader fr = null;
BufferedReader br = null;
String file = "./data/member.txt";
List rData = new ArrayList();
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = null;
while((str = br.readLine()) != null) {
rData.add(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//List rData ==> List members
List members = new ArrayList();
MemberVO member = null;
// String ==> MemberVO ==> add
for(String data: rData) {
String[] dArr = data.split(",");
String id = dArr[0];
String pw = dArr[1];
String name = dArr[2];
int age = Integer.parseInt(dArr[3]);
member = new MemberVO(id, pw, name, age);
members.add(member);
}
// 삭제 ID Index 찾기
System.out.print("삭제할 ID >> "); // 33
String id = sc.next();
//List members = MemberMain.members;
int deleteIndex = -1;
for(int i=0; i File Write
FileWriter fw = null;
try {
fw = new FileWriter(file);
for(MemberVO m : members) {
String data = m.toString()+"\n";
fw.write(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}