[java] 상속 관계의 클래스 작성하기

haeri yu·2020년 5월 18일
0

명품 java programming 제 5장 상속 open challenge

다음과 같은 클래스 구조와 조건을 가진 자바 프로그램을 작성해보라.
Product 클래스는 각 상품의 고유한 식별자, 상품설명, 생산자, 가격 정보를 포함하고 있다. Book 클래스는 ISBN번호, 저자, 책 제목 정보를 포함한다. CompactDisc 클래스는 앨범 제목, 가수 이름 정보를 포함한다. ConversationBook은 회화책에서 다루는 언어명 정보를 포함한다. 객체 지향 개념에 부합하도록 적절한 접근 지정자, 필드, 메소드, 생성자 등을 작성하라. ProductInfo 클래스를 만들고 이곳에 main()을 둔다. main()에서는 최대 10개의 상품을 추가할 수 있으며 모든 상품의 정보를 조회활 수 있다. 모든 제품에 대한 정보를 출력할 때 Product타입의 레퍼런스를 이용하라.

import java.util.*;

class Product {
    private int id; //식별자
    private String desc; //상품 설명
    private String producer; //생산자
    private int price; //가격
    
    public void setValue(int id) {
    	Scanner sc = new Scanner(System.in);
        
        this.id = id;
        System.out.print("상품 설명>>");
        desc = sc.nextLint();
        System.out.print("생산자>>");
        producer = sc.nextLine();
        System.out.print("가격>>>");
        price = sc.nextInt();
    }
    
    public void printInfo() {
    	System.out.println("상품ID>>"+id);
        System.out.println("상품 설명>>"+desc);
        System.out.println("생산자>>"+producer);
        System.out.println("가격>>"+price);
    }
}

class Book extends Product {
    private String isbn; //ISBN번호
    private String author; //저자
    private String title: //책 제목
    
    public void setValue(int id) {
    	super.setValue(id);
        Scanner sc = new Scanner(System.in);
        
        System.out.print("책 제목>>");
        title = sc.nextLine();
        System.out.print("저자>>");
        author = sc.nextLine();
        System.out.print("ISBN>>");
        isbn = sc.nextLine();
    }
    
    public void printInfo() {
    	super.printInfo();
        
        System.out.println("ISBN>>"+isbn);
        System.out.println("책 제목>>"+title);
        System.out.println("저자>>"+author);
    }
}

class ConversationBook extends Book {
    private String lang;

    public void setValue(int id) {
        super.setValue(id);
        Scanner sc = new Scanner(System.in);

        System.out.print("언어>>");
        lang = sc.nextLine();
    }
    
    public void printInfo() {
        super.printInfo();

        System.out.println("언어>>"+lang);
    }
}

class CompactDisc extends Product {
    private String album;
    private String singer;

    public void setValue(int id) {
        super.setValue(id);
        Scanner sc = new Scanner(System.in);

        System.out.print("앨범 제목>>");
        album = sc.nextLine();
        System.out.print("가수>>");
        singer = sc.nextLine();
    }

    public void printInfo() {
        super.printInfo();

        System.out.println("앨범 제목>>"+album);
        System.out.println("가수>>"+singer);
    }
}

public class ProductInfo {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        boolean flag = true;
        int num = 0;
        Product [] prd = new Product[10];

        while(flag) {
            System.out.print("상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>>");
            int m = sc.nextInt();

            switch(m) {
                case 1: //상품추가

                    if (num < 10) {
                        Product p;

                        System.out.print("상품 종류 책(1), 음악CD(2), 회화책(3)>>>");
                        int t = sc.nextInt();
                        sc.nextLine();


                        switch(t) {
                            case 1: //책 입력
                                p = new Book();
                                break;
                            case 2: //음악CD 입력
                                p = new CompactDisc();
                                break;
                            case 3: //회화책 입력
                                p = new ConversationBook();
                                break;
                            default:
                                p = null;
                                System.out.println("잘못 입력 하셨습니다. 다시 입력해 주세요.");
                                break;
                        }

                        if (p != null) {
                            p.setValue(num);
                            prd[num] = p;
                            System.out.println(prd[num]);
                            num++;
                        }

                    } else {
                        System.out.println("최대 10개까지만 등록 가능합니다.");
                    }

                    break;
                case 2: //모든 상품 조회
                    for(int i = 0; i < num; i++) {
                        prd[i].printInfo();
                    }
                    break;
                case 3: //끝내기
                    flag = false;
                    break;
                default:
                    System.out.println("잘못 입력 하셨습니다. 다시 입력해 주세요.");
                    break;
            }
        }
    }
}
profile
안녕하세요

0개의 댓글