자바 소형 도서관 관리 시스템 실현
다음은 소형 도서관 관리 시스템 모델 그림 이다.
모델 전체 개요:
그 중에서 IBorrower 인 터 페 이 스 는 Borrower 류 의 기준 을 확정 하고 Library 류 는 하나의 디자인 모델 로 도서관 은 하나 밖 에 없다.Book 클래스 는 Borrower 클래스 의 내부 클래스 이 고,library Book 클래스 는 Library 클래스 의 내부 클래스 입 니 다.마지막 으로 PlatForm 류 를 이용 하여 한층 더 포장 합 니 다.클래스 방법 중의 핵심 은 주로 배열 의'삭제 검사 수정'작업 이다.본 프로젝트 에서 세 번 이나 이런 조작 을 활용 하여 각각 Borrower,Library,PlatForm 류 방법 에 있다.
IBorrower 인터페이스:
// package com.wu.demo;
/**
* @date 2020-10-30 10:08:48
* @author
* @version 1.8.0
*/
//
public interface IBorrower {
public abstract void borrowBook(String bookName,String bookAuthor,float bookPrice,Library libary);
public abstract void returnBook(String bookName,String bookAuthor,float bookPrice,Library libary);
}
Borrower 클래스:
// package com.wu.demo;
/**
* @date 2020-10-30 10:11:00
* @author
* @version 1.8.0
*/
public class Borrower implements IBorrower{
protected String borrowerName;
protected long borrowerNumber;
private int booksNumber;
private static final int MAX_NUMBER_OF_BOOKS = 4; //
private Book[] books = new Book[MAX_NUMBER_OF_BOOKS];
public Borrower(String borrowerName,long borrowerNumber) {
this.borrowerName = borrowerName;
this.borrowerNumber = borrowerNumber;
this.booksNumber = 0;
}
private class Book{
private String bookName;
private String bookAuthor;
private float bookPrice;
public Book(String bookName,String bookAuthor,float bookPrice){
Book.this.bookName = bookName;
Book.this.bookAuthor = bookAuthor;
Book.this.bookPrice = bookPrice;
}
public String getName() {
return Book.this.bookName;
}
public String getAuthor() {
return Book.this.bookAuthor;
}
public float getPrice() {
return Book.this.bookPrice;
}
}
@Override
/**
*
*/
public void borrowBook(String bookName,String bookAuthor,float bookPrice,Library library) {
if(booksNumber>=MAX_NUMBER_OF_BOOKS) {
System.out.println(" ");
return;
}
books[booksNumber++] = new Book(bookName,bookAuthor,bookPrice);
// ,
library.removeBook( bookName,bookAuthor, bookPrice);
}
@Override
/**
*
*/
public void returnBook(String bookName,String bookAuthor,float bookPrice,Library library) {
//
if(booksNumber<=0) {
return;
}
int index = -1;
for(int i = 0 ; i < books.length ; i++) {
if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor)
&& books[i].bookPrice == bookPrice) {
index = i;
break;
}
}
if(index < 0) {
System.out.println(" ");
return;
}
for(int i = 0; i < booksNumber-1 ; i++) {
if(i>=index) {
books[i] = books[i+1];
}
}
booksNumber --;
// ,
library.addBook(bookName,bookAuthor,bookPrice);
}
/**
*
*/
public void borrowerInfo() {
int i = 0;
System.out.println("------------------------------ ------------------------------");
System.out.println(" :"+this.borrowerName);
System.out.println(" :"+this.borrowerNumber);
for(;i < booksNumber ; i++) {
System.out.println(" "+(i+1)+" : ");
System.out.println(" :"+books[i].getName());
System.out.println(" :"+books[i].getAuthor());
System.out.println(" :"+books[i].getPrice());
}
System.out.println(" : "+booksNumber+" : "+(MAX_NUMBER_OF_BOOKS-i));
System.out.println("------------------------------------------------------------------------");
}
}
라 이브 러 리 클래스:
// package com.wu.demo;
/**
* @date 2020-10-30 10:32:42
* @author
* @version 1.8.0
*/
public class Library {
private static final Library library = new Library();
private static final int MAX_NUMBER_OF_BOOKS = 120; //
private libraryBook[] books = new libraryBook[MAX_NUMBER_OF_BOOKS];
protected static int booksNumber = 0;
//
private Library() {};
private class libraryBook{
private String bookName;
private String bookAuthor;
private float bookPrice;
public libraryBook(String bookName,String bookAuthor,float bookPrice){
libraryBook.this.bookName = bookName;
libraryBook.this.bookAuthor = bookAuthor;
libraryBook.this.bookPrice = bookPrice;
}
public void setName(String bookName) {
libraryBook.this.bookName = bookName;
}
public void setAuthor(String bookAuthor) {
libraryBook.this.bookAuthor = bookAuthor;
}
public void setPrice(float bookPrice) {
libraryBook.this.bookPrice = bookPrice;
}
}
//
static Library getLibrary() {
return library;
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void addBook(String bookName,String bookAuthor,float bookPrice) {
if(booksNumber>=MAX_NUMBER_OF_BOOKS) {
System.out.println(" ");
return;
}
books[booksNumber++] = new libraryBook(bookName,bookAuthor,bookPrice);
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void removeBook(String bookName,String bookAuthor,float bookPrice) {
int index = -1;
for(int i = 0 ; i < booksNumber ; i++) {
if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
books[i].bookPrice == bookPrice) {
index = i;
break;
}
}
if(index < 0) {
System.out.println(" ");
return;
}
for(int i = 0;i < booksNumber - 1;i++) {
if(index <= i) {
books[i] = books[i+1];
}
}
booksNumber --;
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void checkBook(String bookName,String bookAuthor,float bookPrice) {
System.out.println("");
for(int i = 0 ; i<booksNumber ; i++) {
if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
books[i].bookPrice == bookPrice) {
System.out.println(" : ");
return;
}
}
System.out.println(" : ");
System.out.println("");
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void changeBook(String bookName, String bookAuthor,float bookPrice,String newBookName,
String newBookAuthor,float newBookPrice) {
for(int i = 0 ;i<booksNumber;i++) {
if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
books[i].bookPrice == bookPrice) {
books[i].setName(newBookName);
books[i].setAuthor(newBookAuthor);
books[i].setPrice(newBookPrice);
break;
}
}
System.out.println(" ");
}
/**
*
*/
public void booksInfo() {
System.out.println("----------------------------- -----------------------------");
for(int i = 0 ; i<booksNumber;i++) {
System.out.println(" "+(i+1)+" : ");
System.out.println(" :"+books[i].bookName);
System.out.println(" :"+books[i].bookAuthor);
System.out.println(" :"+books[i].bookPrice);
}
System.out.println(" :"+booksNumber+" :"+(MAX_NUMBER_OF_BOOKS-booksNumber));
System.out.println("--------------------------------------------------------------------------");
}
}
PlatForm 클래스:
// package com.wu.demo;
import java.util.Scanner;
/**
* @date 2020-10-30 14:06:34
* @author
* @version 1.8.0
*/
//
public class PlatForm {
private static final int MAX_NUMBER_OF_BORROWERS = 30; // 30
private Borrower[] borrowers = new Borrower[MAX_NUMBER_OF_BORROWERS];
private static int numberBorrowers = 0;
private Library library = Library.getLibrary();
public PlatForm() {
Scanner input = new Scanner(System.in);
// Borrower
System.out.println("----------------------------- -----------------------------");
System.out.println(" ( Q q ):");
String exit = input.nextLine();
System.out.println(" ");
while(!exit.equalsIgnoreCase("q")) {
System.out.print(" :");
String tempName = input.next();
long tempNumber = input.nextLong();
this.borrowers[numberBorrowers++] = new Borrower(tempName,tempNumber);
//
input.nextLine();
System.out.println(" ( Q q ):");
exit = input.nextLine();
if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS){
System.out.println(" ");
break;
}
}
//
System.out.println("--------------------------- ------------------------");
System.out.println(" ( Q q ):");
exit = input.nextLine();
System.out.println(" :");
while(!exit.equalsIgnoreCase("q")) {
System.out.print(" :");
String tempName = input.next();
String tempAuthor = input.next();
float tempPrice = input.nextFloat();
library.addBook(tempName,tempAuthor,tempPrice);
//
input.nextLine();
System.out.println(" ( Q q ):");
exit = input.nextLine();
}
System.out.println("-----------------------------------------------------------------------");
input.close();
}
/**
* :
* @param borrowerName
* @param borrowerNumber
*/
public void addClient(String borrowerName,long borrowerNumber) {
if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS) {
System.out.println(" ");
return;
}
this.borrowers[numberBorrowers++] = new Borrower(borrowerName,borrowerNumber);
System.out.println(" ");
}
/**
* :
* @param borrowerName
* @param borrowerNumber
*/
public void removeClient(String borrowerName,long borrowerNumber) {
int index = -1;
for(int i = 0 ; i<numberBorrowers; i++) {
if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
index = i;
break;
}
}
if(index<0) {
System.out.println(" ");
return;
}
for(int i = 0 ; i < numberBorrowers-1;i++) {
if(index <= i) {
borrowers[i] = borrowers[i+1];
}
}
numberBorrowers--;
System.out.println(" ");
}
/**
*
* @param borrowerName
* @param borrowerNumber
*/
public void platformBorrowerInfo(String borrowerName,long borrowerNumber) {
for(int i = 0 ; i<numberBorrowers;i++) {
if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
borrowers[i].borrowerInfo();
return;
}
}
System.out.println(" ");
}
/**
*
* @param borrowerName
* @param borrowerNumber
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void platformBorrowBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor,
float bookPrice) {
for(int i = 0 ; i<borrowers.length;i++) {
if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
borrowers[i].borrowBook(bookName,bookAuthor, bookPrice, this.library);
break;
}
}
}
/**
*
* @param borrowerName
* @param borrowerNumber
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void platformRrturnBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor,
float bookPrice){
for(int i = 0 ; i<borrowers.length;i++) {
if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
borrowers[i].returnBook(bookName,bookAuthor, bookPrice, library);
break;
}
}
}
/**
*
*/
public void platformLibraryInfo() {
library.booksInfo();
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void platformLibraryAddBook(String bookName,String bookAuthor,float bookPrice) {
library.addBook(bookName,bookAuthor,bookPrice);
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void platformLibraryRemoveBook(String bookName,String bookAuthor,float bookPrice) {
library.removeBook(bookName,bookAuthor,bookPrice);
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
*/
public void platformLibraryCheckBook(String bookName,String bookAuthor,float bookPrice) {
library.checkBook(bookName, bookAuthor, bookPrice);
}
/**
*
* @param bookName
* @param bookAuthor
* @param bookPrice
* @param newBookName
* @param newBookAuthor
* @param newBookPrice
*/
public void platformLibraryChangeBook(String bookName,String bookAuthor,float bookPrice,
String newBookName,String newBookAuthor,float newBookPrice) {
library.changeBook(bookName, bookAuthor, bookPrice,newBookName,newBookAuthor,newBookPrice);
}
}
testDemo(테스트):
// package com.wu.demo;
/**
* @date 2020-10-30 10:51:21
* @author
* @version 1.8.0
*/
public class testDemo {
public static void main(String[] args) {
PlatForm platform = new PlatForm();
//
platform.addClient(" ", 100000L);
platform.addClient(" ", 100001L);
platform.addClient(" ", 100002L);
platform.addClient(" ", 100003L);
platform.platformBorrowerInfo(" ",100000L);
platform.platformBorrowerInfo(" ",100000L); //
platform.platformBorrowerInfo(" ",100002L);
platform.platformBorrowerInfo(" ",100003L);
platform.removeClient(" ", 100002L);
platform.platformBorrowerInfo(" ",100002L); //
//
platform.platformLibraryAddBook(" "," 1",100.0F);
platform.platformLibraryAddBook(" "," 2",90.0F);
platform.platformLibraryAddBook(" "," 3",110.0F);
platform.platformLibraryInfo(); // 3
platform.platformLibraryAddBook("java "," 4",105.0F);
platform.platformLibraryAddBook(" "," 5",129.0F);
platform.platformLibraryAddBook("Linux "," 6",104.0F);
platform.platformLibraryAddBook("matlab "," 7",123.0F);
platform.platformLibraryAddBook("visual C++ "," 8",95.0F);
platform.platformLibraryAddBook("HTML CSS JS"," 9",99.0F);
platform.platformLibraryAddBook("C++ "," 10",119.0F);
platform.platformLibraryAddBook("python "," 11",108.0F);
platform.platformLibraryAddBook(" "," 12",120.0F);
platform.platformLibraryAddBook(" "," 13",106.0F);
platform.platformLibraryInfo(); // 13
platform.platformLibraryRemoveBook("visual C++ "," 8",95.0F);
platform.platformLibraryInfo(); // 12
platform.platformLibraryRemoveBook("Linux " ," 6", 104.0F); // Linux
platform.platformLibraryChangeBook("python ", " 11", 108.0F,"keras "," 11",110.0F); // python keras
platform.platformBorrowBook(" ", 100000L, "java ", " 4", 105.0F); // java
platform.platformBorrowBook(" ", 100000L, " ", " 3", 110.0F); //
platform.platformBorrowBook(" ", 100000L, "C++ ", " 10", 119.0F); // C++
platform.platformBorrowBook(" ", 100000L, " ", " 5", 129.0F); //
platform.platformBorrowBook(" ", 100003L, " ", " 2", 84.0F); //
platform.platformBorrowBook(" ", 100001L, "HTML CSS JS", " 9", 119.0F); // HTML CSS JS
platform.platformBorrowerInfo(" ", 100000L); //
platform.platformBorrowerInfo(" ", 100003L); //
platform.platformBorrowerInfo(" ", 100001L); //
platform.platformRrturnBook(" ", 100000L, " ", " 3", 110.0F); //
platform.platformRrturnBook(" ", 100000L, " ", " 5", 129.0F); //
platform.platformBorrowerInfo(" ", 100000L); //
platform.platformLibraryRemoveBook("java " ," 4", 105.0F); // java
}
}
테스트 결과:더 많은 학습 자 료 는 주제 인 에 주목 하 세 요.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.