Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
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
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter04 : book / bookmain 본문

JAVA/chapter08_constructor

chapter04 : book / bookmain

GAWON 2023. 5. 26. 18:01
package org.joonzis.ex;
/*
 * 필드
 *  - String title				: 책 제목
 *  - String writer				: 저자
 *  - int price					: 책 가격
 *  - int salesVolume			: 판매량
 *  - boolean isBestSeller		: 베스트 셀러 유무
 *  
 * 메소드
 *  - 생성자()
 *  - 생성자() : 제목, 가격 초기화 (저자는 "작자미상" 으로 기입)
 *  - 생성자() : 제목, 가격, 저자 초기화
 *  - setSalesVolume(int sales) : 판매량이 1000 이상이면 베스트 셀러
 *  - output() : 제목, 저자, 가격, 판매량, 베스트셀러 유무 출력
 */


public class Ex04_Book {
	String title;		
	String writer;			
	int price;			
	int salesVolume;			
	boolean isBestSeller;
	
	public Ex04_Book() {}
	public Ex04_Book(String _title, int _price) {
		title = _title;
		price = _price;
		writer = "작자미상";
	}
	public Ex04_Book(String _title, int _price, String _writer) {
		title = _title;
		price = _price;
		writer = _writer;
	}
	void setSalesVolume(int sales) {
		salesVolume = sales;
		if(salesVolume >= 1000) {
			isBestSeller = true;
		}else {
			isBestSeller = false;
		}
	}
	void output() {
		System.out.println("책 제목 : " + title);
		System.out.println("책 가격 : " + price);
		System.out.println("책 저자 : " + writer);
		System.out.println("책 판매량 : " + salesVolume);
		System.out.println(isBestSeller ? "베스트셀러" : "일반서적");
	}
	
	
	
	
	
}
package org.joonzis.ex;

public class Ex04_BookMain {
	public static void main(String[] args) {
		
		Ex04_Book b1 = new Ex04_Book();
		Ex04_Book b2 = new Ex04_Book("기적의 집밥책", 18000);
		Ex04_Book b3 = new Ex04_Book("김밀란 리조또", 18000, "김밀란");
		
		b1.setSalesVolume(0);
		b2.setSalesVolume(500);
		b3.setSalesVolume(2000);
		
		b1.output();
		System.out.println("--------------------");
		b2.output();
		System.out.println("--------------------");
		b3.output();
		
		
		
	}
}

'JAVA > chapter08_constructor' 카테고리의 다른 글

Test . constructor  (0) 2023.05.26
chapter03 : circle / circlemain  (0) 2023.05.26
chapter02 : person / personmain  (0) 2023.05.26
chapter01 : rect / rectmain  (0) 2023.05.26
chapter00 : Triangle / TriangleMain  (0) 2023.05.26