JAVA/chapter15_access_modifer
chapter04 : Book / BookMain
GAWON
2023. 5. 26. 18:46
package org.joonzis.ex;
public class Ex04_Book {
// Field
private String title;
private String writer;
private int price;
private int salesVolume; // 판매량
private boolean isBestSeller; // 판매량이 1000 이상이면 true, 아니면 false
// Constructor
public Ex04_Book(){ }
public Ex04_Book(String title, String writer, int price){
this.title = title;
this.writer = writer;
this.price = price;
}
public Ex04_Book(String title,int price){
this(title,"작자미상",price);
}
// Method
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSalesVolume() {
return salesVolume;
}
public void setSalesVolume(int salesVolume) {
this.salesVolume = salesVolume;
this.isBestSeller = (salesVolume >= 1000) ? true : false;
// setBestSeller((salesVolume >= 1000) ? true : false); ↑ 이거랑 똑같음!
}
public boolean isBestSeller() {
return isBestSeller;
}
public void setBestSeller(boolean isBestSeller) {
this.isBestSeller = isBestSeller;
}
public void output() {
System.out.println("제목 : " + title);
System.out.println("저자 : " + writer);
System.out.println("가격 : " + price);
System.out.println("판매량 : " + salesVolume);
System.out.println(isBestSeller ? "베스트셀러" : "일반서적");
}
}
package org.joonzis.ex;
public class Ex04_BookMain {
public static void main(String[] args) {
Ex04_Book book1 = new Ex04_Book();
Ex04_Book book2 = new Ex04_Book("오디션","천계영", 38000);
Ex04_Book book3 = new Ex04_Book("콩쥐팥쥐", 5000);
book1.setSalesVolume(0);
book2.setSalesVolume(500);
book3.setSalesVolume(1500);
book1.output();
System.out.println("---------------------");
book2.output();
System.out.println("---------------------");
book3.output();
}
}