웹 개발 공부
자바 배열 예제
이전중동직장인
2022. 8. 16. 15:38
클래스 : Book
필드 : String title, String author, int page, boolean checkOut
정적필드 :static bookCount = 0
정적메소드 : static getBookCount() {}
메소드 :
디폴드 생성자 Book() // title="미정", author=“미상”, page=0, (boolean)checkOut=false
매개변수가 있는 생성자 Book(title, author, page) //(boolean) checkOut=false
getCheckOut () {} // checkOut의 값을 return
isCheckOut () {} //
checkOut 값이 true일 때 : false로 값을 바꾸고, “책을 반납하였습니다” 출력
false일 때 : true로 바꾸고 “책을 대여하였습니다” 출력
printBook () {} // checkOut이 false 일 때 "author의 title책이 현재 있습니다"
checkOut이 true "author의 title책이 대여중입니다“
1. booklist 배열을 만들어, 그 안에 Book 객체를 만들어서 넣자.
기능 :
1. 전체 책 개수 확인
2. 전체 책정보 출력
3. 인덱스를 입력하여 책 대여/반납
4. 책 추가
0. 종료
package aug16_SampleArray;
public class Book {
//필드
private String title;
private String author;
private int page;
private boolean checkOut;
// 정적필드
private static int bookCount =0;
//정적메소드
public static int getBookCount() {
return bookCount;
}
//생성자
//디폴트생성자
public Book() {
title="미정";
author="미상";
page=0;
checkOut=false;
}
public Book(String title, String author, int page, boolean checkOut)
{
this.title=title;
this.page=page;
this.author=author;
this.checkOut = false;
}
//메소드
public boolean getCheckOut() {
return this.checkOut;
}
public void ischeckout() {
if (checkOut = true) {
checkOut = false;
System.out.println("책을 반납하였습니다."); }
else {
checkOut = true;
System.out.println("책을 대여하였습니다.");
}
}
public void printBook() {
if(!checkOut) {
System.out.println(author + "의 "+ title +
"이 현재 있습니다.");
}
else {
System.out.println(author +"의 " + title + "책은 대여중입니다.");
}
}
}
출력 클래스
package aug16_SampleArray;
public class BookPrint {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book book1 = new Book ("홍길동전" , "허균", 100, false);
System.out.println(book1.getCheckOut());
book1.ischeckout();
book1.printBook();
}
}