ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] 명품 자바 프로그래밍(Java Programming) 7장 Open Challenge
    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




    OpenChallenge.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
     
    package ch07;
     
    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 >> ");
                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:
                    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.