-
[Java] 명품 자바 프로그래밍(Java Programming) 5장 Open ChallengeCSE/Java 2015. 6. 13. 11:02
Product.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162package 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152package 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
123456789101112131415161718192021222324252627282930313233343536373839404142package 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
1234567891011121314151617181920212223242526272829303132package 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116package 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 실행 결과:
'CSE > Java' 카테고리의 다른 글
[Java] 명품 자바 프로그래밍(Java Programming) 6장 실습문제 6번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 6장 Open Challenge (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 5장 실습문제 2번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 4장 실습문제 6번 (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 4장 Open Challenge (0) 2015.06.13 [Java] 명품 자바 프로그래밍(Java Programming) 3장 실습문제 12번 (0) 2015.06.13