- 도서관의 책을 관리하기 위한 프로그램 작성
- Magazine은 Book
- BookManager로 Book 등록/검색/수정/삭제
- BookManagerTest로 실행
[Book 클래스]
package com.book;
public class Book {
private String isbn;
private String title;
private String author;
private String publisher;
private double price;
private String desc;
// 생성자
public Book(String isbn, String title, String author, String publisher, double price, String desc) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
this.desc = desc;
}
// getter /setter
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.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;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String toString() {
return isbn+", "+title+ ", "+ author+ ", "+ publisher+ ", "+ price+ ", "+ desc;
}
}
[Magazine 클래스]
- Book를 부모 클래스로 Magazine 상속받음.
- Book 클래스의 isbn, title, author, publisher, price, desc를 물려받고, Magazine 고유한 필드인 year, month 를 생성자 설정.
package com.magazin;
import com.book.Book;
public class Magazine extends Book{
private int year;
private int month;
public Magazine(String isbn, String title, String author, String publisher, double price, String desc, int year,int month) {
super(isbn, title, author, publisher, price, desc);
this.year = year;
this.month = month;
}
public String toString() {
return super.toString()+ year+ ", "+ month;
}
}
[BookManager 클래스]
package com.bookmanager;
import com.book.Book;
import com.magazin.Magazine;
public class BookManager {
int numberOfBooks=0;
Book[] books = new Book[100];
/** 1) 도서관의 책 권수 리턴
* @return numberOfBooks
*/
public int getNumberOfBooks() {
return numberOfBooks;
}
/** 2) 책 등록
* @param book
*/
public void insertBook(Book book) {
books[numberOfBooks++]=book;
}
/** 3) 모든 책의 정보 리턴
* @return temp
*/
public Book[] getAllBook() {
Book[] temp=new Book[numberOfBooks];
for(int i=0;i<numberOfBooks;i++) temp[i]=books[i];
return temp;
}
/** 4) 북넘버로 검색
* @param isbn
* @return books[i]
*/
public Book searchBookByIsbn(String isbn) {
for(int i=0;i<numberOfBooks;i++) {
if(books[i].getIsbn().equals(isbn))
return(books[i]);
}
return null;
}
/** 5) 책 제목으로 검색하기
* @param title
* @return result
*/
public Book[] searchBookByTitle(String title) {
int count=0;
Book[] temp=new Book[numberOfBooks];
for(int i=0;i<numberOfBooks;i++) {
if(books[i].getTitle().contains(title)) {
temp[count] = books[i];
count++;
}
}
Book[] result=new Book[count];
for(int i=0;i<count;i++) result[i]=temp[i];
return result;
}
/** 6) 출판사로 책 찾기
* @param publisher
* @return result
*/
public Book[] searchBookByPublisher(String publisher) {
int count=0;
Book[] temp=new Book[numberOfBooks];
for(int i=0;i<numberOfBooks;i++) {
if(books[i].getPublisher().equals(publisher)) {
temp[count]=books[i];
count++;
}
}
Book[] result=new Book[count];
for(int i=0;i<count;i++) result[i]=temp[i];
return result;
}
/** 7) 책 가격으로 찾기
* @param price
* @return result
*/
public Book[] searchBookByPrice(double price) {
Book[] temp=new Book[numberOfBooks];
int count=0;
for(int i=0;i<numberOfBooks;i++) {
if((double)books[i].getPrice()==price) {
temp[count]=books[i];
count++;
}
}
Book[] result=new Book[count];
for(int i=0;i<count;i++) result[i]=temp[i];
return result;
}
/** 8) 모든 책 가격의 합 리턴
* @return sum
*/
public double getSumPriceOfBooks() {
double sum=0;
for(int i=0;i<numberOfBooks;i++) sum+=(books[i].getPrice());
return sum;
}
/** 8) 모든 책의 평균의
* @return avg
*/
public double getAveragePriceOfBooks() {
double avg=getSumPriceOfBooks()/numberOfBooks;
return avg;
}
}
[BookManger]
package com.bookmanager.test;
import com.book.Book;
import com.bookmanager.BookManager;
import com.magazin.Magazine;
public class BookManagerTest {
public static void main(String[] args) {
BookManager manager=new BookManager();
Book book1 = new Book("L811", "Little women", "Ann...", "Famous publisher", 18000.0, "masterpiece");
Book book2= new Book("H411", "History of Time", "Kal..", "Famous publisher", 25000.0, "masterpiece");
Book book3 = new Book("B411", "Bullet", "Kal..", "Famous publisher", 12000.0, "masterpiece");
Book book4 = new Book("L812", "Little women1", "Ann...", "Famous publisher", 18000.0, "masterpiece");
Book book5= new Book("H412", "History of Time1", "Kal..", "Famous publisher", 25000.0, "masterpiece");
Book book6 = new Book("B412", "Bullet1", "Kal..", "Famous publisher", 12000.0, "masterpiece");
Book magazine1 = new Magazine("N204", "Times", "marill..", "Powerful publisher", 11000.0, "must-have", 2020,4);
Book magazine2 = new Magazine("L812", "Korean Economy", "Kim..", "Good publisher", 25000.0, "Hot issue",2020,4);
Book magazine3 = new Magazine("N205", "Times1", "marill..", "Powerful publisher", 11000.0, "must-have", 2020,4);
Book magazine4 = new Magazine("L813", "Korean Economy1", "Kim..", "Good publisher", 25000.0, "Hot issue",2020,4);
Book[] books = {book1,book2,book3,book4,book5,magazine1,magazine2,};
System.out.println("=================1) getNumberOfBooks(책의 개수)=================");
System.out.println(manager.getNumberOfBooks());
System.out.println("=================2) insertBook(책 삽입)=================");
for (Book b : books) manager.insertBook(b);
manager.insertBook(magazine3);
manager.insertBook(magazine4);
manager.insertBook(book6);
System.out.println("=================3) getAllBook(모든 책의 정보)=================");
for(Book book:manager.getAllBook())
System.out.println(book);
System.out.println("=================4) searchBookByIsbn(북넘버로 서치)=================");
System.out.println(manager.searchBookByIsbn("B411"));
System.out.println("=================5)searchBookByTitle(북타이틀로 서치)=================");
for(Book book:manager.searchBookByTitle("Time"))
System.out.println(book);
System.out.println("=================6) searchBookByPublisher(출판사로 서치)=================");
for(Book book:manager.searchBookByPublisher("Famous publisher"))
System.out.println(book);
System.out.println("=================7) searchBookByPrice(책값으로 서치)=================");
for(Book book:manager.searchBookByPrice(25000.0))
System.out.println(book);
System.out.println("=================8) getSumPriceOfBooks(총 책값)=================");
System.out.println(manager.getSumPriceOfBooks());
System.out.println("=================9) getAveragePriceOfBooks(총 책값)=================");
System.out.println(manager.getAveragePriceOfBooks());
}
}
'Web Programming > Code' 카테고리의 다른 글
[Workshop] 영화 등록/업데이트/검색 (0) | 2020.05.03 |
---|
댓글