全国服务热线:

15861139266

用Java语言编写一个图书管理系统,苏州PLC培训,苏州上位机培训,苏州机器视觉培训,苏州工业机器人培训
发布时间:2023-09-19 14:53:42 点击次数:251

用Java语言编写一个图书管理系统

以下是一个简单的基于Java的图书管理系统:

Book.java


public class Book {

    private int id;

    private String title;

    private String author;

    private String publisher;

    private int year;

    public Book(int id, String title, String author, String publisher, int year) {

        this.id = id;

        this.title = title;

        this.author = author;

        this.publisher = publisher;

        this.year = year;

    }

    public int getId() {

        return id;

    }

    public String getTitle() {

        return title;

    }

    public void setTitle(String title) {

        this.title = title;

    }

    public String getAuthor() {

        return author;

    }

    public void setAuthor(String author) {

        this.author = author;

    }

    public String getPublisher() {

        return publisher;

    }

    public void setPublisher(String publisher) {

        this.publisher = publisher;

    }

    public int getYear() {

        return year;

    }

    public void setYear(int year) {

        this.year = year;

    }

}


Library.java


import java.util.ArrayList;

import java.util.List;

public class Library {

    private Listbooks;

    public Library() {

        books = new ArrayList<>();

    }

    public void addBook(Book book) {

        books.add(book);

    }

    public void removeBook(int id) {

        for (Book book : books) {

            if (book.getId() == id) {

                books.remove(book);

                break;

            }

        }

    }

    public Book getBook(int id) {

        for (Book book : books) {

            if (book.getId() == id) {

                return book;

            }

        }

        return null;

    }

    public ListsearchByTitle(String title) {

        Listresult = new ArrayList<>();

        for (Book book : books) {

            if (book.getTitle().equals(title)) {

                result.add(book);

            }

        }

        return result;

    }

    public ListsearchByAuthor(String author) {

        Listresult = new ArrayList<>();

        for (Book book : books) {

            if (book.getAuthor().equals(author)) {

                result.add(book);

            }

        }

        return result;

    }

    public ListsearchByPublisher(String publisher) {

        Listresult = new ArrayList<>();

        for (Book book : books) {

            if (book.getPublisher().equals(publisher)) {

                result.add(book);

            }

        }

        return result;

    }

    public ListsearchByYear(int year) {

        Listresult = new ArrayList<>();

        for (Book book : books) {

            if (book.getYear() == year) {

                result.add(book);

            }

        }

        return result;

    }

}


Main.java


import java.util.List;

import java.util.Scanner;

public class Main {

    private static Library library = new Library();

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (true) {

            System.out.println("\n1. Add book");

            System.out.println("2. Remove book");

            System.out.println("3. Search book");

            System.out.println("4. Exit");

            System.out.print("\nEnter your choice: ");

            int choice = scanner.nextInt();

            switch (choice) {

                case 1:

                    addBook(scanner);

                    break;

                case 2:

                    removeBook(scanner);

                    break;

                case 3:

                    searchBook(scanner);

                    break;

                case 4:

                    System.exit(0);

                default:

                    System.out.println("Invalid choice.");

            }

        }

    }

    private static void addBook(Scanner scanner) {

        System.out.print("\nEnter book ID: ");

        int id = scanner.nextInt();

        scanner.nextLine();

        System.out.print("Enter book title: ");

        String title = scanner.nextLine();

        System.out.print("Enter book author: ");

        String author = scanner.nextLine();

        System.out.print("Enter book publisher: ");

        String publisher = scanner.nextLine();

        System.out.print("Enter book year: ");

        int year = scanner.nextInt();

        Book book = new Book(id, title, author, publisher, year);

        library.addBook(book);

        System.out.println("\nBook added successfully.");

    }

    private static void removeBook(Scanner scanner) {

        System.out.print("\nEnter book ID: ");

        int id = scanner.nextInt();

        Book book = library.getBook(id);

        if (book == null) {

            System.out.println("Book not found.");

        } else {

            library.removeBook(id);

            System.out.println("\nBook removed successfully.");

        }

    }

    private static void searchBook(Scanner scanner) {

        System.out.println("\n1. Search by title");

        System.out.println("2. Search by author");

        System.out.println("3. Search by publisher");

        System.out.println("4. Search by year");

        System.out.print("\nEnter your choice: ");

        int choice = scanner.nextInt();

        scanner.nextLine();

        switch (choice) {

            case 1:

                searchByTitle(scanner);

                break;

            case 2:

                searchByAuthor(scanner);

                break;

            case 3:

                searchByPublisher(scanner);

                break;

            case 4:

                searchByYear(scanner);

                break;

            default:

                System.out.println("Invalid choice.");

        }

    }

    private static void searchByTitle(Scanner scanner) {

        System.out.print("\nEnter book title: ");

        String title = scanner.nextLine();

        Listresult = library.searchByTitle(title);

        printSearchResult(result);

    }

    private static void searchByAuthor(Scanner scanner) {

        System.out.print("\nEnter book author: ");

        String author = scanner.nextLine();

        Listresult = library.searchByAuthor(author);

        printSearchResult(result);

    }

    private static void searchByPublisher(Scanner scanner) {

        System.out.print("\nEnter book publisher: ");

        String publisher = scanner.nextLine();

        Listresult = library.searchByPublisher(publisher);

        printSearchResult(result);

    }

    private static void searchByYear(Scanner scanner) {

        System.out.print("\nEnter book year: ");

        int year = scanner.nextInt();

        Listresult = library.searchByYear(year);

        printSearchResult(result);

    }

    private static void printSearchResult(Listresult) {

        if (result.size() == 0) {

            System.out.println("\nNo books found.");

        } else {

            System.out.println("\nSearch result:");

            for (Book book : result) {

                System.out.println(book.getId() + "\t" + book.getTitle() + "\t" + book.getAuthor() + "\t" + book.getPublisher() + "\t" + book.getYear());

            }

        }

    }

}


这个图书管理系统可以添加、删除和搜索图书。用户可以根据书名、作者、出版商和年份搜索图书。



1.png

立即咨询
  • 品质服务

    服务贴心周到

  • 快速响应

    全天24小时随时沟通

  • 专业服务

    授权率高,保密性强

  • 完善售后服务

    快速响应需求,及时性服务

直播课程
软件开发基础课程
上位机软件开发课
机器视觉软件开发课
专题课
联系方式
电话:15861139266
邮箱:75607082@qq.com
地址:苏州吴中区木渎镇尧峰路69号
关注我们

版权所有:江苏和讯自动化设备有限公司所有 备案号:苏ICP备2022010314号-1

技术支持: 易动力网络