package ch07_fileQuiz;
import java.io.Serializable;
public class Member implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String phonenumber;
public Member(String name, String phonenumber) {
this.name=name; this.phonenumber=phonenumber;
}
public String getname() {return name;}
public void setname(String name) {this.name = name;}
public String getphonenumber() {return phonenumber;}
public void setphonenumber(String phonenumber) {this.phonenumber = phonenumber;}
public String toString() {
return name + " / " + phonenumber;
}
}
----------------------------------------------------------------------------------
// MemberManager
package ch07_fileQuiz;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/*
* - 전화번호 목록 파일로 부터 데이터 가져와서 ArrayList에 저장해서 확인, 수정 추가.
*/
import java.util.Scanner;
public class MemberManager {
private Scanner sc = new Scanner(System.in);
private List <Member> list ;
private File record;
public MemberManager() throws IOException {
list = new ArrayList<>();
record = new File("/Users/simpangyo/FinTech_SPG/Member/Member.txt");
if(record.exists() == false) {
record.mkdir();
record.createNewFile();
System.out.println("폴더 생성.");
}
}
public void insert() {
System.out.println("--- 회 원 추 가 ---");
System.out.print("이름 입력 > ");
String addname = sc.next();
Member man = FindName(addname);
if( man != null) {
System.out.println("이미 있는 회원입니다...");
} else {
System.out.print("전화번호 입력 > ");
String addphonenumber = sc.next();
list.add(new Member(addname, addphonenumber));
}
save();
}
public Member FindName(String name) {
for(Member man : list) {
if (man.getname().equals(name)) {
return man;
}
}
return null;
}
public void delete() {
System.out.println("--- 회 원 삭 제 ---");
System.out.print("삭제할 이름 입력 > ");
String dname = sc.next();
Member name = FindName(dname);
if ( name == null) {
System.out.println("이름을 잘못 입력하셨거나, 없는 회원입니다.");
} else {
list.remove(name);
System.out.println(name + " 삭제 완료...");
}
save();
}
public void List() {
System.out.println("-- 회 원 목 록 ---");
for(Member man : list) {
System.out.print(man + "\\n");
}
System.out.println();
System.out.println("회원 수 : " + list.size());
}
public void modify() {
System.out.println("--- 회 원 수 정 ---");
System.out.print("변경 전 이름 입력 > ");
String name = sc.next();
Member mem = FindName(name);
int idx = list.indexOf(mem);
System.out.println(list.get(idx));
if (mem == null) {
System.out.println("이름을 잘못 입력하셨거나, 없는 회원입니다.");
} else {
System.out.print("변경 후 이름 입력 > ");
String mname = sc.next();
System.out.print("수정할 전화번호 입력 > ");
String mphonenumber = sc.next();
Member modi = new Member(mname,mphonenumber);
list.set(idx, modi);
}
save();
}
public void config() {
System.out.println("--- 회 원 확 인 ---");
System.out.print("검색 이름 입력 > ");
String search = sc.next();
Member man = FindName(search);
if(man !=null ){
System.out.println(man);
} else {
System.out.println("없는 회원입니다...");
}
}
public void save() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(record);
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
for(Member man : list) {
oos.writeObject(man);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(oos != null) oos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
System.out.println(record.getName() + " 저장 완료");
}
public void Load() throws IOException {
if(record.length() == 0) return; // 파일에 기록된 데이터가 없으면 종료
if(list != null) list.clear(); // 여러번 돌게 되면 한번 비워주고 로드.
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(record);
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
while(true) {
Member member = (Member) ois.readObject();
// readObject() 메서드가 읽어드릴 데이터가 없으면 EOFException 이 발생.
list.add(member);
}
} catch (EOFException e) {
try {
if(ois != null) ois.close();
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (Exception e) {
}
System.out.println(record.getName() + " 로드 성공");
}
public void end() {
System.out.println("--- program end ---");
System.exit(0);
}
}
----------------------------------------------------------------------------------
//MemberMain
package ch07_fileQuiz;
import java.io.IOException;
import java.util.Scanner;
public class MemberMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
MemberManager mm = new MemberManager();
while(true) {
System.out.print("1.추가 2.삭제 3.목록 4.수정 5.확인 6.저장 7.불러오기\\n> ");
int select = sc.nextInt();
switch(select) {
case 1:
mm.insert();
break;
case 2:
mm.delete();
break;
case 3:
mm.List();
break;
case 4:
mm.modify();
break;
case 5:
mm.config();
break;
case 6:
mm.save();
break;
case 7:
mm.Load();
break;
case 0:
mm.end();
default:
System.out.println("값을 잘못 입력하셨습니다...");
}
System.out.println();
}
}
}