目录
以管理员身份进入图书管理系统
显示图书
查找图书
增加图书
再次显示图书
删除图书
再次显示图书
退出系统
以普通成员身份进入图书管理系统
借阅图书
查找图书
归还图书
再次查找图书
我们要实现图书管理系统首先必不可少的是要确定有哪几个对象,每个对象都放入一个包当中。
图书管理系统的对象有:图书,使用者。所以我们这里就可以分俩个包。为了方便理解,我们也可以把使用者的一些操作单独放入一个包当中
包分配好了之后,我们就可以在包中写每个包中的具体成员了。
比如book包,我们可以有图书类和用于存放图书的书架类
user包,我们可以有管理员类,普通成员类
operation包中就可以有一些操作:查找图书,增加图书,删除图书,显示图书,借阅图书,归还图书,退出图书管理系统。
因为不管是管理员还是普通成员,都是使用者,这里我们可以写一个User类,让管理员和普通成员继承它,方便以后的一些操作。
因为这些方法都是对书架上的图书进行一些操作,所以我们可以写一个接口来作为一种规范。让其它方法都继承它。
- 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 username = scanner.nextLine();
- System.out.println("请输入你的身份:1 - 管理员,0 - 普通用户");
- int choice = scanner.nextInt();
- if(choice == 1){
- return new AdminUser(username);
- }else{
- return new NormalUser(username);
- }
- }
- public static void main(String[] args) {
- //准备数据
- BookList bookList = new BookList();
- //登录
- User user = login();
- while(true) {
- int choice = user.menu();
- user.doOperation(choice, bookList);
- }
- }
- }
- package user;
-
- import book.BookList;
- import operation.IOperation;
-
- public abstract class User {
- protected String name;
- protected IOperation[] ioperation;//只是定义了一个数组,没有初始化和分配内存
- public User(String name){
- this.name = name;
- }
- public abstract int menu();
- public void doOperation(int choice, BookList bookList){
- ioperation[choice].work(bookList);
- }
- }
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class AdminUser extends User {
- public AdminUser(String name){
- super(name);
- this.ioperation = new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new AddOperation(),
- new DelOperation(),
- new DispalyOperation(),
- };
- }
- public int menu(){
- System.out.println("***********************************");
- System.out.println("hello"+name+"欢迎来到图书小练习");
- System.out.println("0.退出系统");
- System.out.println("1.查找图书");
- System.out.println("2.新增图书");
- System.out.println("3.删除图书");
- System.out.println("4.显示图书");
- System.out.println("***********************************");
- System.out.println("请输入你的操作:");
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
- package user;
-
- import operation.*;
-
- import java.util.Scanner;
-
- public class NormalUser extends User {
- public NormalUser(String name){
- super(name);
- this.ioperation = new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new BorrowOperation(),
- new ReturnOperation(),
- };
- }
- public int menu(){
- System.out.println("***********************************");
- System.out.println("hello"+name+"欢迎来到图书小练习");
- System.out.println("0.退出系统");
- System.out.println("1.查找图书");
- System.out.println("2.借阅图书");
- System.out.println("3.归还图书");
- System.out.println("***********************************");
- System.out.println("请输入你的操作:");
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
- package book;
-
- public class Book {
- private String name;//书名
- private String author;//作者
- private int price;//价格
- private String type;//种类
- private boolean isBorrowed;//是否被借出
-
- 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 + '\'' +
- ((isBorrowed == true)?"已借出":"未借出")+
- '}';
- }
-
- 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 isBorrowed() {
- return isBorrowed;
- }
-
- public void setBorrowed(boolean borrowed) {
- isBorrowed = borrowed;
- }
- }
- package book;
-
- public class BookList {
- Book[] books = new Book[10];
- private int usedsize = 10;
- //预先存放几本书
- public BookList(){
- books[0] = new Book("三国演义","罗贯中",50,"小说");
- books[1] = new Book("西游记","吴承恩",40,"小说");
- books[2] = new Book("红楼梦","曹雪芹",50,"小说");
- usedsize = 3;
- }
-
- public int getUsedsize() {
- return usedsize;
- }
-
- public void setUsedsize(int usedsize) {
- this.usedsize = usedsize;
- }
- public Book getPos(int pos){
- return books[pos];
- }
- //存储一本书到指定的位置
- //当前最后可以存储的位置
- public void setBooks(Book book,int pos){
- books[pos] = book;
- }
- }
- package operation;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("查找图书");
- 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.getPos(i);
- if(name.equals(book.getName())){
- System.out.println("找到了");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有这本书");
- }
- }
- package operation;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class AddOperation implements IOperation {
- @Override
- public void work(BookList bookList) {
- System.out.println("增加图书");
- System.out.println("请输入书的名字");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- System.out.println("请输入书的作者");
- String author = scanner.nextLine();
- System.out.println("请输入书的价格");
- int price = scanner.nextInt();
- scanner.nextLine();//读enter建
- System.out.println("请输入书的类型");
- String type = scanner.nextLine();
- Book book = new Book(name,author,price,type);
- int currentSize = bookList.getUsedsize();
- bookList.setBooks(book,currentSize);
- currentSize++;
- bookList.setUsedsize(currentSize);
- }
- }
- 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("删除图书");
- System.out.println("请输入你要删除的图书:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int n = -1;
- int currentSize = bookList.getUsedsize();
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- n = i;
- break;
- }
- }
- if(n == -1){
- System.out.println("没有你要删除的这本书");
- return;
- }
- for (int i = n; i < currentSize-1; i++) {
- Book book = bookList.getPos(i+1);
- bookList.setBooks(book,i);
- }
- //每次删除都要把最后一本书置空
- bookList.setBooks(null,currentSize-1);
- bookList.setUsedsize(currentSize-1);
- System.out.println("删除成功");
- }
- }
- package operation;
-
- import book.Book;
- import book.BookList;
-
- public class DispalyOperation 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.getPos(i);
- System.out.println(book);
- }
- }
- }
- package operation;
-
- import book.BookList;
-
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出系统");
- System.exit(0);
- }
- }
- package operation;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class BorrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("借阅图书");
- System.out.println("请输入你要借阅的图书");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedsize();
- for (int i = 0; i
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- if(book.isBorrowed() == true){
- System.out.println("该书已经被借出");
- return;
- }else{
- book.setBorrowed(true);
- System.out.println("借书成功");
- return;
- }
- }
- }
- System.out.println("没有你要借的图书");
- }
- }
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("归还图书");
- System.out.println("请输入你要归还的图书");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedsize();
- for (int i = 0; i
- Book book = bookList.getPos(i);
- if(name.equals(book.getName())){
- if(book.isBorrowed() == true){
- book.setBorrowed(true);
- System.out.println("还书成功");
- return;
- }else{
- System.out.println("该书已被归还");
- return;
- }
- }
- }
- System.out.println("该书不是本图书馆的书,无需归还");
- }
- }
-
相关阅读:
API文档转实体类脚本
API之 要求接口上传pdf 以 合同PDF的二进制数据,multpart方式上传
Python 基于docker部署的Mysql备份查询脚本
机器人制作开源方案 | 齿轮传动轴偏心轮摇杆简易四足
尚硅谷大叔培训:揭秘Flink四种执行图——ExecutionGraph和物理执行图
【MySQL】表的增删改查(一)
rar格式转换zip格式,如何做?
WPF 控件专题 Ellipse详解
Android 11.0 系统Settings app详情页增加统计使用时长功能
C# Onnx LSTR 基于Transformer的端到端实时车道线检测
-
原文地址:https://blog.csdn.net/qq_62712350/article/details/126542909