Java---图书管理系统(练习版)

1.主函数

java 复制代码
import book.Book;
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) {
        BookList bookList=new BookList();
        User user=Login();
        //根据用户输入的身份和输入的数字实现相对应的操作
       while(true){
           int choice= user.menu();
           user.doIoperation(choice,bookList);
       }

    }
}

2.user包

2.1User类

java 复制代码
package user;

import book.BookList;
import ioperation.IOPeration;

public abstract class User {
    protected String name;
    public IOPeration[] ioPerations;

    public User(String name) {
        this.name = name;
    }

    public abstract  int menu();

    public void doIoperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

2.2AdminUser类-----管理员

java 复制代码
import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DeleteOperation(),
            new ShowOperation(),
        };
    }

    @Override
    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;
    }
}

2.3NormalUser类----普通成员

java 复制代码
mport ioperation.*;

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()
        };
    }

    @Override
    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.book包

3.1Book类

java 复制代码
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;
    }

    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;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + ((isBorrowed==true)?"已经借出":"未借出") +
                '}';
    }
}

3.2 BookList类

java 复制代码
package book;

public class BookList {
    //将书存在书架中
    private Book[] books=new Book[10];//存10本书
    private int usedSize;//记录书的数量

    public BookList() {
        this.books[0]=new Book("三国演义","罗贯中",18,"小说");
        this.books[1]=new Book("西游记","吴承恩",8,"小说");
        this.books[2]=new Book("红楼梦","曹雪芹",12,"小说");
        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 setBook(int pos,Book book) {
        this.books[pos]=book;
    }
    //获取整个数组
    public Book[] getBooks() {
        return books;
    }
}

4. ioperation包--实现增删查改

4.1 接口IOperation

java 复制代码
package ioperation;

import book.BookList;

public interface IOPeration {
    void work(BookList bookList);
}

4.2 ExitOperation类----退出系统

java 复制代码
package ioperation;

import book.BookList;

public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("你已经退出图书系统");
        System.exit(0);
    }
}

4.3 ShowOperation类----展现书籍

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("书籍展现");
        //获取书籍的数量
        int currentSize=bookList.getUsedSize();
        int i=0;
        for(;i<currentSize;i++){
            System.out.println(bookList.getBook(i));
        }
    }
}

4.4 FindOperation类----实现书籍的查询

java 复制代码
package ioperation;

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();
        for(int i=0;i<bookList.getUsedSize();i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println(book);
            }
        }
        System.out.println("没有你想要找的书");
    }
}

4.5 BorrowOperation类----实现书籍的借阅

java 复制代码
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();
        int i=0;
        for(;i<currentSize;i++){
            Book book=bookList.getBook(i);
           if(book.getName().equals(name)){
               if(book.isBorrowed()){
                   System.out.println("该数已经借出");
                   return;
               }
               book.setBorrowed(true);
               System.out.println("借书成功");
               return;
           }
        }
        System.out.println("没有你想借的书");
    }
}

4.6 ReturnOperation类----实现书籍的返还

java 复制代码
package ioperation;

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);
        System.out.println("请输如你要归还的书名");
        String name=scanner.nextLine();
        int currentSize= bookList.getUsedSize();
        int i=0;
        for(;i<currentSize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("没有你想要归还的数据");
        return;
    }
}

4.7 DeleteOperation类---实现书籍的删除

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("图书删除");

        Scanner scanner=new Scanner(System.in);
        System.out.println("输入你想删除的书名");
        String name=scanner.nextLine();

        int currentSize=bookList.getUsedSize();

        int i=0;
        //找到这本书
        int pos=-1;
        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++){
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");

    }
}

4.8 AddOperation类----实现书籍的增加

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import javax.script.ScriptContext;
import java.util.Scanner;

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        //判断是否满书
        int currentSize=bookList.getUsedSize();//获取数组
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架已满");
            return;
        }
        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 newBook=new Book(name,author,price,type);
        bookList.setBook(currentSize,newBook);
        System.out.println("增加成功");
        bookList.setUsedSize(currentSize+1);
        return;
    }
}
相关推荐
西猫雷婶9 分钟前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila9 分钟前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
初晴~9 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
小_太_阳35 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾38 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
唐 城1 小时前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding2 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云