문제
아래의 5개의 클래스를 작성하고, 메인에서 아래와 같은 실행결과를 출력하시오(다형성)
Product, Book, CompactDisc, ConversationBook,
ProductExample
Product Class(조상클래스)
멤버변수
private int productID
private String description
private String maker
private int price
멤버함수
생성자 구현할 것
getter,setter구현
void showInfo(void) *상품 정보출력
Book Class(자손클래스)
멤버변수
private int ISBN
private String title
private String author
멤버함수
생성자 구현할 것
getter,setter구현
void showInfo(void)*조상클래스showinfo 호출 및 출력
CompactDisc Class(자손클래스)
멤버변수
private String albumTitle
private String artist
멤버함수
생성자 구현할 것
getter,setter구현
void showInfo(void)*조상클래스showinfo 호출 및 출력
ConversationBook Class(Book의 자손클래스)
멤버변수
private String language
멤버함수
생성자 구현할 것
getter,setter구현
void showInfo(void)*조상클래스showinfo 호출 및 출력
ProductExample(실행클래스)
멤버변수
static int productID = 0
static int numberOfProduct = 0
static Product[] p = new Product[10]
멤버함수
private static void addProduct(int type)
* 구현 --> 상품정보 입력받음
실행화면
상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>1
상품 종류 책(1), 음악CD(2), 회화책(3)>>1
상품 설명>>몬스터길들이는 게임책이다
생산자>>넷마블
가격>>0
책 제목>>몬스터길들이기
저자>>개발1팀
국제표준도서번호(ex.0001)0001
상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>1
상품 종류 책(1), 음악CD(2), 회화책(3)>>2
상품 설명>>태양의후예OST입니다.
생산자>>그건잘...
가격>>35000
앨범 제목>>말해!뭐해~
가수>>케이윌
상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>1
상품 종류 책(1), 음악CD(2), 회화책(3)>>3
상품 설명>>아이들이 좋아하는 영어책입니다.
생산자>>생능출판사
가격>>25000
책 제목>>ING영어
저자>>김수로
언어>>영어
국제표준도서번호(ex.0001) >>0003
상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>2
상품ID>>1
상품 설명>>몬스터길들이는 게임책이다
생산자>>넷마블
가격>>0원
국제표준도서번호>>1
책 제목>>몬스터길들이기
저자>>개발1팀
상품ID>>2
상품 설명>>태양의후예OST입니다.
생산자>>그건잘...
가격>>35000원
앨법 제목>>말해!뭐해~
가수>>케이윌
상품ID>>3
상품 설명>>아이들이 좋아하는 영어책입니다.
생산자>>생능출판사
가격>>25000원
국제표준도서번호>>3
책 제목>>ING영어
저자>>김수로
언어>>영어
상품 추가(1), 모든 상품 조회(2), 끝내기(3)>>3
프로그램을 종료합니다.
import java.util.Scanner;
class Product {
private int productID;
private String description;
private String maker;
private int price;
public Product(int productID, String description, String maker, int price) {
this.productID = productID;
this.description = description;
this.maker = maker;
this.price = price;
}
public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMaker() {
return maker;
}
public void setMaker(String maker) {
this.maker = maker;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
void showInfo() {
System.out.println("상품ID>>" + getProductID());
System.out.println("상품 설명>>" + getDescription());
System.out.println("생산자>>" + getMaker());
System.out.println("가격>>" + getPrice());
}
}
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);
this.ISBN = ISBN;
this.title = title;
this.author = author;
}
public int getISBN() {
return ISBN;
}
public void setISBN(int iSBN) {
ISBN = iSBN;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
void showInfo() {
super.showInfo();
String ISBN = Integer.toString(getISBN());
System.out.println("국제표준도서번호>>" + Integer.parseInt(ISBN));
System.out.println("책 제목>>" + getTitle());
System.out.println("저자>>" + getAuthor());
}
}
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;
}
public void setLanguage(String language) {
this.language = language;
}
void showInfo() {
super.showInfo();
System.out.println("언어>>" + getLanguage());
}
}
class CompactDisc extends Product {
private String albumTitle;
private String artist;
public CompactDisc(int productID, String description, String maker, int price, String albumTitle, String artist) {
super(productID, description, maker, price);
this.albumTitle = albumTitle;
this.artist = artist;
}
public String getAlbumTitle() {
return albumTitle;
}
public void setAlbumTitle(String albumTitle) {
this.albumTitle = albumTitle;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
void showInfo() {
super.showInfo();
System.out.println("앨범 제목>>" + getAlbumTitle());
System.out.println("가수>>" + getArtist());
}
}
class ProductExample {
static int productID = 0;
static int numberOfProduct = 0; // ->어디다 씀?
static Product[] p = new Product[10];
private static void addProduct(int type) {
Scanner sc = new Scanner(System.in);
String description;
String maker;
int price;
int ISBN;
String title;
String author;
String albumTitle;
String artist;
String language;
if (type == 1) {
System.out.print("상품 설명>>");
description = sc.nextLine();
System.out.print("생산자>>");
maker = sc.next();
System.out.print("가격>>");
price = sc.nextInt();
System.out.print("책 제목>>");
title = sc.next();
System.out.print("저자>>");
author = sc.next();
System.out.print("국제표준도서번호(ex.0001)");
ISBN = sc.nextInt();
System.out.println();
for (int i = 0; i < p.length; i++) {
if (p[i] == null) {
productID++;
p[i] = new Book(productID, description, maker, price, ISBN, title, author);
break;
}
}
}
else if (type == 2) {
System.out.print("상품 설명>>");
description = sc.nextLine();
System.out.print("생산자>>");
maker = sc.next();
System.out.print("가격>>");
price = sc.nextInt();
System.out.print("앨범 제목>>");
albumTitle = sc.next();
System.out.print("가수>>");
artist = sc.next();
System.out.println();
for (int i = 0; i < p.length; i++) {
if (p[i] == null) {
productID++;
p[i] = new CompactDisc(productID, description, maker, price, albumTitle, artist);
break;
}
}
}
else if (type == 3) {
System.out.print("상품 설명>>");
description = sc.nextLine();
System.out.print("생산자>>");
maker = sc.next();
System.out.print("가격>>");
price = sc.nextInt();
System.out.print("책 제목>>");
title = sc.next();
System.out.print("저자>>");
author = sc.next();
System.out.print("언어>>");
language = sc.next();
System.out.print("국제표준도서번호(ex.0001)");
ISBN = sc.nextInt();
System.out.println();
for (int i = 0; i < p.length; i++) {
if (p[i] == null) {
productID++;
p[i] = new ConversationBook(productID, description, maker, price, ISBN, title, author, language);
break;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("실행화면");
while (true) {
System.out.print("상품 추가(1), 모든 상품 조회(2), 끝내기(3)>> ");
int num = sc.nextInt();
if (num == 1) {
System.out.print("상품 종류 책(1), 음악CD(2), 회화책(3)>> ");
int sort = sc.nextInt();
if (sort == 1) {
addProduct(1);
}
else if (sort == 2) {
addProduct(2);
}
else if (sort == 3) {
addProduct(3);
}
}
else if (num == 2) {
for (int i = 0; i < p.length; i++) {
if (p[0] == null) {
System.out.println("입력된 상품이 없습니다.");
break;
}
if (p[i] != null) { // null이 아닐때 조건 있어야함 아니면 null공간 참조
p[i].showInfo();
// System.out.println();
}
}
}
else if (num == 3) {
System.out.println("프로그램을 종료합니다.");
return;
}
}
}
}
유의한 부분
자바에서는 Class(부모)클래스 extends (조부모) 클래스 {..}
Class(자식)클래스 extends (부모) 클래스 { .. }
이렇게 상속이 가능하다.
else if (num == 2) {
for (int i = 0; i < p.length; i++) {
if (p[0] == null) {
System.out.println("입력된 상품이 없습니다.");
break;
}
if (p[i] != null) { // null이 아닐때 조건 있어야함 아니면 null공간 참조
p[i].showInfo();
// System.out.println();
}
}
}
상품 조회 시 null과 null 아닌 부분 분리하기
String ISBN = Integer.toString(getISBN());
System.out.println("국제표준도서번호>>" + Integer.parseInt(ISBN));
ISBN을 0을 떼서 출력해 주기 위해
Stirng으로 변환 후 다시 Int로 변환함
run 함수를 작성해
public static void main(String[] args) 부분을
public static void main(String[] args) {
ProductExample product = new ProductExample();
product.run();
}
이렇게 만들면 깔끔해 보임
p[productID++] = new ConversationBook(productID, description, maker, price, ISBN, title, author, language);
productID 활용해서 이런 식으로 추가
else if (num == 2) {
if (p[0] == null) {
System.out.println("입력된 상품이 없습니다.");
}
for (int i = 0; i < productID; i++) {
p[i].showInfo();
System.out.println();
}
}
이렇게 productID 활용해서 출력하게 바꿈
'Java' 카테고리의 다른 글
| [Java] (추상 클래스) (16-2) (0) | 2023.08.30 |
|---|---|
| [Java] (추상 클래스) (16-1) (0) | 2023.08.30 |
| [Java] 클래스 (상속, 오버라이딩, 다형성, 스택, 팝) (15) (0) | 2023.08.27 |
| [Java] 클래스 (상속, 오버라이딩, 다형성) (15) (0) | 2023.08.27 |
| [Java] 단축키 (0) | 2023.08.24 |