欢迎浏览高耳机的博客
希望我们彼此都有更好的收获
感谢三连支持!
1.介绍
图书馆作为知识的殿堂和学术的中心,其管理系统不仅是图书馆管理的必备工具,更是为用户提供优质、高效服务的重要保障,促进了知识的传播和学术的发展。随着信息量的急剧增长和用户需求的多样化,传统的图书馆管理方式已经难以满足现代社会的需求。因此,引入一套高效、智能的图书馆管理系统变得尤为迫切。
在本篇博客中,我们将探讨如何构建一套易用便捷的图书馆管理系统。我们将从系统的需求分析开始,一步步领略图书馆管理系统背后的编程思想。相信本文能为您带来新的启发和思考。
2. 准备框架
首先,作为图书管理系统,应具有相应的 登录 以及 菜单 功能
登录功能,我们以简单的姓名属性来进行实现;
而身份的选择,决定了菜单中可使用的功能种类;
接着,当我们登录系统,系统首先会初始化书架中所有的书籍到内存中,以供我们实现需要的功能;
最后,在相应的身份下选择相应的功能,例如查找,借阅和归还书籍;
因此,需要以下包和类来搭建框架:
Book类,用于定义书籍属性,提供构造方法,get and set 方法,以及toString方法等实现;
BookList类,通过数组来存放书籍,实现初始化,并提供一些方法,例如数组下标;
operation包中包含了菜单中所有的操作选项,由IOperation作为接口;
User类作为AdminUser与NormalUser的父类,通过动态绑定来实现不同用户的不同操作;
3.代码实现
3.1 book
3.1.1 Book
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isLend;//是否被借出
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isLend() {
return isLend;
}
public void setLend(boolean lend) {
isLend = lend;
}
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
(isLend == true ? "已借出" : "未借出")+
//", isLend=" + isLend +
'}';
}
}
3.1.2 BookList
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList(){
books[0] = new Book("操作系统原理","阿罗哈",67,"教材");
books[1] = new Book("网络技术","阿罗呀",17,"教材");
books[2] = new Book("Linux嵌入式","阿罗哇",57,"教材");
this.usedSize = 3;//用来记录数组已用大小,以及寻找下标
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public boolean isFull(){
if(this.usedSize == books.length){
return true;
}
return false;
}
}
3.2 user
3.2.1 User
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] ioperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
//该方法是一个抽象方法,所以User类要是一个抽象类
//并且该方法需要在User的子类中实现
public void doIoperations(int choice, BookList bookList){
this.ioperations[choice].work(bookList);
}
}
3.2.2 AdminUser
package user;
import java.util.Scanner;
import operation.*;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu() {
System.out.println("*******普通用户菜单*******");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("***********************");
System.out.println("请输入你需要的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
3.2.3 NormalUser
package user;
import java.util.Scanner;
import operation.*;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};//将功能存入数组中,通过输入数组下标来实现对应功能
}
public int menu() {
System.out.println("*******普通用户菜单*******");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3。归还图书");
System.out.println("0.退出系统");
System.out.println("***********************");
System.out.println("请输入你需要的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
3.3 operation
3.3.1 IOperation
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
//通过该接口,所有接入该接口的类都将重写work方法
//通过动态绑定实现对应功能
3.3.2 AddOperation
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList){
if(bookList.isFull()){
System.out.println("书架满了,无法新增!");
return;
}
System.out.println("请输入新增的图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
System.out.println("请输入新增图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入新增图书的价格:");
int price = scanner.nextInt();
System.out.println("请输入新增图书的类型:");
String type = scanner.nextLine();
Book book = new Book(bookName,author,price,type);
int currentSize = bookList.getUsedSize();//3
bookList.setBook(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!");
}
}
3.3.3 BorrowedOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("输入借阅图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(bookName)) {
book.setLend(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("借阅失败!");
}
}
3.3.4 DelOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入你要删除的图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int pos = -1;
int i = 0;
for (; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
//找到这本书了 记录下标
pos = i;
break;
}
}
if(i >= currentSize) {
System.out.println("没有要找的书!");
return;
}
//开始删除了
for (int j = pos; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1];
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
//修改usedSize
bookList.setUsedSize(currentSize-1);
bookList.setBook(currentSize-1,null);
System.out.println("删除成功!");
}
}
3.3.5 ExitOperation
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统中......");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBook(i,null);
}
System.exit(0);
}
}
3.3.6 FindOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("请输入你要查找的图书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//Book book = bookList[i];
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
System.out.print("找到了这本书:");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的书....");
}
}
3.3.7 ReturnOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入你要归还的图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
book.setLend(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("归还失败!");
}
}
3.3.8 ShowOperation
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书......");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//Book book = bookList[i];
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
3.4 Main
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请选择你的身份,1.管理员用户 2.普通用户");
int choice = scanner.nextInt();
if(choice == 1){
AdminUser adminUser = new AdminUser(name);
return adminUser;
}else{
NormalUser normalUser = new NormalUser(name);
return normalUser;
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
//User有可能引用 管理员用户 有可能引用普通用户
User user = login();
while (true){
int choice = user.menu();
user.doIoperations(choice,bookList);
}
}
}
4.总结
该代码主要涉及了类和对象, 继承和多态,抽象类和接口这些编程思想
通过该项目的实战,可以对这些思想有着更加深刻的理解;
希望这篇博客能为你理解java编程思想提供一些帮助。
如有不足之处请多多指出。
我是高耳机。