java/코드 리뷰

16. characterstream- (11) ReaderDemo ( 도서 관리 시스템 )

Astaroth아스 2020. 4. 27. 16:48
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
package characterstream;
 
public class Book {
    
    private int no;
    private String title;
    private String writer;
    private String publisher;
    private int price;
    
    public Book() {
        
    }
    
    public Book(int no, String title, String writer, String publisher, int price) {
        super();
        this.no = no;
        this.title = title;
        this.writer = writer;
        this.publisher = publisher;
        this.price = price;
    }
 
    public int getNo() {
        return no;
    }
 
    public void setNo(int no) {
        this.no = no;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getWriter() {
        return writer;
    }
 
    public void setWriter(String writer) {
        this.writer = writer;
    }
 
    public String getPublisher() {
        return publisher;
    }
 
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
 
    public int getPrice() {
        return price;
    }
 
    public void setPrice(int price) {
        this.price = price;
    }
    
}
 
 
 
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
package characterstream;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class ReaderDemo1 {
 
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("c:/files/data.txt");
        BufferedReader br = new BufferedReader(fr);
        
//        String text1 = br.readLine();
//        String text2 = br.readLine();
//        String text3 = br.readLine();
//        String text4 = br.readLine();
//        String text5 = br.readLine();
//        String text6 = br.readLine();
//        
//        System.out.println(text1);
//        System.out.println(text2);
//        System.out.println(text3);
//        System.out.println(text4);
//        System.out.println(text5);
//        System.out.println(text6);
        
        String text = null;
        int totalOrderPrice = 0;
        
        while ((text = br.readLine()) != null) {
            if (!text.isEmpty()) {
//                System.out.println(text); // 위의 text들을 한 줄로 요약
                String[] items = text.split(",");
//                System.out.println(items[4]); // 4번째 가격만 출력
                int price = Integer.parseInt(items[4]);
                totalOrderPrice += price;
            }
        }
        
        System.out.println("총 구매금액 : " + totalOrderPrice);
 
        br.close();
    }
}
 
 
 
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
package characterstream;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class ReaderDemo2 {
 
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
 
        // data.txt의 도서 정보 읽어서 Book 객체에 담고, ArrayList에 Book 객체 저장
        List<Book> books = new ArrayList<Book>();
 
        FileInputStream fis = new FileInputStream("c:/files/data.txt");
        InputStreamReader isr = new InputStreamReader(fis, "ms949");
        BufferedReader reader = new BufferedReader(isr);
 
        String text = null;
 
        while ((text = reader.readLine()) != null) {
            if (!text.isEmpty()) {
                String[] items = text.split(",");
 
                int no = Integer.parseInt(items[0]);
                String title = items[1];
                String writer = items[2];
                String publisher = items[3];
                int price = Integer.parseInt(items[4]);
 
                Book book = new Book();
                book.setNo(no);
                book.setTitle(title);
                book.setWriter(writer);
                book.setPublisher(publisher);
                book.setPrice(price);
 
                books.add(book);
 
            }
        }
        reader.close();
 
        while (true) {
            System.out.println("========================================");
            System.out.println("1.도서내역조회    2.상세조회        0.종료");
            System.out.println("========================================");
 
            System.out.print("메뉴 번호를 입력하세요 : ");
            int menuNo = sc.nextInt();
            System.out.println();
 
            if (menuNo == 1) {
                System.out.println("[도서내역조회]");
                System.out.printf("%-3s    %-30s    %-15s    %-7s\n""번호""제목""저자""가격");
                for (Book book : books) {
                    int no = book.getNo();
                    String title = book.getTitle();
                    String writer = book.getWriter();
                    int price = book.getPrice();
 
                    System.out.printf("%-3s    %-30s    %-15s    %-7s\n", no, title, writer, price);
                }
                System.out.println();
 
            } else if (menuNo == 2) {
                System.out.println("[도서정보 보기]");
                System.out.print("책번호를 입력하세요 : ");
                int bookNo = sc.nextInt();
                System.out.println();
 
                for (Book book : books) {
                    if (book.getNo() == bookNo) {
                        System.out.println("책번호 : " + book.getNo());
                        System.out.println("제목 : " + book.getTitle());
                        System.out.println("저자 : " + book.getWriter());
                        System.out.println("출판사 : " + book.getPublisher());
                        System.out.println("가격 : " + book.getNo());
                    }
                }
                System.out.println();
 
            } else if (menuNo == 0) {
                break;
            }
 
        }
 
        sc.close();
    }
}