Java实现简易图书管理系统

用java实现一个简易的图书管理系统。可以选择普通用户或管理员登陆,可以进行书籍的添加,删除,修改,查询,还书,借阅,展示操作。并且实现了一个简单的控制台版用户登陆程序, 程序启动提示用户输入用户名密码。 如果用户名 密码出错, 使用自定义异常的方式来处理。

 Book类:

package book_management_system.book;

public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean isBorrowed;
    public Book(String name, double price, String author, String type)
    {
        this.name = name;
        this.price = price;
        this.author = author;
        this.type = type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

    public String getType() {
        return type;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
}

Booklist类: 

package book_management_system.book;


import java.util.*;

public class BookList {
    //集合类:ArrayList<>();
    public static List<Book> books = new ArrayList<>();
    public static List<String> bookname = new ArrayList<>();

    static {
        books.add(new Book("三国演义", 45, "罗贯中", "回体小说"));
        books.add(new Book("水浒传", 56, "施耐庵", "回体小说"));
        books.add(new Book("三体", 180, "刘慈欣", "科幻小说"));
        books.add(new Book("西游记", 72, "吴承恩", "神魔小说"));
        books.add(new Book("倚天屠龙记", 61, "金庸", "武侠小说"));
        books.add(new Book("红楼梦", 42, "曹雪芹", "回文小说"));
        bookname.add("三国演义");
        bookname.add("水浒传");
        bookname.add("三体");
        bookname.add("西游记");
        bookname.add("倚天屠龙记");
        bookname.add("红楼梦");

    }

    public void displayBooks() {
        for (Book book : books) {
            System.out.println(book);
        }
    }

    public void displaybookname() {
        for (String book : bookname) {
            System.out.println(book);
        }
    }

    public boolean contains(String name) {
        return bookname.contains(name);
    }

    public void add(Book book) {
        books.add(book);
        bookname.add(book.getName());
    }

    public Book returnbook(String bookname)
    {
        for (Book book : books)
        {
            if (book.getName().equals(bookname))
            {
                return book;
            }
        }
        return null;
    }
    public void del(Book book)
    {
        books.remove(book);
        bookname.remove(book.getName());
    }
}
 IOperation接口:
package book_management_system.operation;


import book_management_system.book.BookList;

import java.util.Scanner;

public interface  IOperation {
    Scanner scanner =new Scanner(System.in);
    void work(BookList bookList);
}

添加书籍:

package book_management_system.operation;

import book_management_system.book.Book;
import book_management_system.book.BookList;

import static book_management_system.book.BookList.bookname;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList)
    {
        System.out.println("正在进行添加书本操作");
        System.out.println("请输入书籍名称");
        String name = scanner.next();
        if(bookList.contains(name))
        {
            System.out.println("书本已经存在,无需重复添加");
            return;
        }
        System.out.println("请输入作者");
        String author=scanner.next();
        System.out.println("请输入价格");
        double price =scanner.nextDouble();
        System.out.println("请输入书籍类别");
        String type =scanner.next();
        Book book =new Book(name,price,author,type);
        bookList.add(book);
        System.out.println("添加书籍成功");
    }
}

借阅书籍:

package book_management_system.operation;

import book_management_system.book.BookList;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行借阅操作");
        System.out.println("请输入要借阅的书籍名称");
        String name= scanner.next();
    if (bookList.contains(name))
    {
        bookList.returnbook(name).setBorrowed(true);
        System.out.println("借阅成功");

    }
    else
    {
        System.out.println("没有此书籍");
    }


    }
}

删除书籍:

package book_management_system.operation;

import book_management_system.book.BookList;

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行删除操作");
        while(true) {
            System.out.println("请输入要删除的书名");
            bookList.displaybookname();
            String name = scanner.next();
            if (bookList.contains(name)) {
                bookList.del(bookList.returnbook(name));
                System.out.println("删除成功");
                break;
            }
            else
            {
                System.out.println("未查询到所要删除的书名");
            }
        }

    }
}

展示书籍:

package book_management_system.operation;

import book_management_system.book.BookList;

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行查看列表书籍操作");
        bookList.displayBooks();
    }
}

查询书籍:

package book_management_system.operation;


import book_management_system.book.BookList;


public class FindOperation implements IOperation
{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行书籍查询操作");
        while (true) {
            System.out.println("请输入查询书籍名称");
            String name = scanner.next();
            if (bookList.contains(name)) {
                System.out.println(bookList.returnbook(name));
                break;

            } else {
                System.out.println("查询不到");

            }
        }


    }
}

归还书籍:

package book_management_system.operation;


import book_management_system.book.BookList;

/**
 * 归还书籍
 */
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行还书操作");
        while (true)
        {
            System.out.println("请输入还书书名");
            String name= scanner.next();
            if (bookList.contains(name))
            {
                if (bookList.returnbook(name).isBorrowed()==true)
                {
                    bookList.returnbook(name).setBorrowed(false);
                    System.out.println("还书成功");
                    break;
                }
                else
                {
                    System.out.println("此书籍未被借阅");
                    break;
                }
            }
            else
            {
                System.out.println("未查询到书名");
                break;
            }
        }

    }
}

更新书籍:

package book_management_system.operation;


import book_management_system.book.Book;
import book_management_system.book.BookList;

public class UpdateOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("正在进行书籍更新操作");
        while (true)
        {
            System.out.println("输入要修改的书名");
            bookList.displaybookname();
            String name=scanner.next();
            if (bookList.contains(name))
            {
                bookList.del(bookList.returnbook(name));
                System.out.println("请输入改正后的书名");
                String bookname= scanner.next();
                System.out.println("请输入改正后的价格");
                Double price=scanner.nextDouble();
                System.out.println("请输入改正后的作者名");
                String author=scanner.next();
                System.out.println("请输入改正后的类型");
                String type=scanner.next();
                Book book=new Book(bookname, price, author,  type);
                bookList.add(book);
                System.out.println("修改书籍成功");
                break;
            }
            else
            {
                System.out.println("书本不存在");
            }
        }


    }
}

 用户:

package book_management_system.user;

import book_management_system.book.BookList;
import book_management_system.operation.IOperation;

public abstract class User {
    protected String name;
    protected IOperation[] operations;
    public abstract int menu();
    public void doOperation(int choice, BookList bookList)
    {
        operations[choice-1].work(bookList);
    }

}

管理员用户:

package book_management_system.user;

import book_management_system.operation.*;

import java.util.Scanner;

public class AdminUser extends User{
    Scanner scanner=new Scanner(System.in);
    public AdminUser(String name)
    {
        this.name=name;
        this.operations=new IOperation[]
                {
                        new AddOperation(),
                        new DeleteOperation(),
                        new UpdateOperation(),
                        new FindOperation(),
                        new DisplayOperation(),

                };
    }
    @Override
    public int menu() {
        System.out.println("----------------------------------------------");
        System.out.println("欢迎"+this.name+"登录图书管理系统");
        System.out.println("请选择你要进行的操作:");
        System.out.println("1.添加书籍");
        System.out.println("2.删除书籍");
        System.out.println("3.更新书籍");
        System.out.println("4.查询书籍");
        System.out.println("5.展示当前书籍列表");
        System.out.println("-1.退出");
        int choice = scanner.nextInt();
        if (choice==-1)
        {
            scanner.close();
        }
        System.out.println("------------------------------------------");
        return choice;
    }
}

普通用户:

package book_management_system.user;

import book_management_system.operation.*;

import java.util.Scanner;

public class NormalUser extends User{
    Scanner scanner=new Scanner(System.in);
    public NormalUser(String name)
    {
        this.name=name;
        this.operations=new IOperation[]
                {
                        new DisplayOperation(),
                        new FindOperation(),
                        new BorrowOperation(),
                        new ReturnOperation(),

                };
    }
    @Override
    public int menu() {
        System.out.println("-------------------------------------------------");
        System.out.println("欢迎"+this.name+"登录图书管理系统");
        System.out.println("请选择你要进行的操作:");
        System.out.println("1.显示当前的书籍列表");
        System.out.println("2.查询书籍");
        System.out.println("3.借阅书籍");
        System.out.println("4.归还书籍");
        System.out.println("-1.退出");
        int choice = scanner.nextInt();
        if (choice==-1)
        {
           scanner.close();
        }
        System.out.println("---------------------------------------------------");
        return choice;

    }
}

Main类:

package book_management_system;

import book_management_system.book.BookList;
import book_management_system.user.AdminUser;
import book_management_system.user.NormalUser;
import book_management_system.user.User;
import java.util.Scanner;

public class Main {
    public static Scanner scanner=new Scanner(System.in);
    private String userName="刘思凡";

    public String getPassWord() {
        return passWord;
    }

    private String passWord="123456";

    public String getUserName() {
        return userName;
    }

    public static void main(String[] args )throws PasswordExpection, NameException {
        User user =login();
        BookList bookList =new BookList();
        while (true)
        {
            int choice= user.menu();
            if (choice==-1)
            {
                System.out.println("感谢使用");
            }
            user.doOperation(choice,bookList);
        }


    }
    public static User login() throws  NameException ,PasswordExpection{
//         String userName="刘思凡";
//         String passWord="123456";
        while(true)
        {
            System.out.println("请输入你的用户名");
            String name = scanner.next();
            System.out.println("请输入你的密码");
            String passowrd = scanner.next();
            Main main=new Main();
            if (!main.getUserName().equals(name))
            {
                throw new NameException("用户名错误");

            }

            if (!main.getPassWord().equals(passowrd))
         
            {
                throw new PasswordExpection("密码错误");
            }
            System.out.println("登陆成功");
            System.out.println("请输入您的角色,1表示普通用户,0表示管理员");
        int choice =scanner.nextInt();
        if (choice==1)
        {
            return new NormalUser(name);
        }
        return new AdminUser(name);

        }
    }
}

用户名异常: 

package book_management_system;

/**
 * @Author liusifan
 * @Data 2022/2/16 20:38
 */
public class NameException extends Exception {
    public NameException(String a) {
        super(a);
    }
}

密码异常: 

package book_management_system;

/**
 * @Author liusifan
 * @Data 2022/2/16 20:42
 */
public class PasswordExpection extends Exception{
    public PasswordExpection(String a)
    {
        super(a);
    }


}

运行结果:

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值