首頁 > 軟體

JavaSE實現圖書管理系統的範例程式碼

2022-08-25 14:02:58

前言

這篇部落格是在學習了一部分Java基礎語法之後的練習專案,通過這個小專案的練習,對Java中的類和物件,抽象類和介面,物件導向的繼承、多型和封裝、組合等進行熟悉理解;抽象出不同的物件,將物件進行合理的設計,完成物件之間的互動,物件導向進行程式設計。

1. 專案需求

圖書管理系統,面向管理員和普通使用者使用,對管理員的開放的功能有:新增圖書,刪除圖書,查詢圖書,顯示圖書和退出程式等;對普通使用者的開放的功能有:查詢圖書,借閱圖書,歸還圖書和退出程式。

2. 實現思路

先抽象提取出不同的物件,首先想到的物件是使用者和書,

使用者可分為管理員和普通使用者,將管理員和普通使用者共有的屬性設定為一個父類別User,管理員和普通使用者繼承User,可以先將父類別設定為普通類,在寫碼過程如果合適可以修改為抽象類;

系統對書進行管理,書本身就可以抽象為一個物件;對多本書進行操作管理,書架也可以抽象為一個物件;所以書和書架用兩個類來實現,書架可以設計為一個陣列;

這裡系統對書進行管理,其實就是對書架對應陣列進行操作,不同的操作可以通過一個標準去實現,也就是可以定義一個介面,每一個操作類去實現這個介面,完成向上轉型,通過介面實現多型;

然後寫程式碼的過程中思考完善各個物件的成員,最終範例化物件,通過不同物件之間的互動完成管理系統。

3. 程式碼實現

包的設計

book包

Book類

package book;

public class Book {
    private String name;//書名
    private String author;//書的作者
    private int price;//書的價格
    private String type;//書的型別
    private boolean borrowed;//是否被借出,預設false

    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 getBorrowed() {
        return borrowed;
    }

    public void setBorrowed(Boolean borrowed) {
        this.borrowed = borrowed;
    }

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

BookList類

package book;

public class BookList {
    private Book[] books = new Book[100];
    private int usedsize;
    //書架當中預設有三本書

    public BookList() {
        this.books[0] = new Book("三國演義","羅貫中",66,"小說");
        this.books[1] = new Book("西遊記","吳承恩",68,"小說");
        this.books[2] = new Book("紅樓夢","曹雪芹",64,"小說");
        this.usedsize = 3;
    }

    public int getUsedsize() {
        return usedsize;
    }

    public void setUsedsize(int usedsize) {
        this.usedsize = usedsize;
    }


    public Book getBooks(int pos){
        return this.books[pos];
    }

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

operations包

IOperation介面

package operations;

import book.BookList;

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

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();
        scanner.nextLine();
        System.out.println("輸入新增圖書型別:");
        String type = scanner.nextLine();

        Book book = new Book(name, author, price, type);
        //獲取存書位置
        int currentSize = bookList.getUsedsize();
        //把書放到書架(陣列)
        bookList.setBooks(book, currentSize);
        //書的總數加1
        bookList.setUsedsize(currentSize+1);
        System.out.println("新增成功!");
    }
}

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();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                if(book.getBorrowed()){
                    System.out.println("該書已經被借出!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借閱成功!");
                    return;
                }
            }
        }
        System.out.println("沒有找到你要借閱的書");
    }
}

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 currentSize = bookList.getUsedsize();

        //找到並記錄要刪除書的下標
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(-1 == index) {
            System.out.println("沒有找到要刪除的這本書!");
        }else {
            for (int i = index; i < currentSize-1; i++) {
                Book book = bookList.getBooks(i+1);
                bookList.setBooks(book, i);
            }
            //每次刪除,都要將原來最後一本書位置的參照置空
            bookList.setBooks(null,currentSize-1);
            bookList.setUsedsize(currentSize-1);
            System.out.println("刪除成功");
        }
    }
}

DisplayOperation類

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("顯示圖書");
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

ExitOperation類

package operations;

import book.BookList;

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

FindOperation類

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();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                System.out.println("找到了,該書資訊如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("沒有找到這本書!");
    }
}

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();
        int currentSize = bookList.getUsedsize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                if(book.getBorrowed()){
                    book.setBorrowed(false);
                    System.out.println("歸還成功!");
                    return;

                }else {
                    System.out.println("該書未借出!");
                    return;
                }
            }
        }
        System.out.println("沒有找到你要歸還的書");
    }
}

user包

User類

public abstract class User {
    protected String name;
    protected IOperation[] iOperation;

    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        iOperation[choice].work(bookList);
    }
}

AdminUser類

package user;

import operations.*;

import java.util.Scanner;

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

    @Override
    public int menu() {
        System.out.println("---------------------------------");
        System.out.println("Hello 管理員:>"+""+name+" 歡迎來到圖書管理系統!");
        System.out.println("      1.查詢圖書  2.新增圖書");
        System.out.println("      3.刪除圖書  4.顯示圖書");
        System.out.println("           0.退出系統");
        System.out.println("---------------------------------");
        System.out.println("請輸入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NormalUser類

package user;

import operations.*;

import java.util.Scanner;

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

    @Override
    public int menu() {
        System.out.println("---------------------------------");
        System.out.println("Hello 使用者:>"+name+" 歡迎來到圖書管理系統!");
        System.out.println("      1.查詢圖書  2.借閱圖書");
        System.out.println("      3.歸還圖書  0.退出系統");
        System.out.println("---------------------------------");
        System.out.println("請輸入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

Main類

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        System.out.println("請輸入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        System.out.println("請確認你的身份:> |1->管理員|0->普通使用者|");
        int choice = scanner.nextInt();
        if(1==choice){
            return new AdminUser(userName);
        }else{
            return new NormalUser(userName);
        }
    }

    public static void main(String[] args) {
        //準備資料
        BookList bookList = new BookList();
        //登入
        User user = login();

        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }
}

4. 實現效果

管理員

普通使用者

以上就是JavaSE實現圖書管理系統的範例程式碼的詳細內容,更多關於JavaSE圖書管理系統的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com