⭐ 作者:小胡_不糊涂
🌱 作者主页:小胡_不糊涂的个人主页
📀 收录专栏:浅谈Java
💖 持续更文,关注博主少走弯路,谢谢大家支持 💖
图书管理系统
- 1. 设计思路图
- 2. 创建 book 包
- 2.1 Book 类
- 2.2 BookList 类
- 3. 创建 operation 包
- 3.1 FindOperation 类-查找图书
- 3.2 AddOperation 类-增加图书
- 3.3 DelOperation 类-删除图书
- 3.4 ShowOperation 类-显示图书
- 3.5 BorrowOperation 类-借阅图书
- 3.6 ReturnOperation 类-归还图书
- 3.5 ExitOperation 类-退出系统
- 4. 创建 user 包
- 4.1 User 类
- 4.2 NormUser 类
- 4.3 AdminUser 类
- 5. 建立 Main 类
1. 设计思路图
图书管理系统的使用者有两类,一类是图书管理员,一类是借书用户。
他们在管理系统中的需求与操作是不同的,所以要分别进行设计。
2. 创建 book 包
建立一个 book 包,用来存放与书相关的内容。
2.1 Book 类
Book 类中存放与书有关的属性,而一本书的属性有很多,比如:书名、作者、价格、类型、编号…
代码实现:
//属性private String name;//书名private String author;//作者private double price;//价格private String type;//类型private boolean isBorrowed;//是否被借出
对以上私有的属性提供 Getter and Setter
方法:
右击选择
Generate
,点击Getter and Setter
方法,然后选择所有属性
得到:
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 double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}
构造书的对象(没有必要写书是否被借出,只需提供书的信息即可):
右击选择
Generate
,点击Constructor
方法,然后选择属性
得到:
//构造书的对象--没必要写书是否被借出public Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}
打印一本书:
右击选择
Generate
,点击toString
方法,然后选择所有属性
得到:
//打印图书@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price='" + price + '\'' +", type='" + type + '\'' +((isBorrowed == true) ? " 已经借出" : " 未被借出") +'}';}
整合代码:
package book;
//存放与书相关的
public class Book {//属性private String name;//书名private String author;//作者private double price;//价格private String type;//类型private boolean isBorrowed;//是否被借出//构造书的对象--没必要写书是否被借出public Book(String name, String author, double price, String type) {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 double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}//打印图书@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price='" + price + '\'' +", type='" + type + '\'' +", isBorrowed=" + isBorrowed +'}';}
}
2.2 BookList 类
BookList 类(书架)用来存放书与借书,而对于书架上的书,我们需要知道一个书架能放多少本书,有几本书未被借出,书架上所存放的书都有哪些,书架上还能继续存放几本书等。
基于这些问题,我们先定义一个 books
数组来控制书架上存放书的数量,
private Book[] books ;private int usedSize;//记录当前书架上 实际存放的书的数量private static final int DEFAULT_CAPACITY = 10;public BookList() {this.books = new Book[DEFAULT_CAPACITY];//初始化书的数量}
因为usedSize
是private修饰的,我们就可以提供Getter and Setter
方法:
public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}
而对于books
数组,当我们想获取或设置某个下标的书时,也可以提供Getter and Setter
方法,但是需要进行改进:
public Book getBook(int pos) {return books[pos];//返回书的位置}public void setBooks(int pos,Book book) {books[pos] = book;//将books的pos位置存放一本书}
提前存入一些书进去:
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.usedSize = 3;
整合代码:
package book;
//书架
public class BookList {private Book[] books ;private int usedSize;//记录当前书架上 实际存放的书的数量private static final int DEFAULT_CAPACITY = 10;public BookList() {this.books = new Book[DEFAULT_CAPACITY];//初始化书的数量//放好书!!this.books[0] = new Book("三国演义","罗贯中",10,"小说");this.books[1] = new Book("西游记","吴承恩",9,"小说");this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");this.usedSize = 3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book getBook(int pos) {return books[pos];}public void setBooks(int pos,Book book) {books[pos] = book;}public Book[] getBooks() {return books;}
}
3. 创建 operation 包
存放操作相关的内容:
管理员:查找图书、增加图书、删除图书、显示图书、退出系统
用户:查找图书、借阅图书、归还图书、退出系统
其中查找图书、退出系统两者都具备,可以在一个类中进行操作。
在上述操作中都需要对 BookList
进行操作,为了统一管理,我们在 operation
包内定义一个接口 IOPeration
,通过IOPeration
就可以对这些操作进行组织,从而实现了接口的统一性:
在该接口中定义一个 work
方法,对书架上的书进行统一管理:
package operation;
import book.BookList;
public interface IOPeration {void work(BookList bookList);
}
3.1 FindOperation 类-查找图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;public class FindOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("查找图书!");//通过书名查找图书Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();//遍历这个数组int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)) {System.out.println("找到了这本书,信息如下:");System.out.println(book);return;}}`在这里插入代码片`System.out.println("没有找到这本书!");}
}
3.2 AddOperation 类-增加图书
import book.Book;
import book.BookList;
import java.util.Scanner;public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书!");Scanner scanner = new Scanner(System.in);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,price,type);//检查 数组当中 有没有这本书int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book1 = bookList.getBook(i);if(book1.getName().equals(name)) {System.out.println("有这本书,不进行存放了!");return;}}if(currentSize == bookList.getBooks().length) {System.out.println("书架满了!");}else {bookList.setBooks(currentSize,book);bookList.setUsedSize(currentSize+1);}}
}
3.3 DelOperation 类-删除图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;public class DelOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入你要删除的图书:");String name = scanner.nextLine();int pos = -1;int currentSize = bookList.getUsedSize();int i = 0;for (; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)) {pos = i;break;}}if(i == currentSize) {System.out.println("没有你要删除的图书!");return;}//开始删除int j = pos;for (; j < currentSize-1; j++) {//[j] = [j+1]Book book = bookList.getBook(j+1);bookList.setBooks(j,book);}bookList.setBooks(j,null);bookList.setUsedSize(currentSize-1);}
}
3.4 ShowOperation 类-显示图书
因为我们的书都是在数组中保存,所以可以通过数组元素的打印方法,来显示所有图书:
package operation;
import book.Book;
import book.BookList;public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("显示图书!");int currentSize = bookList.getUsedSize();//获取书架上书的数量for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);System.out.println(book);}}
}
3.5 BorrowOperation 类-借阅图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;public class BorrowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书!");/*** 1. 你要借阅哪本书?* 2. 你借阅的书有没有?* 3. 借阅的方式是什么? -》 isB.... = true*/Scanner scanner = new Scanner(System.in);System.out.println("请输入你要借阅的图书:");String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)) {book.setBorrowed(true);System.out.println("借阅成功!");System.out.println(book);return;}}System.out.println("你借阅的图书 不存在!! ");}
}
3.6 ReturnOperation 类-归还图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入你要归还的图书:");String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)) {book.setBorrowed(false);System.out.println("归还成功!");System.out.println(book);return;}}System.out.println("你归还的图书 不存在!! ");}
}
3.5 ExitOperation 类-退出系统
package operation;
import book.BookList;public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);}
}
4. 创建 user 包
用来维护使用者。对于该系统来讲,使用者有管理员和普通用户,所以在 user 包中我们分别建立用户类(User)、管理员类(AdminUser)和普通用户类(NormUser)。
User用来存放和声明AdminUser和NormUser两类中相同的一些属性与方法,而不同的方法分别在各类中进行重写即可,比如:两者的使用时所显示的菜单以及各自所调用的一些功能。
4.1 User 类
管理员和用户在进行系统操作时,会有一些相同的操作,在User类中就会实现他们之间共有的属性与方法。
先在User类中先定义一个name成员:
package user;
public abstract class User {protected String name;//只能User包中的子类访问//构造方法public User(String name) {this.name = name;}public abstract int menu();//声明menu方法,没有具体实现的抽象方法}
创建doOperation方法,用来调用用户需要进行的操作:
public void doOperation(int choice, BookList bookList){ioPerations[choice].work(bookList);}
整合代码:
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();//声明menu方法,没有具体实现的抽象方法public void doOperation(int choice, BookList bookList){ioPerations[choice].work(bookList);}
}
4.2 NormUser 类
与AsminUser相同的一些操作已经在User类中说明,所以这里只需继承父类(User)即可:
public class NormalUser extends User{//帮助父类进行构造,对父类中的name成员进行初始化public NormalUser(String name) {super(name);};
}
设计用户菜单:
在每次打印完菜单后,提醒用户输入所需操作对应的数字,并返回对应操作数。
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("**********************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}
因为继承了User类,需要帮助父类初始化ioPerations,所以在该数组中存入属于普通用户的操作,且放入顺序与对应的操作数字一致:
this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()
};
整合代码:
package user;
import operation.*;
import java.util.Scanner;public class NormalUser extends User{public NormalUser(String name) {super(name);this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),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("**********************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}
}
4.3 AdminUser 类
继承User类:
public class AdminUser extends User{//帮助父类进行构造,对父类中的name成员进行初始化public AdminUser(String name) {super(name);};
}
设计管理者菜单:
提醒管理员输入所需操作对应的数字,并返回对应操作数。
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("**********************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}
初始化父类ioPerations:
this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()
};
整合代码:
package user;
import operation.*;
import java.util.Scanner;public class NormalUser extends User{public NormalUser(String name) {super(name);this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),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("**********************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}
}
5. 建立 Main 类
Main类的作用:联合三个包中的方法实现程序运行。
设置一个用户登录界面。先输入使用者的姓名,然后选择自己的身份(1–>管理员 2–>普通用户),通过所选数字的不同,使系统调用两类用户各自所属的方法。
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;public class Main {public static User login(){System.out.println("请输入你的姓名:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();System.out.println("请输入你的身份:1->管理员 2->普通用户");int choice =scanner.nextInt();if(choice==1){//管理员return new AdminUser(name);}else{//普通用户return new NormalUser(name);}}public static void main(String[] args) {User user=login();//向上转型,user指向哪个对象要看返回值是什么int choice=user.menu();//动态绑定,要通过user调用menu方法,就要在User先声明。并用choice接收返回的操作数}
}
根据上述choice的值可以选择调用哪个方法,因此我们创建一个 doOperation 方法来调用用户想要执行的操作:
BookList bookList=new BookList();
user.doOperation(choice,bookList);
整合代码:
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;public class Main {public static User login(){System.out.println("请输入你的姓名:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();System.out.println("请输入你的身份:1->管理员 2->普通用户");int choice =scanner.nextInt();if(choice==1){//管理员return new AdminUser(name);}else{//普通用户return new NormalUser(name);}}public static void main(String[] args) {User user=login();//向上转型,user指向哪个对象要看返回值是什么//根据choice选择来调用哪个方法BookList bookList=new BookList();while(true) {int choice = user.menu();//动态绑定,要通过user调用menu方法,就要在User先声明。并用choice接收返回的操作数user.doOperation(choice, bookList);}}
}