常州电工培训

常州电工证

常州变频器培训

常州触摸屏培训

江苏和讯自动化设备有限公司欢迎您!
  • 和讯PLC,电工培训中心优势,名师团队一对一教学.
热门课程
联系方式
  • 常州和讯自动化培训中心
  • 常州市新北区太湖东路府琛大厦2号楼307-1室,307-2室(常州万达广场对面)
  • 电话:0519-85602926
  • 手机:15861139266 13401342299
当前位置:网站首页 > 新闻中心 新闻中心
用Java语言编写一个图书管理系统-常州PLC培训,常州上位机培训,常州工业机器人培训
日期:2023-12-14 9:56:56人气:  标签:常州PLC培训 常州上位机培训 常州工业机器人培训

 

以下是一个简单的基于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());

            }

        }

    }

}


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


本文网址:

相关信息:
版权所有 CopyRight 2006-2017 江苏和讯自动化设备有限公司 电话:0519-85602926 地址:常州市新北区太湖东路府琛大厦2号楼307-1室,307-2室
苏ICP备14016686号-2 技术支持:常州鹤翔网络
本站关键词:常州电工培训 常州电工证 常州变频器培训 常州触摸屏培训 网站地图 网站标签
在线与我们取得联系