반응형
package dayday9;
/*
* Book class
* field : 책이름, 출판사, 가격
* method :
* 생성자 : 기본, (책이름, 출판사, 가격), (책이름, 출판사), (책이름, 가격)
* setPrice() : 가격 저장 메소드 - 재료로 들어온 값을 가격에 저장, 재료가 음수일경우 0으로 저장
* getAll() : 모든 정보를 String으로 return
*
* =======================================
* Quiz01
* - book 객체를 4개의 배열로 생성하여 모두 생성자를 다르게 실행, 4개의 모든 객체 정보 for문통해 출력
*
*/
import javax.swing.JOptionPane;
class book{
String bookName;
String publicName;
int Price;
book(){
System.out.println("기본 생성자 호출");
}
book(String bookName,String publicName,int Price){
this(Price);
this.bookName = bookName;
this.publicName = publicName;
}
book(int Price) {
if(setPrice(Price)){
this.Price = Price;
}else{
System.out.println("책의 값은 음수가 될 수 없습니다.");
this.Price = 0;
}
}
book(String bookName,String publicName){
this.bookName = bookName;
this.publicName = publicName;
}
book(String bookName,int Price){
this(Price);
this.bookName = bookName;
}
Boolean setPrice(int Price){
return Price > 0 ? true : false;
}
String getAll(){
return "책이름 : " + this.bookName + "\n출판사 : " + this.publicName + "\n가격 : "+ this.Price+"원\n"+"/////////////////////////////////////////////////\n";
}
}
public class Quiz01 {
public static void main(String[] args) {
book[] b = {
new book(),
new book("해리포터-마법사의 돌","1출판사",1100),
new book("해리포터-비밀의 방","2출판사"),
new book("해리포터-아즈카반의 죄수",-16000)
};
String mes = "";
for(book tmp : b){
mes += tmp.getAll();
}
JOptionPane.showMessageDialog(null, mes);
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[JAVA] 생성자, this 사용 예제 (0) | 2023.04.21 |
---|---|
[JAVA] static 사용 예제 (1) | 2023.04.21 |
[JAVA] static class, final 사용 예제 (0) | 2023.04.21 |
[JAVA] package, 접근제어자 사용 예제 (0) | 2023.04.21 |
[JAVA] Inheritance(상속) 사용 예제 (0) | 2023.04.21 |