ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] 명품 자바 프로그래밍(Java Programming) 5장 Open Challenge
    CSE/Java 2015. 6. 13. 11:02

    Product.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
     
    package ch05;
     
    public class Product {
        private int id;
        private String describe;
        private String producer;
        private int price;
     
        public Product() {
     
        }
     
        public Product(int id, String describe, String producer, int price) {
            super();
            this.id = id;
            this.describe = describe;
            this.producer = producer;
            this.price = price;
        }
     
        public void show() {
            System.out.println("상품 ID >> " + id);
            System.out.println("상품 설명 >> " + describe);
            System.out.println("생산자 >> " + producer);
            System.out.println("가격 >> " + price);
        }
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        public String getDescribe() {
            return describe;
        }
     
        public void setDescribe(String describe) {
            this.describe = describe;
        }
     
        public String getProducer() {
            return producer;
        }
     
        public void setProducer(String producer) {
            this.producer = producer;
        }
     
        public int getPrice() {
            return price;
        }
     
        public void setPrice(int price) {
            this.price = price;
        }
     
    }
     
    cs




    Book.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
    package ch05;
     
    public class Book extends Product {
        private String ISBN;
        private String author;
        private String bookName;
     
        public Book() {
            super();
        }
     
        public Book(int id, String describe, String producer, int price,
                String iSBN, String author, String bookName) {
            super(id, describe, producer, price);
            ISBN = iSBN;
            this.author = author;
            this.bookName = bookName;
        }
     
        public void show() {
            super.show();
            System.out.println("ISBN >> " + ISBN);
            System.out.println("저자 >> " + author);
            System.out.println("책 제목 >> " + bookName);
        }
     
        public String getISBN() {
            return ISBN;
        }
     
        public void setISBN(String iSBN) {
            ISBN = iSBN;
        }
     
        public String getAuthor() {
            return author;
        }
     
        public void setAuthor(String author) {
            this.author = author;
        }
     
        public String getBookName() {
            return bookName;
        }
     
        public void setBookName(String bookName) {
            this.bookName = bookName;
        }
     
    }
     
    cs





    CompactDisc.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
     
    package ch05;
     
    public class CompactDisc extends Product {
        private String title;
        private String singer;
     
        public CompactDisc() {
            super();
        }
     
        public CompactDisc(int id, String describe, String producer, int price,
                String title, String singer) {
            super(id, describe, producer, price);
            this.title = title;
            this.singer = singer;
        }
     
        public void show() {
            super.show();
            System.out.println("앨범 제목 >> " + title);
            System.out.println("가수 >> " + singer);
        }
     
        public String getTitle() {
            return title;
        }
     
        public void setTitle(String title) {
            this.title = title;
        }
     
        public String getSinger() {
            return singer;
        }
     
        public void setSinger(String singer) {
            this.singer = singer;
        }
     
    }
     
    cs




    ConversationBook.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
     
    package ch05;
     
    public class ConversationBook extends Book {
        private String language;
     
        public ConversationBook() {
            super();
        }
     
        public ConversationBook(int id, String describe, String producer,
                int price, String iSBN, String author, String bookName,
                String language) {
            super(id, describe, producer, price, iSBN, author, bookName);
            this.language = language;
        }
     
        public void show() {
            super.show();
            System.out.println("언어 >> " + language);
        }
     
        public String getLanguage() {
            return language;
        }
     
        public void setLanguage(String language) {
            this.language = language;
        }
     
    }
     
    cs





    OpenChallenge05.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
     
    package ch05;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
     
    public class OpenChallenge05 {
     
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            List<Product> pArr = new ArrayList<Product>();
            Scanner scan = new Scanner(System.in);
     
            int count = 1;
            int chooser = 0;
     
            String describe = "";
            String producer = "";
            int price = 0;
     
            while (true) {
                System.out.print("상품 추가(1), 모든 상품 조회(2), 끝내기(3) >> ");
                chooser = scan.nextInt();
                switch (chooser) {
                case 1:
                    System.out.print("상품 종류 책(1), 음악CD(2), 회화책(3) >> ");
                    chooser = scan.nextInt();
     
                    System.out.print("상품 설명 >> ");
                    describe = scan.next();
                    System.out.print("생산자 >> ");
                    producer = scan.next();
                    System.out.print("가격 >> ");
                    price = scan.nextInt();
     
                    switch (chooser) {
     
                    case 1:
                        Book b1 = new Book();
                        b1.setId(count);
                        b1.setDescribe(describe);
                        b1.setProducer(producer);
                        b1.setPrice(price);
     
                        System.out.print("책 제목 >> ");
                        b1.setBookName(scan.next());
     
                        System.out.print("저자 >> ");
                        b1.setAuthor(scan.next());
     
                        System.out.print("ISBN >> ");
                        b1.setISBN(scan.next());
     
                        pArr.add(b1);
                        count++;
                        break;
     
                    case 2:
                        CompactDisc c1 = new CompactDisc();
                        c1.setId(count);
                        c1.setDescribe(describe);
                        c1.setProducer(producer);
                        c1.setPrice(price);
     
                        System.out.print("앨범 제목 >> ");
                        c1.setTitle(scan.next());
     
                        System.out.print("가수 >> ");
                        c1.setSinger(scan.next());
     
                        pArr.add(c1);
                        count++;
                        break;
     
                    case 3:
                        ConversationBook cv1 = new ConversationBook();
                        cv1.setId(count);
                        cv1.setDescribe(describe);
                        cv1.setProducer(producer);
                        cv1.setPrice(price);
     
                        System.out.print("책 제목 >> ");
                        cv1.setBookName(scan.next());
     
                        System.out.print("저자 >> ");
                        cv1.setAuthor(scan.next());
     
                        System.out.print("ISBN >> ");
                        cv1.setISBN(scan.next());
     
                        System.out.print("언어 >> ");
                        cv1.setLanguage(scan.next());
     
                        pArr.add(cv1);
                        count++;
                        break;
                    }
                    break;
     
                case 2:
                    for (int i = 0; i < pArr.size(); i++) {
                        pArr.get(i).show();
                    }
     
                    break;
     
                case 3:
                    System.exit(0);
                }
     
            }
        }
     
    }
     
    cs








    실행 결과:














    댓글

Designed by Tistory.