1. 代码实现:

Book类:

public class Book { private String name; private String author; private double price; private String type; private boolean isBorrowed;

public Book() {}

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

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

@Override
public String toString() {
    return "图书信息{" +
            "书名='" + name + '\'' +
            ", 作者='" + author + '\'' +
            ", 价格=" + price +
            ", 种类='" + type + '\'' +
            ", 是否被借出=" + isBorrowed +
            '}';
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Book other = (Book) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

BookList类:

import java.util.ArrayList;

public class BookList { private ArrayList books;

public BookList() {
    this.books = new ArrayList<>();
    books.add(new Book("Java基础入门", "张三", 30.5, "教育"));
    books.add(new Book("Python从入门到放弃", "李四", 45.8, "教育"));
    books.add(new Book("计算机网络", "王五", 60.2, "科技"));
    books.add(new Book("数据结构与算法", "赵六", 42.0, "科技"));
}

public BookList(ArrayList<Book> books) {
    this.books = books;
}

public ArrayList<Book> getBooks() {
    return books;
}

public void setBooks(ArrayList<Book> books) {
    this.books = books;
}

@Override
public String toString() {
    return "BookList{" +
            "books=" + books +
            '}';
}

}

Manager类:

import java.util.ArrayList; import java.util.Scanner;

public class Manager { private BookList bookList;

public Manager(BookList bookList) {
    this.bookList = bookList;
}

public void lookAllBook() {
    ArrayList<Book> books = bookList.getBooks();
    System.out.println("所有书籍列表:");
    for (Book book : books) {
        System.out.println(book);
    }
}

public void lookBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要查询的书籍名称:");
    String name = scanner.next();
    for (Book book : bookList.getBooks()) {
        if (book.getName().equals(name)) {
            System.out.println(book);
            return;
        }
    }
    System.out.println("书籍不存在!");
}

public void deleteBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要删除的书籍名称:");
    String name = scanner.next();
    ArrayList<Book> books = bookList.getBooks();
    for (Book book : books) {
        if (book.getName().equals(name)) {
            books.remove(book);
            System.out.println("删除成功!");
            return;
        }
    }
    System.out.println("书籍不存在,删除失败!");
}

public void addBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入书籍名称:");
    String name = scanner.next();
    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, author, price, type);
    ArrayList<Book> books = bookList.getBooks();
    books.add(book);
    System.out.println("添加成功!");
}

}

User类:

import java.util.ArrayList; import java.util.Scanner;

public class User { private BookList bookList;

public User(BookList bookList) {
    this.bookList = bookList;
}

public void lookBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要查询的书籍名称:");
    String name = scanner.next();
    for (Book book : bookList.getBooks()) {
        if (book.getName().equals(name)) {
            System.out.println(book);
            return;
        }
    }
    System.out.println("书籍不存在!");
}

public void borrowBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要借阅的书籍名称:");
    String name = scanner.next();
    ArrayList<Book> books = bookList.getBooks();
    for (Book book : books) {
        if (book.getName().equals(name)) {
            if (book.isBorrowed()) {
                System.out.println("该书籍已被借出!");
                return;
            } else {
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
    }
    System.out.println("书籍不存在,借阅失败!");
}

public void returnBook() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要归还的书籍名称:");
    String name = scanner.next();
    ArrayList<Book> books = bookList.getBooks();
    for (Book book : books) {
        if (book.getName().equals(name)) {
            if (book.isBorrowed()) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            } else {
                System.out.println("该书籍未被借出!");
                return;
            }
        }
    }
    System.out.println("书籍不存在,归还失败!");
}

}

BookManager类:

import java.util.Scanner;

public class BookManager { public static void main(String[] args) { BookList bookList = new BookList(); Manager manager = new Manager(bookList); User user = new User(bookList); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("欢迎使用图书管理系统!请选择您的身份:"); System.out.println("1.系统管理员 2.普通用户 3.退出系统"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.println("欢迎进入系统管理员界面:"); while (true) { System.out.println("请选择您要进行的操作:"); System.out.println("1.查看所有书籍列表 2.查看书籍信息 3.删除书籍信息 4.添加书籍 5.返回上一级"); int choice1 = scanner.nextInt(); switch (choice1) { case 1: manager.lookAllBook(); break; case 2: manager.lookBook(); break; case 3: manager.deleteBook(); break; case 4: manager.addBook(); break; case 5: break; default: System.out.println("输入有误,请重新输入!"); continue; } System.out.println("是否继续进行操作?是(Y)/否(N)"); String flag = scanner.next(); if (flag.equalsIgnoreCase("N")) { break; } } break; case 2: System.out.println("欢迎进入用户界面:"); while (true) { System.out.println("请选择您要进行的操作:"); System.out.println("1.查看书籍信息 2.借阅书籍 3.归还书籍 4.返回上一级"); int choice2 = scanner.nextInt(); switch (choice2) { case 1: user.lookBook(); break; case 2: user.borrowBook(); break; case 3: user.returnBook(); break; case 4: break; default: System.out.println("输入有误,请重新输入!"); continue; } System.out.println("是否继续进行操作?是(Y)/否(N)"); String flag = scanner.next(); if (flag.equalsIgnoreCase("N")) { break; } } break; case 3: System.out.println("感谢您的使用,再见!"); System.exit(0); default: System.out.println("输入有误,请重新输入!"); continue; } } }

图书管理系统List集合版1 需求描述:通过我们学习过的面向对象和集合部分完成一个综合案例:简版图书管理系统!本系统共分为两个角色分别为用户和管理员都只能进行最基本图书操作。管理员模块功能1查看书籍列表:查询所有的书籍信息。2查看书籍信息:通过键盘录入要查询的书籍名称如果书籍存在则输出书籍的信息如果不存在就输出不存在。3删除书籍信息:通过键盘录入要删除书籍的名字如果书籍存在则删除书籍的信息如果不存

原文地址: https://www.cveoy.top/t/topic/g9pu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录