图书管理系统

图书管理系统

class Book {
    private String title;
    private String author;
    private boolean isAvailable;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.isAvailable = true;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void setAvailable(boolean available) {
        isAvailable = available;
    }
}

class Library {
    private List<Book> books;

    public Library() {
        books = new ArrayList<>();
    }

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

    public void removeBook(Book book) {
        books.remove(book);
    }

    public List<Book> searchBooks(String keyword) {
        List<Book> results = new ArrayList<>();
        for (Book book : books) {
            if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
                results.add(book);
            }
        }
        return results;
    }

    public List<Book> getAvailableBooks() {
        List<Book> availableBooks = new ArrayList<>();
        for (Book book : books) {
            if (book.isAvailable()) {
                availableBooks.add(book);
            }
        }
        return availableBooks;
    }
}

class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

class UserManager {
    private List<User> users;

    public UserManager() {
        users = new ArrayList<>();
    }

    public void registerUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名:");
        String username = scanner.nextLine();
        System.out.println("输入密码:");
        String password = scanner.nextLine();
        User user = new User(username, password);
        users.add(user);
        System.out.println("用户注册成功。");
    }

    public User loginUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名:");
        String username = scanner.nextLine();
        System.out.println("输入密码:");
        String password = scanner.nextLine();
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("用户登录成功。");
                return user;
            }
        }
        System.out.println("用户名或密码错误。");
        return null;
    }
}

class Administrator {
    private Library library;

    public Administrator(Library library) {
        this.library = library;
    }

    public void addBook() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入图书标题:");
        String title = scanner.nextLine();
        System.out.println("输入图书作者:");
        String author = scanner.nextLine();
        Book book = new Book(title, author);
        library.addBook(book);
        System.out.println("图书添加成功。");
    }

    public void removeBook() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入图书标题:");
        String title = scanner.nextLine();
        System.out.println("输入图书作者:");
        String author = scanner.nextLine();
        List<Book> books = library.searchBooks(title);
        for (Book book : books) {
            if (book.getAuthor().equals(author)) {
                library.removeBook(book);
                System.out.println("图书删除成功。");
                return;
            }
        }
        System.out.println("找不到要删除的图书。");
    }

    public void showBooks() {
        List<Book> books = library.getAvailableBooks();
        System.out.println("图书列表:");
        for (Book book : books) {
            System.out.println(book.getTitle() + " - 作者:" + book.getAuthor());
        }
    }
}

class Reader {
    private Library library;

    public Reader(Library library) {
        this.library = library;
    }

    public void searchBooks() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入关键词进行搜索:");
        String keyword = scanner.nextLine();
        List<Book> books = library.searchBooks(keyword);
        System.out.println("搜索结果:");
        for (Book book : books) {
            System.out.println(book.getTitle() + " - 作者:" + book.getAuthor());
        }
    }

    public void borrowBook() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入图书标题:");
        String title = scanner.nextLine();
        System.out.println("输入图书作者:");
        String author = scanner.nextLine();
        List<Book> books = library.searchBooks(title);
        for (Book book : books) {
            if (book.getAuthor().equals(author) && book.isAvailable()) {
                book.setAvailable(false);
                System.out.println("借书成功。");
                return;
            }
        }
        System.out.println("图书不可用。");
    }

    public void returnBook() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入图书标题:");
        String title = scanner.nextLine();
        System.out.println("输入图书作者:");
        String author = scanner.nextLine();
        List<Book> books = library.searchBooks(title);
        for (Book book : books) {
            if (book.getAuthor().equals(author) && !book.isAvailable()) {
                book.setAvailable(true);
                System.out.println("归还图书成功。");
                return;
            }
        }
        System.out.println("找不到图书或已归还。");
    }
}

public class LibraryManagementSystem {
    public static void main(String[] args) {
        Library library = new Library();
        Administrator administrator = new Administrator(library);
        Reader reader = new Reader(library);
        UserManager userManager = new UserManager();

        Scanner scanner = new Scanner(System.in);
        int choice;

        while (true) {
            System.out.println("1. 注册\n2. 登录\n0. 退出");
            System.out.println("请选择:");
            choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    userManager.registerUser();
                    break;
                case 2:
                    User user = userManager.loginUser();
                    if (user != null) {
                        // 根据用户角色进行操作
                        if (isAdmin(user)) {
                            boolean flag = true;
                            while (flag) {
                                // 管理员操作
                                System.out.println("欢迎管理员 " + user.getUsername());
                                System.out.println("1. 添加图书\n2. 删除图书\n3. 显示图书\n4、退出");
                                System.out.println("请选择:");
                                int adminChoice = scanner.nextInt();
                                scanner.nextLine(); // 消费换行符
                                switch (adminChoice) {
                                    case 1:
                                        administrator.addBook();
                                        break;
                                    case 2:
                                        administrator.removeBook();
                                        break;
                                    case 3:
                                        administrator.showBooks();
                                        break;
                                    case 4:
                                        flag = false;
                                        break;
                                    default:
                                        System.out.println("无效的选择。");
                                        break;
                                }
                            }

                        } else {
                            boolean loop = true;
                            while (loop) {
                                // 读者操作
                                System.out.println("欢迎读者 " + user.getUsername());
                                System.out.println("1. 搜索图书\n2. 借阅图书\n3. 归还图书\n4退出");
                                System.out.println("请选择:");
                                int readerChoice = scanner.nextInt();
                                scanner.nextLine(); // 消费换行符

                                switch (readerChoice) {
                                    case 1:
                                        reader.searchBooks();
                                        break;
                                    case 2:
                                        reader.borrowBook();
                                        break;
                                    case 3:
                                        reader.returnBook();
                                        break;
                                    case 4:
                                        loop = false;
                                        break;
                                    default:
                                        System.out.println("无效的选择。");
                                        break;
                                }
                            }
                        }
                    }
                    break;
                case 0:
                    System.out.println("正在退出...");
                    System.exit(0);
                default:
                    System.out.println("无效的选择。");
                    break;
            }
        }
    }

    public static boolean isAdmin(User user) {
        // 假设用户名以 "admin" 开头的用户为管理员
        return user.getUsername().startsWith("admin");
    }
}

####运行截图
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值