개발/정리

입력받아 주소록 만들기(입력/출력/삭제/끝내기)

Julybug 2021. 6. 26. 10:54
반응형
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
package addressBook;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
 
public class ADD {
 
    /*변수선언*/
    static String fileName = "Address.txt";
    static String name = null;
    static String number = null;
    static String temp = "";
    static boolean check = false;
    static HashMap<StringString> addList = new HashMap<StringString>();
    
    
    /*메뉴 번호를 선택*/
    public static void MenuChoice() throws IOException {
        Scanner sc = new Scanner(System.in);
        int a;
 
        do {
            System.out.println("\n*+*+*+*+*+[MENU]+*+*+*+*+*+");
            System.out.println("\t[1] 연락처 등록\n\t[2] 연락처 출력\n\t[3] 연락처 삭제\n\t[4] 끝내기");
            System.out.println("*+*+*+*+*+*+*+*+*+*+*+*+*+*");
            a = sc.nextInt();
 
            switch (a) {
            case 1: {
                input();break;
 
            }
            case 2: {
                output();break;
 
            }
            case 3: {
                delete();break;
 
            }
            default:
                if (a > 4)
                    System.out.println("[" + a + "]" + "번은 없는 메뉴 입니다.");
                break;
 
            }
 
        } while (a != 4);
        System.out.println("[4]. 종료합니다.");
 
    }
    /*등룍*/
    private static void input() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
        Scanner sc = new Scanner(System.in);
 
        System.out.print("추가하실 이름:");
        name = sc.next();
        bw.write(name + "\n");
        System.out.print("추가하실 번호:");
        number = sc.next();
        bw.write(number + "\n");
 
        addList.put(name, number);  //이름이랑 번호를 HashMap에 저장
        bw.close();
    }
 
    /*삭제*/
    private static void delete() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        Scanner sc = new Scanner(System.in);
        System.out.print("삭제하실 이름 : ");
        name = sc.next();
 
        while (true) {
            number = br.readLine();
            if (number == null) {
                break;
            }
 
            if (number.equals(name)) {
                check = true;
                continue;
            }
            temp += number + "\n";
        }
        br.close();
 
        if (check) {
            BufferedWriter bwe = new BufferedWriter(new FileWriter(fileName));
            bwe.write(temp);
            addList.remove(name);
            bwe.close();
            System.out.println("\t[삭제 성공]");
        } else {
            System.out.println("\t[삭제 실패]");
        }
 
    }
 
    /*출력*/
    private static void output() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));        
        Scanner sc = new Scanner(System.in);
 
        Object[] keylist = addList.keySet().toArray();        //Hashmap에 있는 정보 모두 출력 
        for (int i = 0; i < keylist.length; i++) {
            String k = (String) keylist[i];
            System.out.print("[ " + (i + 1+ "번 ]  ");
            System.out.print(k + " : ");
            System.out.println((String) addList.get(k));
        
                
        }if( keylist.length==0)
            System.out.println("!!주소록이 비었습니다!!");
 
 
    }
 
    /*메인*/
    public static void main(String[] args) throws IOException {
 
        MenuChoice();
}
 
cs
 
 
 
반응형

'개발 > 정리' 카테고리의 다른 글

0628 숙제  (0) 2021.06.28
소켓, SQL, JDBC, 스프링 개념정리  (0) 2021.06.28
SQL , 오라클 , 정의어 DDL, DML, DCL 개념 정리  (0) 2021.06.28
데이터 베이스  (0) 2021.06.28
구구단 (반복문의 중첩/for문)  (0) 2021.06.26