Product.java
package kr.or.kihd.pmapp;
//상품관리 프로그램
//조상클래스
public class Product {
private int productId;
private String description;
private String maker;
private int price;
//생성자
public Product(int productId, String description, String maker, int price) {
super();
this.productId = productId;
this.description = description;
this.maker = maker;
this.price = price;
}
//getter 제공
public int getProductId() {
return productId;
}
public String getDescription() {
return description;
}
public String getMaker() {
return maker;
}
public int getPrice() {
return price;
}
public void showInfo() {
System.out.println("상품ID>> "+ (this.getProductId() + 1));
System.out.println("상품설명>> "+ (this.getDescription()));
System.out.println("생산자>> "+ (this.getMaker()));
System.out.println("가격>> "+ (this.getPrice()));
}
}
Book.java
package kr.or.kihd.pmapp;
public class Book extends Product{
private int ISBN;
private String title;
private String author;
public Book(int productId, String description, String maker, int price, int iSBN, String title, String author) {
super(productId, description, maker, price);
ISBN = iSBN;
this.title = title;
this.author = author;
}
public int getISBN() {
return ISBN;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("국제표준도서번호 >> " + this.getISBN());
System.out.println("책 제목 >> " + this.getTitle());
System.out.println("저자 >> " + this.getAuthor());
}
}
ConpactDisc.java
package kr.or.kihd.pmapp;
public class ConpactDisc extends Product {
private String albumTitle;
private String artisst;
public ConpactDisc(int productId, String description, String maker, int price, String albumTitle, String artisst) {
super(productId, description, maker, price);
this.albumTitle = albumTitle;
this.artisst = artisst;
}
public String getAlbumTitle() {
return albumTitle;
}
public String getArtisst() {
return artisst;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("엘범제목 >> " + this.getAlbumTitle());
System.out.println("가수 >> " + this.getArtisst());
}
}
ConversationBook.java
package kr.or.kihd.pmapp;
public class ConversationBook extends Book {
private String language;
public ConversationBook(int productId, String description, String maker, int price, int iSBN, String title,
String author, String language) {
super(productId, description, maker, price, iSBN, title, author);
this.language = language;
}
public String getLanguage() {
return language;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("언어 >> " + this.getLanguage());
}
}
ProductTest.java
package kr.or.kihd.pmapp;
import java.util.Scanner;
public class ProductTest {
//클래스 영역에 올라감(공유)
static int productID = 0;
static int numberOfProduct = 0;
static Product[] product = new Product[10];
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int choice = 0;
while(choice != 3) {
int type = 0;
System.out.println("======상품조회,추가 프로그램입니다.=======");
System.out.print("상품추가(1), 상품조회(2), 끝내기(3)");
choice = scan.nextInt();
switch(choice) {
case 1:
if(numberOfProduct >= product.length) {
System.out.println("더 이상 상품추가 불가");
break;
}
System.out.print("책(1), 음악CD(2), 회화책(3) >> ");
type = scan.nextInt();
if(type < 1 || type > 3) {
System.out.println("잘못 입력함");
break;
}
addProduct(type);
break;
case 2:
for(int i=0; i<numberOfProduct; i++) {
product[i].showInfo();
}
break;
case 3:
System.out.println("프로그램 종료");
break;
}
}
}
public static void addProduct(int type) {
scan.nextLine(); //버퍼에 남아있을수 있어서
System.out.print("상품설명 >> ");
String desc = scan.nextLine();
System.out.print("생산자 >> ");
String maker = scan.nextLine();
System.out.print("가격 >> ");
int price = scan.nextInt();
scan.nextLine(); //버퍼에 남아있을수 있어서
switch(type) {
case 1:
System.out.print("일반책 제목 >> ");
String title = scan.nextLine();
System.out.print("저자 >> ");
String author = scan.nextLine();
System.out.print("국제표준도서번호(ex.0001) >> ");
int ISBM = scan.nextInt();
//필드의 다형성을 활용(조상타입의 배열에 자손의 인스턴스를 담고 있음) Product[] product => book이 상속
product[numberOfProduct] = new Book(productID++, desc, maker, price, ISBM, title, author);
break;
case 2:
System.out.print("앨범 제목 >> ");
String albumtitle = scan.nextLine();
System.out.print("가수 >> ");
String artisst = scan.nextLine();
product[numberOfProduct] = new ConpactDisc(productID, desc, maker, price, albumtitle, artisst);
break;
case 3:
System.out.print("회화책 제목 >> ");
String title2 = scan.nextLine();
System.out.print("저자 >> ");
String author2 = scan.nextLine();
System.out.print("언어 >> ");
String language = scan.nextLine();
System.out.print("국제표준도서번호(ex.0001) >> ");
int ISBN2 = scan.nextInt();
product[numberOfProduct] = new ConversationBook(productID, desc, maker, price, ISBN2, title2, author2, language) ;
break;
}
numberOfProduct++;
}
}
result
======상품조회,추가 프로그램입니다.=======
상품추가(1), 상품조회(2), 끝내기(3)1
책(1), 음악CD(2), 회화책(3) >> 2
상품설명 >> 크리스마스캐롤
생산자 >> 사람
가격 >> 10000
앨범 제목 >> 크리스마스
가수 >> 테스
======상품조회,추가 프로그램입니다.=======
상품추가(1), 상품조회(2), 끝내기(3)2
상품ID>> 1
상품설명>> 크리스마스캐롤
생산자>> 사람
가격>> 10000
엘범제목 >> 크리스마스
가수 >> 테스
======상품조회,추가 프로그램입니다.=======
상품추가(1), 상품조회(2), 끝내기(3)1
책(1), 음악CD(2), 회화책(3) >> 1
상품설명 >> 코로나이후
생산자 >> 심처잉
가격 >> 10000
일반책 제목 >> 코로나
저자 >> 유발히라리
국제표준도서번호(ex.0001) >> 1111
======상품조회,추가 프로그램입니다.=======
상품추가(1), 상품조회(2), 끝내기(3)2
상품ID>> 1
상품설명>> 크리스마스캐롤
생산자>> 사람
가격>> 10000
엘범제목 >> 크리스마스
가수 >> 테스
상품ID>> 1
상품설명>> 코로나이후
생산자>> 심처잉
가격>> 10000
국제표준도서번호 >> 1111
책 제목 >> 코로나
저자 >> 유발히라리
======상품조회,추가 프로그램입니다.=======
상품추가(1), 상품조회(2), 끝내기(3)3
프로그램 종료