| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 오라클
- 백준문제
- 별 찍기
- 공부
- for문
- 전화번호부
- 이클립스
- 티스토리 블로그
- 파워서플라이
- 자바
- 스프링
- Oracle
- 순환문
- spring
- 무결성 제약조건
- 깃허브 블로그
- 오류모음
- 설정
- 환경설정
- 인터페이스
- 오류
- 백준
- ORA-02292
- ORA-01407
- 백준문제풀이
- Ajax
- 이클립스단축기
- jsp
- while
- MSI
Archives
- Today
- Total
danDevlog
Java - day15 (전화번호부 7단계) 본문
728x90
PhoneBook.java
package Phone;
import java.io.IOException;
public class PhoneBook {
public static void main(String[] args) throws IOException, ClassNotFoundException {
PhoneBookManager manager = new PhoneBookManager();//객체 1개를 공유 해야 함.
while (true) {
Menu.showMenu(manager);
}
}
}
Menu.java
package Phone;
import java.io.IOException;
import java.util.Scanner;
class Menu
{
static void showMenu(PhoneBookManager pb) throws IOException, ClassNotFoundException
{
// PhoneBookManager pb = new PhoneBookManager(); // 만악의 원흉
System.out.println("메뉴를 선택하세요.");
System.out.println("---------------");
System.out.println("1. 데이터 입력");
System.out.println("2. 데이터 검색");
System.out.println("3. 데이터 삭제");
System.out.println("4. 프로그램 종료");
System.out.println("5. 전화번호 목록");
System.out.println("---------------");
Scanner sc = new Scanner(System.in);
System.out.print("선택 : ");
int input = sc.nextInt();
switch (input) {
case 1:
System.out.println("데이터 입력을 시작합니다.");
System.out.println("1.일반, 2.대학, 3.회사");
int input2 = sc.nextInt();
switch (input2) {
case 1:
sc.nextLine();
System.out.print("이름 : ");
String name = sc.nextLine();
System.out.print("전화번호 : ");
String number = sc.nextLine();
System.out.print("생년월일 : ");
String birth = sc.nextLine();
pb.readData(name, number, birth);
break;
case 2:
sc.nextLine();
System.out.print("이름 : ");
String name2 = sc.nextLine();
System.out.print("전화번호 : ");
String number2 = sc.nextLine();
System.out.print("생년월일 : ");
String birth2 = sc.nextLine();
System.out.print("전공 : ");
String major = sc.nextLine();
System.out.print("학년 : ");
int grade = sc.nextInt();
pb.readData2(name2, number2, birth2, major, grade);
break;
case 3:
sc.nextLine();
System.out.print("이름 : ");
String name3 = sc.nextLine();
System.out.print("전화번호 : ");
String number3 = sc.nextLine();
System.out.print("생년월일 : ");
String birth3 = sc.nextLine();
System.out.print("회사 : ");
String agent = sc.nextLine();
pb.readData3(name3, number3, birth3, agent);
break;
default:
System.out.println("잘못 입력");
break;
}
// 더미 데이터 만들기.
// pb.readData("hong0", "1111", "990201");
// pb.readData("hong1", "1111", "990202");
// pb.readData("hong2", "1111", "990203");
// pb.readData("hong3", "1111", "990204");
// pb.readData("hong4", "1111", "990205");
break;
case 2:
System.out.println("데이터 검색을 시작합니다.");
pb.searchData();
break;
case 3:
System.out.println("데이터 삭제 시작합니다.");
pb.deleteData();
break;
case 4:
System.out.println("프로그램을 종료합니다.");
pb.storeToFile();
sc.close();
System.exit(0);
break;
case 5:
System.out.println("전화번호부를 출력합니다.");
pb.watchBook();
pb.readFromFile();
break;
default:
System.out.println("잘못선택하셨습니다.");
break;
}
}
}
PhoneBookManager.java
package Phone;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
class PhoneBookManager // 제어 클래스
{
Scanner sc = new Scanner(System.in);
final int MAX = 100;
//public PhoneInfo[] phoneList = new PhoneInfo[MAX];
// PhoneInfo[] phoneList = new PhoneInfo[MAX];// 객체 배열에 값이 초기화 됨.
HashSet<PhoneInfo> phoneList = new HashSet<>();
int cnt=0;
void readData(String name, String phoneNumber, String birth) {
// phoneList[cnt] = new PhoneInfo(name, phoneNumber, birth);
cnt++;
phoneList.add(new PhoneInfo(name, phoneNumber, birth));
System.out.println("입력이 완료되었습니다.");
}
void readData2(String name, String phoneNumber, String birth ,String major, int year) {
// phoneList[cnt] = new PhoneUnivInfo(name, phoneNumber, birth, major, year);
phoneList.add(new PhoneUnivInfo(name, phoneNumber, birth, major, year));
cnt++;
System.out.println("입력이 완료되었습니다.");
}
void readData3(String name, String phoneNumber, String birth, String agent) {
// phoneList[cnt] = new PhoneCompanyInfo(name, phoneNumber, birth, agent);
phoneList.add(new PhoneCompanyInfo(name, phoneNumber, birth, agent));
cnt++;
System.out.println("입력이 완료되었습니다.");
}
void searchData() {
System.out.print("검색할 이름 : ");
String name2 = sc.nextLine();
boolean isTrue = false;
// System.out.println("cnt:"+this.cnt);//0
// System.out.println(phoneList[0].getName());
// for (int i = 0; i < cnt; i++) {
//// System.out.println(phoneList[i].getName());
// if (name2.equals(phoneList[i].getName())) {
// phoneList[i].showPhoneInfo();
// isTrue = true;
// }
// }
Iterator<PhoneInfo> it = phoneList.iterator();
while(it.hasNext()) {
PhoneInfo p = it.next();
if (name2.equals(p.getName())) {
p.showPhoneInfo();
isTrue = true;
}
}
System.out.println(isTrue == true ? "" : "데이터가 없습니다.\n");
}
void deleteData() {
System.out.print("검색할 이름 : ");
String name3 = sc.nextLine();
boolean isTrue = false;
// for (int i = 0; i < cnt; i++) {
// if (name3.equals(phoneList[i].getName())) {
// for(int j = 0; j<cnt-1; j++) {
// phoneList[j] = phoneList[j+1];
// }
// cnt--;
// System.out.println("데이터 삭제 완료");
// }else {
// isTrue = false;
// }
// }
Iterator<PhoneInfo> it = phoneList.iterator();
while(it.hasNext()) {
PhoneInfo p = it.next();
if (name3.equals(p.getName())) {
it.remove();
isTrue = true;
System.out.println("데이터 삭제 완료");
}
}
System.out.println(isTrue == true ? "" : "데이터가 없습니다.\n");
}
void watchBook() {
// for (int i = 0; i < cnt; i++) {
// phoneList[i].showPhoneInfo();
// }
Iterator<PhoneInfo> it = phoneList.iterator();
while(it.hasNext()) {
PhoneInfo p = it.next();
p.showPhoneInfo();
}
}
void storeToFile() throws IOException {
try {
FileOutputStream fileSteam = new FileOutputStream("C:\\testjava\\test.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileSteam);
objectOutputStream.writeObject(phoneList);
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// try(FileWriter fw = new FileWriter("phoneBook.txt")){
// Iterator<PhoneInfo> it = phoneList.iterator();
// while(it.hasNext()) {
// PhoneInfo p = it.next();
// if(it instanceof PhoneInfo) {
// fw.write("이름 : " + p.name + "\n");
// fw.write("전화번호 : " + p.phoneNumber + "\n");
// fw.write("생년월일 : " + p.birth + "\n");
// System.out.println("출력이 완료되었습니다.");
// }else if(it instanceof PhoneUnivInfo) {
// fw.write("이름 : " + p.name + "\n");
// fw.write("전화번호 : " + p.phoneNumber + "\n");
// fw.write("생년월일 : " + p.birth + "\n");
// System.out.println("출력이 완료되었습니다.");
// }
// }
// } catch(IOException e) {
// e.printStackTrace();
// }
}
void readFromFile() throws IOException, ClassNotFoundException {
try {
FileInputStream fileStream = new FileInputStream("C:\\testjava\\test.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileStream);
Object object = objectInputStream.readObject();
System.out.println("읽어온 객체의 type->"+ object.getClass());
// System.out.println(object.toString());
Iterator<PhoneInfo> it = ((HashSet<PhoneInfo>) object).iterator();
while(it.hasNext()) {
PhoneInfo p = it.next();
System.out.println(p.toString());
}
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}// end class
PhoneInfo.java
package Phone;
import java.io.Serializable;
class PhoneInfo implements Serializable //데이터 클래스
{
/**
*
*/
private static final long serialVersionUID = 329304887188068285L;
protected String name;
protected String phoneNumber;
protected String birth;
//생성자
public PhoneInfo(String name, String phoneNumber, String birth) {
// TODO Auto-generated constructor stub
this.name = name;
this.phoneNumber = phoneNumber;
this.birth = birth;
}
public PhoneInfo() {
}
public void showPhoneInfo(){
System.out.println("name: "+ name);
System.out.println("phone: "+ phoneNumber);
if(birth!=null)
System.out.println("birth: "+ birth);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "일반[이름 : " + name + ", 전화번호 : " + phoneNumber + ", 생일 : " + birth + "]";
}
}
// 대학 친구들의 전화번호 저장
class PhoneUnivInfo extends PhoneInfo{
private String major;
private int year;
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public PhoneUnivInfo(String name, String phoneNumber, String birth ,String major, int year) {
// TODO Auto-generated constructor stub
this.name = name;
this.phoneNumber = phoneNumber;
this.birth = birth;
this.major = major;
this.year = year;
}
public PhoneUnivInfo() {
// TODO Auto-generated constructor stub
}
@Override
public void showPhoneInfo(){
System.out.println("name: "+ name);
System.out.println("phone: "+ phoneNumber);
if(birth!=null)
System.out.println("birth: "+ birth);
System.out.println("major: " + major);
System.out.println("year: " + year);
}
@Override
public String toString() {
return "대학[이름 : " + name + ", 전화번호 : " + phoneNumber + ", 생일 : " + birth + ", 전공 : " + major + ", 학년 : " + year + "]";
}
}
// 회사전화번호부
class PhoneCompanyInfo extends PhoneInfo{
private String agent;
public PhoneCompanyInfo(String name, String phoneNumber, String birth, String agent) {
// TODO Auto-generated constructor stub
this.name = name;
this.phoneNumber = phoneNumber;
this.birth = birth;
this.agent = agent;
}
public String getAgent() {
return agent;
}
public void setAgent(String agent) {
this.agent = agent;
}
public PhoneCompanyInfo() {
// TODO Auto-generated constructor stub
}
@Override
public void showPhoneInfo(){
System.out.println("name: "+ name);
System.out.println("phone: "+ phoneNumber);
if(birth!=null)
System.out.println("birth: "+ birth);
System.out.println("agent: " + agent);
}
@Override
public String toString() {
return "회사[이름 : " + name + ", 전화번호 : " + phoneNumber + ", 생일 : " + birth + ", 회사 : " + agent + "]";
}
}'Java' 카테고리의 다른 글
| Java - day16 (세미프로젝트 - POS구현) (0) | 2022.03.15 |
|---|---|
| Java - day 15 (11~13연습문제 / 예외처리) (0) | 2022.03.10 |
| Java - day14 (추상 클래스 / 인터페이스/StringBulider/HashMap/TreeSet) (0) | 2022.03.08 |
| Java - day13 (자바 연습문제) (0) | 2022.03.07 |
| Java - day12 (전화번호부 HashSet 다른풀이) (0) | 2022.03.06 |
Comments