溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java順序表怎么實現圖書管理系統

發布時間:2021-11-22 16:10:36 來源:億速云 閱讀:123 作者:iii 欄目:開發技術

這篇文章主要講解了“Java順序表怎么實現圖書管理系統”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java順序表怎么實現圖書管理系統”吧!

一、簡介

實現此項目的目的是鞏固并理解前面的知識點:類,抽象類,封裝,繼承,多態,接口等

二、核心需求

管理端

??查閱書籍
??增加書籍
??刪除書籍
??打印書籍列表
??退出系統

用戶端

??查詢書籍
??借閱書籍
??歸還書籍
??打印書籍列表
??退出系統

三、類的設計

1. 創建圖書類

圖書類中包含圖書的名稱,價格,類型,作者和是否被借出等信息,并生成構造方法,Getter()和Setter()方法,toString方法(注意成員變量應該盡可能使用private關鍵字修飾)

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

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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 String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", author='" + author + '\'' +
                ", 狀態:" +((isBorrowed) ? "已借出":"未借出")+
                '}';
    }
}

2. 創建圖書列表類

圖書列表類用于存放圖書,我們可以先在列表中初始化幾本書以方便后續測試

public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;
    public BookList(){
        books[0] = new Book("三國演義",19,"小說","羅貫中");
        books[1] = new Book("水滸傳",29,"小說","施耐庵");
        books[2] = new Book("西游記",39,"小說","吳承恩");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public Book getBook(int pos){
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}

3. 創建用戶類

創建一個用戶類并將其定義為抽象類,再創建普通用戶類和管理員類繼承于用戶類:

創建用戶類并定義為抽象類:

public abstract class User {
    protected String name;
    protected IOperation[] iOperations;
    public abstract int menu();
    public void doWork(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
    public User(String name) {
        this.name = name;
    }
}

創建管理員用戶類:

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DisplayOperation(),
            new DelOperation()
        };
    }

    @Override
    public int menu(){
        System.out.println("===========管理員菜單============");
        System.out.println("您好, 管理員 "+this.name+":");
        System.out.println("歡迎來到圖書館!");
        System.out.println("1. 查找圖書");
        System.out.println("2. 新增圖書");
        System.out.println("3. 顯示圖書");
        System.out.println("4. 刪除圖書");
        System.out.println("0. 退出系統");
        System.out.println("=================================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

創建普通用戶類:

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new DisplayOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu(){
        System.out.println("===========普通用戶菜單============");
        System.out.println("您好,用戶 "+this.name+":");
        System.out.println("歡迎來到圖書館!");
        System.out.println("1. 顯示圖書");
        System.out.println("2. 查找圖書");
        System.out.println("3. 借閱圖書");
        System.out.println("4. 歸還圖書");
        System.out.println("0. 退出系統");
        System.out.println("=================================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

4. 創建操作相關的類

首先創建一個接口用于實現多態:

public interface IOperation {
    void work(BookList bookList);
}

創建添加書籍類:

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入圖書名稱:");
        String name = scanner.nextLine();
        System.out.println("請輸入價格:");
        double price = scanner.nextDouble();
        System.out.println("請輸入類型:");
        String type = scanner.next();
        System.out.println("請輸入作者:");
        String author = scanner.next();
        Book book = new Book(name,price,type,author);
        int usedSize = bookList.getUsedSize();
        bookList.setBook(usedSize,book);
        bookList.setUsedSize(++usedSize);
        System.out.println("添加圖書成功!");
    }
}

創建查找書籍類:

public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("請輸入書名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for(int i=0;i<bookList.getUsedSize();i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println(book);
                return;
            }
        }
        System.out.println("找不到 《"+name+"》 這本書");
    }
}

創建借閱書籍類:

public class BorrowOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("請輸入你要借閱的書籍:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i = 0;
        for (i = 0; i < bookList.getUsedSize() - 1; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName()) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println("借閱成功!");
                return;
            }
            if (name.equals(book.getName()) && book.isBorrowed()) {
                System.out.println("該書籍已被借出");
                return;
            }
        }
        System.out.println("找不到你要借閱的書籍!");
    }
}

創建歸還書籍類:

public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("請輸入你要歸還的書籍:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i=0;
        for(i=0;i<bookList.getUsedSize()-1;i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())&& book.isBorrowed()){
                book.setBorrowed(false);
                System.out.println("歸還成功!");
                return;
            }
            if(name.equals(book.getName())&& !book.isBorrowed()){
                System.out.println("此書處于未借出狀態!");
                return;
            }
        }
        System.out.println("找不到你要歸還的書籍!");
    }
}

創建刪除書籍類:

public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("請輸入要刪除的書名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int index = 0;
        int i = 0;
        for(i=0;i<bookList.getUsedSize();i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(i>=bookList.getUsedSize()) {
            System.out.println("找不到這本書");
            return;
        }
        int j = 0;
        for (j = index;j< bookList.getUsedSize()-1;j++){
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(bookList.getUsedSize()-1, null);
        bookList.setUsedSize(bookList.getUsedSize()-1);
        System.out.println("刪除成功!");
    }
}

創建打印書籍列表類:

public class DisplayOperation implements IOperation{
    public void work(BookList bookList){
        int usedSize = bookList.getUsedSize();
        for (int i=0;i<usedSize;i++){
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

退出系統類:

public class ExitOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("退出系統!");
        System.exit(0);
    }
}

主函數類:

public class Main {
    public static User work(){
        System.out.println("請輸入您的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("請輸入身份: 1-> 管理員登錄  0-> 用戶登錄");
        int choice = scanner.nextInt();
        if(choice==1){
            return new AdminUser(name);
        }
            return new NormalUser(name);
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = work();
        while (true) {
        int choice = user.menu();
            user.doWork(choice, bookList);
        }
    }
}

感謝各位的閱讀,以上就是“Java順序表怎么實現圖書管理系統”的內容了,經過本文的學習后,相信大家對Java順序表怎么實現圖書管理系統這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女