亚洲mv大片欧洲mv大片入口,国产粉嫩无码一区二区三区,国内精品自产拍在线观看91,久久久亚洲欧洲日产国码二区,中文字幕人妻久久一区二区三区

常州機器視覺培訓

常州上位機軟件開發(fā)

常州工業(yè)機器人編程設計培訓

常州PLC培訓

常州PLC

常州PLC編程培訓

常州電工培訓

常州和訊plc培訓中心歡迎您!
當前位置:網(wǎng)站首頁 > 新聞中心 新聞中心
用Java語言編寫一個圖書管理系統(tǒng)-常州PLC培訓,常州上位機培訓,常州機器視覺培訓
日期:2023-12-13 13:33:19人氣:  標簽:常州PLC培訓 常州上位機培訓 常州機器視覺培訓

 

以下是一個簡單的基于Java的圖書管理系統(tǒng):

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());

            }

        }

    }

}


這個圖書管理系統(tǒng)可以添加、刪除和搜索圖書。用戶可以根據(jù)書名、作者、出版商和年份搜索圖書。

本文網(wǎng)址:

相關信息:
版權所有 CopyRight 2006-2017 江蘇和訊自動化設備有限公司 常州自動化培訓中心 電話:0519-85602926 地址:常州市新北區(qū)府琛商務廣場2號樓1409室
蘇ICP備14016686號-2 技術支持:常州山水網(wǎng)絡
本站關鍵詞:常州PLC培訓 常州PLC編程培訓 常州PLC編程 常州PLC培訓班 網(wǎng)站地圖 網(wǎng)站標簽
在線與我們取得聯(lián)系
亚洲mv大片欧洲mv大片入口,国产粉嫩无码一区二区三区,国内精品自产拍在线观看91,久久久亚洲欧洲日产国码二区,中文字幕人妻久久一区二区三区