溫馨提示×

溫馨提示×

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

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

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

發(fā)布時間:2023-04-18 14:11:58 來源:億速云 閱讀:96 作者:iii 欄目:編程語言

這篇“Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫”文章吧。

一、功能介紹

此圖書管理系統(tǒng)借助IDEA開發(fā)工具實現(xiàn)

圖書館系統(tǒng)一共有兩種身份的訪問:

1.管理員身份:

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

2.普通用戶身份:

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

我們一共有三個包分別是book,operations,user實現(xiàn).

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

二、Main包

main函數(shù)主要進行大致流程的進行,圖書庫的初始化,圖書管理系統(tǒng)的登錄,以及具體操作的選擇,即實施.

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //1.先初始化圖書庫,以及初始化:
        BookList bookList = new BookList();

        //2.登錄
        User user = login();//向上轉(zhuǎn)型,User接受管理員或者用戶對象

        //3.打印菜單,進行具體操作
        while(true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

登錄功能:

 public static User login() {
        System.out.println("請輸入你的姓名: ");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        System.out.println("請輸入你的身份: 1-> 管理員 2-> 用戶");
        int choice = scanner.nextInt();
        if(choice == 1) {
            return new AdminUser(userName);
        }else {
            return new NormalUser(userName);
        }
    }

三、User包

Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫

因為有兩套系統(tǒng),管理員和普通用戶,所以我們將共同屬性提取出來一個User抽象類,以達到代碼復用

1. User
package user;

import book.BookList;
import operations.IOperation;

public abstract class User {
    protected String name;

    IOperation[] iOperations;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doOperation(int choice, BookList bookList) {
        iOperations[choice].work(bookList);
    }
}
2. AdminUser
package user;

import operations.*;

import java.util.Scanner;
public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }
    public int menu() {
        System.out.println("歡迎: "+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("**********************************");
        System.out.println("請輸入你的操作: ");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}
3. NormalUser
package user;

import operations.*;

import java.util.Scanner;
public class NormalUser extends User{

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

    public int menu() {
        System.out.println("歡迎: "+name+"來到圖書館");
        System.out.println("**********************************");
        System.out.println("1. 查找圖書");
        System.out.println("2. 借閱圖書");
        System.out.println("3. 歸還圖書");
        System.out.println("0. 退出圖書");
        System.out.println("**********************************");
        System.out.println("請輸入你的操作: ");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

四、book包

我們對book的屬性進行書寫,以及在BookList種對圖書庫的書進行初始化.

1. Book
package book;
public class Book {
    private String name;//書名
    private String author;//作者
    private int price;//價格
    private String type;//書的類型
    private boolean isBorrowed;//書默認未借出

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

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ","+ ((isBorrowed == true) ? "該書已借出" : "該書未借出" )+
                '}';
    }
}
2. BookList
package book;
public class BookList {
    public Book[] books = new Book[100];
    public int usedSize;//用來存當前共有多少本書


    /**
     * 事先通過代碼塊
     *
     * 事先存進去三本書
     */
    {
        books[0] = new Book("java","高斯林",95,"IT");
        books[1] = new Book("C++","姚琳",93,"IT");
        books[2] = new Book("python","馬瑟斯",80,"IT");
        this.usedSize = 3;
    }

    public Book getPos(int pos) {
        //獲取某一位置的書
        return books[pos];
    }

    public void setBooks(Book book,int pos) {
        //存儲一本書 到指定位置
        books[pos] = book;

    }
    public int getUsedSize() {
        return usedSize;
    }

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

五、operations包

我們的圖書管理系統(tǒng)有很多具體的操作,為了后面方便多態(tài),以及檢驗錯誤,所以我們實現(xiàn)一個具體的IOperation接口,每一個具體的操作去實現(xiàn)這個接口.

1. IOperation接口
package operations;

import book.BookList;

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

增加圖書:

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增圖書! ");
        System.out.println("請輸入要新增的圖書的名字: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("請輸入要新增的圖書的作者: ");
        String author = scanner.nextLine();
        System.out.println("請輸入要新增的圖書的價格: ");
        int price = scanner.nextInt();
        System.out.println("請輸入要新增的圖書的類型: ");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);

        //1.獲取當前書存放的位置
        int curSize = bookList.getUsedSize();
        //2.把書放在指定位置
        bookList.setBooks(book,curSize);
        //3.更新書的個數(shù)
        bookList.setUsedSize(curSize+1);

    }
}
3. BorrowOperation

借閱圖書:

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借閱圖書! ");
        System.out.println("請輸入要借閱的圖書的名字: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())) {
                if(book.isBorrowed()) {
                    System.out.println("該書已經(jīng)被借出! ");
                }else {
                    book.setBorrowed(true);
                    System.out.println("借閱圖書成功! ");
                    return;
                }
            }
        }
        System.out.println("沒有你要借閱的書! ");
    }
}
4. DelOperation

刪除圖書:

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("刪除圖書! ");
        System.out.println("請輸入要刪除的圖書: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //查找圖書是否有此圖書,記錄下標
        int index = -1;
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())) {
               index = i;
               break;
            }
        }
        if(index == -1) {
            System.out.println("沒有 "+name+"這本書!");
            return;
        }
        for (int i = index; i < bookList.getUsedSize()-1; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }

        //刪除的書,要置空
        bookList.setBooks(null, bookList.getUsedSize()-1);
        bookList.setUsedSize(bookList.getUsedSize()-1);
    }
}
5. DisplayOperation

顯示圖書:

package operations;

import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("顯示圖書! ");
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            System.out.println(book);
        }
    }
}
6. ExitOperation

退出圖書:

package operations;

import book.BookList;
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        System.out.println("退出系統(tǒng)! ");
        System.exit(0);
    }
}
7. FindOperation

查找圖書:

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        //查找圖書
        System.out.println("查找圖書! ");
        System.out.println("請輸入要查找的圖書: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())) {
                System.out.println("找到了! ");
                System.out.println(book);
                return;
            }
        }
        System.out.println("沒有這本書! ");
    }
}
8. ReturnOperation

歸還圖書:

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("歸還圖書! ");
        System.out.println("請輸入要歸還的圖書的名字: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())) {
                book.setBorrowed(false);
                System.out.println("歸還圖書成功! ");
                return;
            }
        }
        System.out.println("沒有你要歸還的書! ");
    }
}

以上就是關于“Java實現(xiàn)圖書管理系統(tǒng)的代碼怎么寫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關的知識內(nèi)容,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI