-
[Java] 명품 자바 프로그래밍(Java Programming) 7장 실습문제 12번 Java / CSECSE/Java 2015. 6. 13. 11:06
Phone.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748package ch07;public class Phone {private String name;private String address;private String phoneNumber;public Phone() {}public Phone(String name, String address, String phoneNumber) {super();this.name = name;this.address = address;this.phoneNumber = phoneNumber;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getPhoneNumber() {return phoneNumber;}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}public String toString() {return name + " " + address + " " + phoneNumber;}}cs OpenChallenge07.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148package ch07;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Scanner;import java.util.Set;public class OpenChallenge07 {@SuppressWarnings("resource")public static void main(String[] args) {HashMap<String, Phone> phoneBook = new HashMap<String, Phone>();Scanner scan = new Scanner(System.in);int chooser = 0;String name = "";String address = "";String phoneNumber = "";Phone p;System.out.println("---------------------------------------------");System.out.println("전화번호 관리 프로그램을 시작합니다.");System.out.println("---------------------------------------------");while (true) {System.out.print("삽입:0, 삭제:1, 찾기:2, 전체보기:3, 파일 저장:4, 파일 읽기: 5, 종료:6 >> ");chooser = scan.nextInt();switch (chooser) {case 0:System.out.print("이름 >> ");name = scan.next();System.out.print("주소 >> ");address = scan.next();System.out.print("전화번호 >> ");phoneNumber = scan.next();p = new Phone(name, address, phoneNumber);phoneBook.put(name, p);break;case 1:System.out.print("이름 >> ");name = scan.next();p = searchName(phoneBook, name);if (p != null) {phoneBook.remove(name);} else {System.out.println(name + "은 등록되지 않은 사람입니다.");}break;case 2:System.out.print("이름 >> ");name = scan.next();p = searchName(phoneBook, name);if (p != null) {System.out.println(p);} else {System.out.println(name + "은 등록되지 않은 사람입니다.");}break;case 3:Set<String> keys = phoneBook.keySet();Iterator<String> it = keys.iterator();while (it.hasNext()) {String key = it.next();Phone value = phoneBook.get(key);System.out.println(value);}break;case 4:try {File file = new File("C:\\tmp\\phoneBook.txt");BufferedWriter bw = new BufferedWriter(new FileWriter(file));Set<String> key = phoneBook.keySet();Iterator<String> ite = key.iterator();while (ite.hasNext()) {String keyField = ite.next();Phone phone = phoneBook.get(keyField);bw.write(phone.toString());bw.newLine();}bw.close();} catch (IOException e) {e.printStackTrace();}break;case 5:try {File file = new File("C:\\tmp\\phoneBook.txt");BufferedReader br = new BufferedReader(new FileReader(file));String data = "";while ((data = br.readLine()) != null) {System.out.println(data);}br.close();} catch (IOException e) {e.printStackTrace();}break;case 6:System.out.println("프로그램을 종료합니다...");System.exit(0);default:System.out.println("잘못 입력 하셨습니다.");}}}public static Phone searchName(HashMap<String, Phone> map, String name) {Phone p = null;p = map.get(name);return p;}}cs 'CSE > Java' 카테고리의 다른 글
[Java] 명품 자바 프로그래밍(Java Programming) 7장 실습문제 10번 Java / CSE (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 7장 실습문제 6번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 6장 실습문제 2번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 7장 Open Challenge (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 6장 실습문제 12번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 6장 실습문제 10번 (0) 2015.06.13