Java programming

(22.10.22.)Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ: HashSet์„ ์ด์šฉํ•œ CRUD

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 10. 22.

Book ์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์†์„ฑ์€ ์ €์ž, ์ œ๋ชฉ, ์ถœํŒ์ผ, ๊ฐ€๊ฒฉ 4๊ฐ€์ง€๋กœ ํ•˜๊ณ  ๋™์ผํ•œ ๋„์„œ ์ •๋ณด๊ฐ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ํ‚ค๋ณด๋“œ์—์„œ ์ •๋ณด๋ฅผ ์ปฌ๋ ‰์…˜์— ์ €์žฅํ•ด๋ณด๊ณ ,  ์ €์ž, ์ œ๋ชฉ, ์ถœํŒ์ผ ์†์„ฑ์ด ๋ชจ๋‘ ์ผ์น˜ํ•˜๋Š” ๋„์„œ๋ฅผ ๋™์ผ ๋„์„œ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด CRUD ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์ž!

 

Book Class ์ฝ”๋“œ

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
import java.util.Objects;
 
public class Book 
{
    private String author;
    private String title;
    private String pubDate;
    private int price;
    
    public Book() {}
    
    public Book(String author, String title, String pubDate, int price) {
        this.author = author;
        this.title = title;
        this.pubDate = pubDate;
        this.price = price;
    }
 
    @Override
    public boolean equals(Object obj) {
        Book other = (Book) obj;
        return this.author.equals(other.author) &&
                this.title.equals(other.title) &&
                this.pubDate.equals(other.pubDate) ? true : false;
    }
    @Override
    public int hashCode() {
        return Objects.hash(author, title, pubDate);
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getPubDate() {
        return pubDate;
    }
    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}
cs

๋จผ์ € Book ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š”๋ฐ ์ €์ž, ์ œ๋ชฉ, ์ถœํŒ์ผ์ด ๊ฐ™์œผ๋ฉด ๋™์ผ ์ €์„œ๋กœ ๊ฐ„์ฃผํ•˜๊ธฐ ์œ„ํ•ด์„œ equals ๊ธฐ๋Šฅ์„ ๋ฌธ์ž์—ด ์•Œ๋งน์ด๋งŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  HashSet ์ž๋ฃŒ๊ตฌ์กฐ์—์„  ๋‘ ๊ฐ์ฒด๋ฅผ equals()๋กœ ๋น„๊ตํ•ด์„œ ๋™์ผํ•˜๋‹ค๋ฉด hashcode() ๋ฆฌํ„ด๊ฐ’๋„ ๋™์ผํ•ด์•ผํ•œ๋‹ค.

๋”ฐ๋ผ์„œ hashcode()๋„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์ค˜์•ผํ•œ๋‹ค. hashcode()๋Š” Hash*** ์ปฌ๋ ‰์…˜์— ์˜ํ•ด์„œ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋  ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ ํ˜ธ์ถœํ•œ๋‹ค.

Main Class ์ฝ”๋“œ

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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import com.ezen.java.oop.Book;
 
public class SetCRUD 
{
    static enum Menu { LIST, ADD, FIND, UPDATE, DELETE, EXIT};
    
    public static void main(String[] args) 
    {
        HashSet<Book> set = new HashSet<>();
        Menu menu;
        boolean go = true;
        while(go){
            menu = menu();
            switch(menu)
            {
            case LIST:  list(set);                 break;
            case ADD:   add(set);                break;
            case FIND:  printBook(find(set));     break;
            case UPDATE:update(set);             break;
            case DELETE:delete(set);            break;
            case EXIT:  go=false;                 break;
            defaultSystem.out.println("๋ฉ”๋‰ด์ž…๋ ฅ ์˜ค๋ฅ˜");
            }
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
    
    private static Menu menu(){
        Scanner kbd = new Scanner(System.in);
        System.out.print("๋ชฉ๋ก(s), ์ถ”๊ฐ€(a), ๊ฒ€์ƒ‰(f), ์ˆ˜์ •(u), ์‚ญ์ œ(d), ์ข…๋ฃŒ(x):");
        String m = kbd.nextLine().strip();
        Menu menu = null;
        switch(m)
        {
            case "s": menu = Menu.LIST;     break;
            case "a": menu = Menu.ADD;         break;
            case "f": menu = Menu.FIND;     break;
            case "u": menu = Menu.UPDATE;     break;
            case "d": menu = Menu.DELETE;     break;
            case "x": menu = Menu.EXIT;     break;
            default : menu = null
        }
        return menu;
    }
    
    private static void add(HashSet<Book> set){
        Scanner kbd = new Scanner(System.in);
        
        System.out.print("์ €์ž:");
        String author = kbd.nextLine().strip();
        System.out.print("์ œ๋ชฉ:");
        String title = kbd.nextLine().strip();
        System.out.print("์ถœํŒ์ผ:");
        String pubDate = kbd.nextLine().strip();
        System.out.print("๊ฐ€๊ฒฉ:");
        int price = kbd.nextInt();
        kbd.nextLine();
 
        set.add(new Book(author,title,pubDate,price));
    }
    
    private static void list(HashSet<Book> set){
        System.out.println();
        Iterator<Book> iter = set.iterator();
        while(iter.hasNext())
        {
            printBook(iter.next());
        }
        System.out.println();
    }
    
    private static Book getKeyword(){
        Scanner kbd = new Scanner(System.in);
        System.out.print("์ €์ž:");
        String author = kbd.nextLine().strip();
        System.out.print("์ œ๋ชฉ:");
        String title = kbd.nextLine().strip();
        System.out.print("์ถœํŒ์ผ:");
        String pubDate = kbd.nextLine().strip();
        
        Book book = new Book();
        book.setAuthor(author);
        book.setTitle(title);
        book.setPubDate(pubDate);
        return book;
    }
    
    private static Book find(HashSet<Book> set){
        Book key = getKeyword();
        if(set.contains(key))
        {
            ArrayList<Book> list = new ArrayList<>(set);
            return list.get(list.indexOf(key));
        }else {
            System.out.println("๊ฒ€์ƒ‰์‹คํŒจ");
        }
        return null;
    }
    
    private static void printBook(Book book){
        String author = book.getAuthor();
        String title = book.getTitle();
        String pubDate = book.getPubDate();
        int price = book.getPrice();
        
        System.out.printf("%s\t%s\t%s\t%d \n", author,title,pubDate,price);
    }
    
    private static void update(HashSet<Book> set){
        Book found = find(set);
        if(found!=null) {
            System.out.println("์ˆ˜์ •๋Œ€์ƒ ๋ฐœ๊ฒฌ");
            System.out.print("์ƒˆ ๊ฐ€๊ฒฉ:");
            Scanner kbd = new Scanner(System.in);
            int newPrice = kbd.nextInt();
            found.setPrice(newPrice);
            System.out.println("๊ฐ€๊ฒฉ ์ˆ˜์ • ์„ฑ๊ณต");
        }else {
            System.out.println("์ˆ˜์ • ๋Œ€์ƒ ๊ฒ€์ƒ‰ ์‹คํŒจ");
        }
    }
    
    private static void delete(HashSet<Book> set){
        Book found = find(set);
        if(found!=null) {
            
            if(set.remove(found))
            {
                System.out.println("์‚ญ์ œ ์„ฑ๊ณต");
            }
        }else {
            System.out.println("์‚ญ์ œ ๋Œ€์ƒ ๊ฒ€์ƒ‰ ์‹คํŒจ");
        }
    }
}
cs

 

๋ฉ”์†Œ๋“œ ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ๋ณธ๋‹ค๋ฉด ๋จผ์ € ๋ฉ”๋‰ด ์—ด๊ฑฐํ˜• ๋ณ€์ˆ˜ํ˜• ๋งŒ๋“ค์–ด์„œ ๋ฉ”๋‰ด๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ๋ฉ”๋‰ด ๋ฉ”์†Œ๋“œ์—์„  ํ‚ค๋ณด๋“œ์—์„œ  ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ๋ฉ”์ธ๋ฉ”์†Œ๋“œ์— ์ž…๋ ฅ๊ฐ’์„ ๋ณด๋‚ด๊ณ  ํ˜ธ์ถœ๋œ ๋ฉ”๋‰ด ๋ฉ”์†Œ๋“œ์— ๋งž๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ€์„œ ๊ธฐ๋Šฅ์ด ์ž‘๋™ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ์—์„   ๋ฉ”๋‰ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์‘ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

Bookํด๋ž˜์Šค์˜ Hashset ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ static์œผ๋กœ ํ•ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋‰ด์—์„œ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ๋“ค์€ Bookํด๋ž˜์Šค์˜ Hashset ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋‘ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์•ผํ•œ๋‹ค.


add() ๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static void add(HashSet<Book> set)
    {
        Scanner kbd = new Scanner(System.in);
        
        System.out.print("์ €์ž:");
        String author = kbd.nextLine().strip();
        System.out.print("์ œ๋ชฉ:");
        String title = kbd.nextLine().strip();
        System.out.print("์ถœํŒ์ผ:");
        String pubDate = kbd.nextLine().strip();
        System.out.print("๊ฐ€๊ฒฉ:");
        int price = kbd.nextInt();
        kbd.nextLine();
 
        set.add(new Book(author,title,pubDate,price));
    }
cs

ํ‚ค๋ณด๋“œ๋กœ ๋ถ€ํ„ฐ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๊ทธ๊ฒƒ์„ ์ƒˆ๋กœ Book ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค.


list() ๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
private static void list(HashSet<Book> set)
    {
        System.out.println();
        Iterator<Book> iter = set.iterator();
        while(iter.hasNext())
        {
            printBook(iter.next());
        }
        System.out.println();
    }
cs

add ๋ฉ”์†Œ๋“œ๋กœ ์ธํ•ด ์ถ”๊ฐ€๋œ Book์˜ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. 

HashSet ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ˆœ์„œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— Iterator ๋ฐ˜๋ณต์ž๋ฅผ ์จ์•ผํ•œ๋‹ค.

์ˆœ์„œ๋ฅผ ๋‚˜์—ด ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ Book ๊ฐ์ฒด๋ฅผ printBook ๋ฉ”์†Œ๋“œ์— ๋„ฃ์–ด์ฃผ๋ฉด printBook์—์„œ ์ •ํ•ด๋‘” ์–‘์‹์— ๋งž๊ฒŒ Book ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

printBook() ๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
private static void printBook(Book book)
    {
        String author = book.getAuthor();
        String title = book.getTitle();
        String pubDate = book.getPubDate();
        int price = book.getPrice();
        
        System.out.printf("%s\t%s\t%s\t%d \n", author,title,pubDate,price);
    }
cs

Book ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์ถœ๋ ฅ ์–‘์‹์„ ์ •ํ•ด๋‘” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ถœ๋ ฅ์šฉ์œผ๋กœ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋งŒ๋“ค์–ด ๋†“์•˜๋‹ค.


getKeyword()๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static Book getKeyword()
    {
        Scanner kbd = new Scanner(System.in);
        System.out.print("์ €์ž:");
        String author = kbd.nextLine().strip();
        System.out.print("์ œ๋ชฉ:");
        String title = kbd.nextLine().strip();
        System.out.print("์ถœํŒ์ผ:");
        String pubDate = kbd.nextLine().strip();
        
        Book book = new Book();
        book.setAuthor(author);
        book.setTitle(title);
        book.setPubDate(pubDate);
        return book;
    }
cs

์ฑ…์˜ ์ €์ž, ์ œ๋ชฉ, ์ถœํŒ์ผ์ด ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ์ฐพ๊ธฐ๋กœ ํ–ˆ์œผ๋ฏ€๋กœ ๋”ฐ๋กœ ํ‚ค์›Œ๋“œ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ƒˆ๋กœ์šด Book ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅ ๋ฐ›์€ ์ €์ž, ์ œ๋ชฉ, ์ถœํŒ์ผ์„ ์ €์žฅํ•˜์—ฌ ์ด Book๊ฐ์ฒด์™€ ๊ฐ™์€ Book๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ์„ ๊ฒƒ์ด๋‹ค.


find()๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
private static Book find(HashSet<Book> set)
    {
        Book key = getKeyword();
        if(set.contains(key))
        {
            ArrayList<Book> list = new ArrayList<>(set);
            return list.get(list.indexOf(key));
        }else {
            System.out.println("๊ฒ€์ƒ‰์‹คํŒจ");
        }
        return null;
    }
cs

getKeyword()๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ›์•„์˜จ Book ๊ฐ์ฒด๋ฅผ key๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด ์ค€๋‹ค. HashSet<Book> set์— key์˜ ๋‚ด์šฉ๊ณผ ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ์œ„ํ•ด contains ๊ธฐ๋Šฅ์„ ์“ด๋‹ค. ์ด๋•Œ HashSet ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— HashSet๋ฅผ ArrayList ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด ์ฐพ์€ ์ธ๋ฐ์Šค ๋ฒˆํ˜ธ์˜ Book ๊ฐ์ฒด ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค. ๋ฆฌํ„ด ๋œ ์ •๋ณด๋Š” printBook ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ€์„œ ๊ฐ’์„ ์ฐพ์€ ์ •๋ณด๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด์ฃผ๊ฒŒ ๋œ๋‹ค.


update()๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static void update(HashSet<Book> set)
    {
        Book found = find(set);
        if(found!=null) {
            System.out.println("์ˆ˜์ •๋Œ€์ƒ ๋ฐœ๊ฒฌ");
            System.out.print("์ƒˆ ๊ฐ€๊ฒฉ:");
            Scanner kbd = new Scanner(System.in);
            int newPrice = kbd.nextInt();
            found.setPrice(newPrice);
            System.out.println("๊ฐ€๊ฒฉ ์ˆ˜์ • ์„ฑ๊ณต");
        }else {
            System.out.println("์ˆ˜์ • ๋Œ€์ƒ ๊ฒ€์ƒ‰ ์‹คํŒจ");
        }
    }
cs

์ˆ˜์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„  ๋จผ์ € ์ˆ˜์ •ํ•  Book๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ์•„์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ฐพ๋Š” ๊ณผ์ •์€ find()๋ฉ”์†Œ๋“œ ๊ณผ์ •๊นŒ์ง€ ๊ฐ™๋‹ค. ์ฐพ๊ธฐ ์œ„ํ•ด ํ‚ค๋ณด๋“œ๋กœ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์€ find() ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ๊ฐ’์„ found๋ผ๋Š” ์ด๋ฆ„์˜ ์ƒˆ๋กœ์šด Book๊ฐ์ฒด์— ์ €์žฅํ•˜์—ฌ ์ค€๋‹ค. find() ๋ฉ”์†Œ๋“œ์—์„  ์ฐพ์œผ๋ ค๋Š” Book ๊ฐ์ฒด ๋˜๋Š” null๋ฅผ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ null์ด ์•„๋‹ˆ๋ฉด ์ˆ˜์ •๋Œ€์ƒ์ด ๋ฐœ๊ฒฌํ–ˆ๋‹ค ํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ์ˆ˜์ •ํ•  ๊ฐ€๊ฒฉ ๊ฐ’์„ ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅ๋ฐ›๊ณ  ์ƒˆ๋กœ์šด ๊ฐ€๊ฒฉ ๊ฐ’์„ Book ํด๋ž˜์Šค์˜ setPrice() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ found Book๊ฐ์ฒด์— ์ €์žฅํ•œ๋‹ค.


delete()๋ฉ”์†Œ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
private static void delete(HashSet<Book> set)
    {
        Book found = find(set);
        if(found!=null) {
            
            if(set.remove(found))
            {
                System.out.println("์‚ญ์ œ ์„ฑ๊ณต");
            }
        }else {
            System.out.println("์‚ญ์ œ ๋Œ€์ƒ ๊ฒ€์ƒ‰ ์‹คํŒจ");
        }
    }
cs

delete()๋ฉ”์†Œ๋“œ๋„ update()๋ฉ”์†Œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋จผ์ € ์‚ญ์ œํ•  ๊ฐ’์„ ์ฐพ์•„์•ผํ•œ๋‹ค. ์ฐพ์€ ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ผ๋ฉด remove ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋Œ“๊ธ€