1、运行演示
QQ2024528-205028-HD
详细讲解在这篇博客:JavaSE:图书管理系统-CSDN博客
2、所建的包
3、Java代码
3.1 book包
3.1.1 Book类代码
package book;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:16*/
public class Book {private String name;private String author;private int price;private String type;private boolean isBorrowed;public Book(String name, String author, String type, int price) {this.name = name;this.author = author;this.price = price;this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public boolean getBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", 状态:"+(isBorrowed ? " 该书已被借出" : "该书未被借出")+'}';}
}
3.1.2 BookList类代码
package book;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:19*/
public class BookList {private Book[] books = new Book[10];private int bookNumber;public BookList() {books[0] = new Book("哈利波特", "J.K.罗琳", "小说",30);books[1] = new Book("三国演义", "罗贯中","小说", 35);books[2] = new Book("红楼梦", "曹雪芹", "小说",25);books[3] = new Book("鲁宾逊漂流记", "丹尼尔·笛福", "小说",40);this.bookNumber = 4;}public int getBookNumber() {return bookNumber;}public void setBookNumber(int bookNumber) {this.bookNumber = bookNumber;}public Book getBook(int index) {Book book = books[index];return book;}public void setBooks(Book book,int index) {books[index] = book;}
}
3.2 operation包
3.2.1 AddBook类代码
package operation;import book.Book;
import book.BookList;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:33*/
public class AddBook implements IOperation {@Overridepublic void work(BookList bookList) {int currentNum = bookList.getBookNumber();Scanner scanner = new Scanner(System.in);System.out.println("*************Add Book*************");System.out.println("请输入您所想增添图书的书名:");String name = scanner.nextLine();System.out.println("请输入您所想增添图书的作者:");String author = scanner.nextLine();System.out.println("请输入您所想增添图书的类型:");String type = scanner.nextLine();System.out.println("请输入您所想增添图书的价格:");int price = scanner.nextInt();Book book = new Book(name,author,type,price);bookList.setBooks(book,currentNum);bookList.setBookNumber(currentNum+1);}
}
3.2.2 BorrowBook类代码
package operation;import book.Book;
import book.BookList;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:35*/
public class BorrowBook implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("*************Borrow Book*************");System.out.println("请输入您所要借阅图书的书名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentNum = bookList.getBookNumber();for (int i = 0; i < currentNum; i++) {Book book = bookList.getBook(i);if(name.equals(book.getName())) {if(book.getBorrowed() == true) {System.out.println("很抱歉!!!该书籍已被其他人借阅!!!");return;}else {book.setBorrowed(true);System.out.println("借阅成功!");return;}}}System.out.println("没有查找到该书籍!!!");}
}
3.2.3 DeleteBook类代码
package operation;import book.Book;
import book.BookList;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:34*/
public class DeleteBook implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("*************Borrow Book*************");System.out.println("请输入您所要删除图书的书名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentNum = bookList.getBookNumber();int index = -1;for (int i = 0; i < currentNum; i++) {Book book = bookList.getBook(i);if(name.equals(book.getName())) {index = i;break;}}if(index == -1) {System.out.println("没有查找到该图书!!!");}else {for (int i = index; i < currentNum-1; i++) {Book book = bookList.getBook(i+1);bookList.setBooks(book,i);}bookList.setBooks(null,currentNum);bookList.setBookNumber(currentNum-1);System.out.println("成功删除该图书!!!");}}
}
3.2.4 ExitSystem类代码
package operation;import book.BookList;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 16:22*/
public class ExitSystem implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!图书管理系统随时欢迎您的下次到来!");System.exit(0);}
}
3.2.5 FindBook类代码
package operation;import book.Book;
import book.BookList;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:34*/
public class FindBook implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("*************Find Book*************");System.out.println("请输入您所要查找图书的书名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentNum = bookList.getBookNumber();for (int i = 0; i < currentNum; i++) {Book book = bookList.getBook(i);if(name.equals(book.getName())) {System.out.println("查找到该书!该书相关信息如下:");System.out.println(book);return;}}System.out.println("没有查找到该书籍!!!");}
}
3.2.6 IOperation接口代码
package operation;import book.BookList;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:38*/
public interface IOperation {void work(BookList bookList);
}
3.2.7 ReturnBook类代码
package operation;import book.Book;
import book.BookList;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:35*/
public class ReturnBook implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("*************Return Book*************");System.out.println("请输入您所要归还图书的书名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentNum = bookList.getBookNumber();for (int i = 0; i < currentNum; i++) {Book book = bookList.getBook(i);if(name.equals(book.getName())) {if(book.getBorrowed()) {book.setBorrowed(false);System.out.println("图书归还成功!!!欢迎您的下次光临!!!");return;}else {System.out.println("查找到该书,但该书未被借出!!!");return;}}}System.out.println("没有查找到该书籍!!!");}
}
3.2.8 ShowBook类代码
package operation;import book.Book;
import book.BookList;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 17:21*/
public class ShowBook implements IOperation{@Overridepublic void work(BookList bookList) {int currentNum = bookList.getBookNumber();System.out.println("***************展示书籍***************");for (int i = 0; i < currentNum; i++) {Book book = bookList.getBook(i);System.out.println(book);}System.out.println("***************展示书籍***************");}
}
3.3 user包
3.3.1 User类代码
package user;import book.BookList;
import operation.*;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:30*/
public abstract class User {protected String name;IOperation[] iOperations;public User(String name) {this.name = name;}public abstract int menu();public void youOperate(int choice,BookList bookList) {this.iOperations[choice].work(bookList);}
}
3.3.2 Administrator类代码
package user;import operation.*;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:31*/
public class Administrator extends User {public Administrator(String name) {super(name);iOperations = new IOperation[]{new ExitSystem(),new AddBook(),new FindBook(),new ShowBook(),new DeleteBook()};}@Overridepublic int menu() {System.out.println("请输入您的操作:");System.out.println(" 1.添加书籍 2.查找书籍");System.out.println("3.展示书籍 4.删除书籍 0.退出系统");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}
}
3.3.3 NormalUser类代码
package user;import operation.*;import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:* User: dings* Date: 2024-05-13* Time: 13:30*/
public class NormalUser extends User {public NormalUser(String name) {super(name);iOperations = new IOperation[]{new ExitSystem(),new FindBook(),new BorrowBook(),new ReturnBook(),new ShowBook()};}@Overridepublic int menu() {System.out.println("请输入您的操作:");System.out.println(" 1.查找书籍 2.借阅书籍");System.out.println("3.归还书籍 4.展示书籍 0.退出系统");Scanner scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}
}
3.4 默认包
3.4.1 Main类代码
import book.BookList;
import user.Administrator;
import user.NormalUser;
import user.User;import java.util.Random;
import java.util.Scanner;/*** Created with IntelliJ IDEA.* Description:图书管理系统* User: dings* Date: 2024-05-13* Time: 13:18*/
public class Main {public static User login() {System.out.println("请输入您的姓名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("Hello! "+name+"! "+"欢迎来到图书系统!");System.out.println("请选择您的身份:1->图书管理员 2->普通用户");int choice = scanner.nextInt();if(choice == 1) {return new Administrator(name);}else {return new NormalUser(name);}}public static void main(String[] args) {BookList bookList = new BookList();User user = login();while (true) {int choice = user.menu();user.youOperate(choice,bookList);}}
}