본문 바로가기

Programming/Java

명품 java programming open challenging : 상속 관계의 클래스 작성(ProductInfo)

반응형

다음 그림과 같은 클래스 구조를 가진 자바 프로그램을 작성하겠다.





각 클래스에는 반드시 들어가야하는 필드들이 있고, main()에서는 최대 10개의 상품을 추가할 수 있으며 모든 상품의 정보를 조회할 수 있다. 모든 제품에 대한 정보를 출력할 때 Product 타입의 레퍼런스를 이용한다.


Product class : 각 상품의 고유한 식별자, 상품 설명, 생산자, 가격정보

Book class : ISBN 번호, 저자, 책 제목 정보

CompactDisc : 앨범 제목, 가수 이름

ConversationBook : 언어명 정보


*객체 지향 개념에 부합하도록 적절한 접근 지정자, 필드, 메소드, 생성자 등을 작성한다. 


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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.util.*;
 
class Product{
    private int id;
    private String comment;
    private String producer;
    private int price;
 
    //멤버 변수 접근
    public void setId(int id){
        this.id = id;
    }
    public int getId(){
        return id;
    }
    public void setComment(String comment){
        this.comment = comment;
    }
    public String getComment(){
        return comment;
    }
    public void setProducer(String producer){
        this.producer = producer;
    }
    public String getProducer(){
        return producer;
    }
    public void setPrice(int price){
        this.price = price;
    }
    public int getPrice(){
        return price;
    }
 
    public void addProduct(){ //상품 추가
        Scanner s = new Scanner(System.in);
 
        System.out.print("상품 설명>> ");
        setComment(s.nextLine());
        System.out.print("생산자>> ");
        setProducer(s.nextLine());
        System.out.print("가격>> ");
        setPrice(s.nextInt());
    }
 
    public void showProduct(){ //상품 조회
        System.out.println("상품 ID>> " + getId());
        System.out.println("상품 설명>> " + getComment());
        System.out.println("생산자>> " + getProducer());
        System.out.println("가격>> " + getPrice());
    }
 
}
 
 
class Book extends Product{ //책
     private int isbn;
     private String author;
     private String booktitle;
 
     //멤버 변수 접근
    public void setIsbn(int isbn){
        this.isbn = isbn;
    }
    public int getIsbn(){
        return isbn;
    }
    public void setAuthor(String author){
        this.author = author;
    }
    public String getAuthor(){
        return author;
    }
    public void setBooktitle(String booktitle){
        this.booktitle = booktitle;
    }
    public String getBooktitle(){
        return booktitle;
    }
    public void addProduct(){ //상품 추가
        Scanner s = new Scanner(System.in);
 
        super.addProduct();
 
        System.out.print("책 제목>> ");
        setBooktitle(s.nextLine());
        System.out.print("저자>> ");
        setAuthor(s.nextLine());
        System.out.print("ISBN>> ");
        setIsbn(s.nextInt());
    }
 
    public void showProduct(){ //상품 조회
        super.showProduct();
 
        System.out.println("ISBN>> " + getIsbn());
        System.out.println("책 제목>> " + getBooktitle());
        System.out.println("저자>> " + getAuthor());
    }
}
 
 
class ConversationBook extends Book{ //회화책
    private String language;
 
    //멤버 변수 접근
    public void setLanguate(String language){
        this.language = language;
    }
    public String getLanguage(){
        return language;
    }
 
    public void addProduct(){ //상품 추가
        Scanner s = new Scanner(System.in);
 
        super.addProduct();
 
        System.out.print("언어>> ");
        setLanguate(s.next());
    }
 
    public void showProduct(){ //상품 조회
        super.showProduct();
 
        System.out.println("언어>> " + getLanguage());
    }
}
 
 
class CompactDisc extends Product{ //음악 CD
    private String title; //앨범정보
    private String name; //가수이름
 
    //멤버 변수 접근
    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return title;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
 
    public void addProduct(){ //상품 추가
        Scanner s = new Scanner(System.in);
 
        super.addProduct();
 
        System.out.print("앨범 제목>> ");
        setTitle(s.nextLine());
        System.out.print("가수>> ");
        setName(s.nextLine());
    }
 
    public void showProduct(){ //상품 조회
        super.showProduct();
 
        System.out.println("앨범 제목>> " + getTitle());
        System.out.println("가수>> " + getName());
    }
 
}
 

 
public class ProductInfo {
    public static void main(String[] args){
        ArrayList<Product> list = new ArrayList<Product>();
        int count = 0//최대 10개 상품 추가
        Scanner scanner = new Scanner(System.in);
        Product p;
 
        System.out.println("*******Product Info*******");
 
        while(true){
            System.out.print("상품 추가<1>, 모든 상품 조회<2>, 끝내기<3> >> ");
            int menu = scanner.nextInt();
 
            switch(menu) {
                //상품 추가
                case 1:
                    //최대 10개 상품까지 추가 가능
                    if(count == 10){
                        System.out.println("최대 10개까지만 추가할 수 있습니다. ");
                        break;
                    }
                    System.out.print("상품 종류 책<1>, 음악CD<2>, 회화책<3> >> ");
                    int product = scanner.nextInt();
                    switch(product){
                        case 1:
                            p = new Book();
                            break;
                        case 2:
                            p = new CompactDisc();
                            break;
                        case 3:
                            p = new ConversationBook();
                            break;
                        default :
                            System.out.println("No more product menu over 3. Please choose from 1 to 3.");
                            continue;
                    }
                    p.addProduct();
                    p.setId(list.size());
                    list.add(p);
                    count+=1;
                    break;
 
                //상품 조회
                case 2:
                    for(int i=0; i<list.size(); i++){
                        list.get(i).showProduct();
                        System.out.println();
                    }
                    break;
 
                //끝내기
                case 3:
                    System.out.println("Exit.");
                    return;
                default :
                    System.out.println("Wrong number. Please input right number (1-3).");
                    continue;
            }
            System.out.println();
        }
    }
}
 
cs



덧붙이자면, 이 프로그램을 짜면서 나는 생성자를 삽입하지 않았다. 그렇지만 이 프로그램에서 반드시 지정되어야 하는, Product클래스의 Id값 같은 경우는 Product 생성자 내에서 setting 하게 만들어, id값 setting을 강제할 수도 있다. 간단히 설명하면, 다음과 같은 코드가 public void setId(int id) 대신 들어가는 것이다. 


 public class Product{

        private int id;

        public Product(int id){

                 this.id = id;

        }

}



다시 본론으로 들어와서, 위의 코드를 실행하면 다음과 같이 출력된다. 


10개 이상의 상품 추가를 제어하는 부분에 대해서는, 다음과 같이 출력하도록 하였다. 실제로 코드를 run 하여 10개의 상품을 추가한 후였다.


10개 이상의 상품 추가가 불가하다는 안내문을 띄우고, 사용자가 다시 메인 메뉴를 선택할 수 있게끔하였다.

반응형