ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] 명품 자바 프로그래밍(Java Programming) 7장 실습문제 12번 Java / CSE
    CSE/Java 2015. 6. 13. 11:06

    Phone.java


     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    package 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











     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
     
    package 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







    댓글

Designed by Tistory.