• 用java编写图书管理系统


    一个简单的图书管理系统通常包含图书的增加、删除、查询和显示等基本功能。

    以下是一个用 Java 编写的简单图书管理系统的示例代码

    import java.util.ArrayList;
    import java.util.Scanner;
    
    class Book {
        private String title;
        private String author;
    
        public Book(String title, String author) {
            this.title = title;
            this.author = author;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getAuthor() {
            return author;
        }
    
        @Override
        public String toString() {
            return "Title: " + title + ", Author: " + author;
        }
    }
    
    class Library {
        private ArrayList books;
    
        public Library() {
            this.books = new ArrayList<>();
        }
    
        public void addBook(Book book) {
            books.add(book);
            System.out.println("Book added successfully.");
        }
    
        public void removeBook(String title) {
            for (Book book : books) {
                if (book.getTitle().equalsIgnoreCase(title)) {
                    books.remove(book);
                    System.out.println("Book removed successfully.");
                    return;
                }
            }
            System.out.println("Book not found.");
        }
    
        public void displayBooks() {
            if (books.isEmpty()) {
                System.out.println("No books in the library.");
            } else {
                System.out.println("Books in the library:");
                for (Book book : books) {
                    System.out.println(book);
                }
            }
        }
    
        public Book findBook(String title) {
            for (Book book : books) {
                if (book.getTitle().equalsIgnoreCase(title)) {
                    return book;
                }
            }
            return null;
        }
    }
    
    public class LibraryManagementSystem {
        public static void main(String[] args) {
            Library library = new Library();
            Scanner scanner = new Scanner(System.in);
    
            while (true) {
                System.out.println("\nLibrary Management System");
                System.out.println("1. Add Book");
                System.out.println("2. Remove Book");
                System.out.println("3. Display Books");
                System.out.println("4. Find Book");
                System.out.println("5. Exit");
                System.out.print("Enter your choice: ");
    
                int choice = scanner.nextInt();
                scanner.nextLine(); // Consume newline
    
                switch (choice) {
                    case 1:
                        System.out.print("Enter book title: ");
                        String title = scanner.nextLine();
                        System.out.print("Enter book author: ");
                        String author = scanner.nextLine();
                        Book newBook = new Book(title, author);
                        library.addBook(newBook);
                        break;
    
                    case 2:
                        System.out.print("Enter book title to remove: ");
                        String titleToRemove = scanner.nextLine();
                        library.removeBook(titleToRemove);
                        break;
    
                    case 3:
                        library.displayBooks();
                        break;
    
                    case 4:
                        System.out.print("Enter book title to find: ");
                        String titleToFind = scanner.nextLine();
                        Book foundBook = library.findBook(titleToFind);
                        if (foundBook != null) {
                            System.out.println("Book found: " + foundBook);
                        } else {
                            System.out.println("Book not found.");
                        }
                        break;
    
                    case 5:
                        System.out.println("Exiting the program.");
                        System.exit(0);
    
                    default:
                        System.out.println("Invalid choice. Please enter a valid option.");
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    这个简单的图书管理系统允许用户添加书籍、删除书籍、显示所有书籍以及查找书籍。用户可以通过输入选项的数字来执行相应的操作。这只是一个基本示例,实际的系统可能需要更多功能和更复杂的设计。

  • 相关阅读:
    zookeeper选举机制
    (附源码)app智能车APP 毕业设计 250623
    【基于机械臂触觉伺服的物体操控】论文研读《A control framework for tactile servoing》
    C++ 中的头文件和源文件
    第五天:前端页面展示不出来
    devops学习(三) K8环境部署jenkins
    ZMQ之自杀的蜗牛模式和黑箱模式
    Web3行业人才需求激增,加密初创企业的薪资究竟如何?
    springboot毕设项目大学生创新创业项目的管理系统kri27(java+VUE+Mybatis+Maven+Mysql)
    爬虫 | 正则、Xpath、BeautifulSoup示例学习
  • 原文地址:https://blog.csdn.net/weixin_43160662/article/details/134521705