Java實現圖書管理系統的示例代碼

User類

首先創建一個抽象類User,屬性為姓名,提供瞭構造方法和get方法(因為本系統不需要修改名字,所以沒有設置set方法),有一個able方法,讓子類管理員和普通用戶實現不同的界面和操作。

public abstract class User {
    private String name;
 
    public User(String name){
        this.name=name;
    }
    abstract void able();
 
    public String getName() {
        return name;
    }
}

管理員和普通用戶類

這裡定義瞭一個管理員和普通用戶類繼承User類,able方法裡是菜單,可選擇界面裡的操作

public class AdminUser extends User{
 
 
    public AdminUser(String name) {
        super(name);
    }
 
    @Override
    void able() {
        BookList bookList=new BookList();
        while (true) {
            System.out.println("***********************");
            System.out.println("歡迎 "+getName()+" 登錄圖書管理系統");
            System.out.println("1.添加圖書");
            System.out.println("2.查看全部圖書");
            System.out.println("3.修改圖書");
            System.out.println("4.刪除圖書");
            System.out.println("0.退出系統");
            System.out.println("請輸入您的選擇");
            Scanner sc=new Scanner(System.in);
            int i=sc.nextInt();
            Operate.operates[i].work(bookList);
        }
 
    }
 
}
public class normalUser extends User{
    public normalUser(String name) {
        super(name);
    }
 
    @Override
    void able() {
        BookList bookList=new BookList();
        while (true) {
            System.out.println("***********************");
            System.out.println("歡迎 "+getName()+" 登錄圖書管理系統");
            System.out.println("1.查找圖書");
            System.out.println("2.借出圖書");
            System.out.println("3.歸還圖書");
            System.out.println("0.退出系統");
            System.out.println("請輸入您的選擇");
            Scanner sc=new Scanner(System.in);
            int i=sc.nextInt();
            Operate.operates[i].work(bookList);
        }
    }
}

Book類

書類,屬性有:書名,價格,狀態(已借出/未被借出)

告訴大傢一個快速生成JavaBean的小技巧,點擊右上角file->Settings->  Plugins->輸入ptj,下載好之後,右鍵點擊下面的PTJ to Javabean,就可以快速生成啦

package BOOK;
 
public class Book {
    private String name;
    private int prize;
    private boolean isBorrowed;
 
    public Book() {
    }
 
    public Book(String name, int prize) {
        this.name = name;
        this.prize = prize;
    }
 
    /**
     * 獲取
     * @return name
     */
    public String getName() {
        return name;
    }
 
    /**
     * 設置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * 獲取
     * @return prize
     */
    public int getPrize() {
        return prize;
    }
 
    /**
     * 設置
     * @param prize
     */
    public void setPrize(int prize) {
        this.prize = prize;
    }
 
    /**
     * 獲取
     * @return isBorrowed
     */
    public boolean IsBorrowed() {
        return isBorrowed;
    }
    public void setNoBorrowed(){
        isBorrowed=false;
    }
    public void setYesBorrowed(){
        isBorrowed=true;
    }
 
    /**
     * 設置
     * @param isBorrowed
     */
    public void setIsBorrowed(boolean isBorrowed) {
        this.isBorrowed = isBorrowed;
    }
 
    public String toString() {
        return "Book{name = " + name + ", prize = " + prize + ", isBorrowed = "  +
                (isBorrowed==false?"未被借出":"已借出")+"}";
    }
}

BookList類

書架類,定義瞭一個能放10本書的數組,提供瞭一些操作方法,具體請看註釋

package BOOK;
 
public class BookList {
    private Book[] books=new Book[10];
    private int size;//書的數量
    \\構造方法,默認放三本書進去
    public BookList() {
        books[0]=new Book("三國", 100000);
        books[1]=new Book("駱駝祥子", 10);
        books[2]=new Book("西遊記", 90);
        this.size = 3;
    }
    \\添加方法
    public void add(Book book){
        books[size]=book;
        size++;
    }
    \\獲取有多少本書
    public int getSize(){
        return size;
    }
    \\獲取書架
    public Book[] getBooks(){
        return books;
    }
    \\返回書架上索引為index的書
    public Book getBook(int index){
        return books[index];
    }
    \\往索引index插入已初始化的book
    public void setBook(int index,Book book){
        books[index]=book;
    }
    \\刪除書架上索引為index的書
    public void removeBook(int index){
        books[index]=null;
    }
    \\改變書的數量為i
    public void setSize(int i){
        size=i;
    }
 
}

Operate類

隻是一個管理員的操作類,作為其他操作的父類,裡面初始化一個操作數組,按照管理員類裡輸入的數字,再比照數組裡操作的索引來實現不同的功能

work方法讓子類去重寫,來實現不同的功能

package Ac;
 
import BOOK.BookList;
 
public class Operate {
    static public Operate[] operates={
             new exitOperate(),\\退出系統
             new addOperate(),\\添加圖書
            new showOperate(),\\展示所有圖書
            new updateOperate(),\\修改某一本書
            new removeOperate()\\刪除圖書
     };
     public void work(BookList bookList){
     }
}

隻是一個普通用戶的操作類,和上面的一樣,不在此過多贅述

package Pc;
 
import BOOK.BookList;
 
public class Operate {
    static public Operate[] operates={
            new exitOperate(),\\退出系統
            new findOperate(),\\查找圖書
            new borrowBook(),\\借書
            new returnBook()\\歸還圖書
    };
    public void work(BookList bookList){
    }
}

退出系統

我相信不用多講,各位dddd

package Ac;
 
import BOOK.BookList;
 
public class exitOperate extends Operate{
 
 
    @Override
    public void work(BookList bookList) {
        System.out.println("退出成功");
        System.exit(0);
    }
}

addOperate 添加書籍(管理員)

package Ac;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class addOperate extends Operate{
 
    @Override
    public void work(BookList bookList) {
        System.out.println("請輸入書名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        \\查找書名,相同則錄入失敗
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books=bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)){
                System.out.println("已有此書,錄入失敗");
                return;
            }
        }
        System.out.println("請輸入價格");
        int prize=sc.nextInt();
        \\把書放入書架
        Book book=new Book(name,prize);
        bookList.add(book);
        System.out.println("錄入成功");
    }
}

展示所有圖書(管理員)

很簡單,遍歷書架上所有書就完事瞭

package Ac;
 
import BOOK.BookList;
 
public class showOperate extends Operate{
    @Override
    public void work(BookList bookList) {
        if (bookList.getSize()==0){
            System.out.println("沒有書");
        }
        for (int i = 0; i < bookList.getSize(); i++) {
            System.out.println(bookList.getBook(i));
        }
    }
}

修改圖書 (管理員)

package Ac;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class updateOperate extends Operate{
    @Override
    public void work(BookList bookList) {
 
        System.out.println("請輸入您要修改的書的名字");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        \\查找錄入的書
        int i=findBook(bookList,name);
        if (i!=-1){
            \\錄入書名,如果書名存在則錄入失敗
            System.out.println("請輸入修改後書名");
            String newName=sc.next();
            for (int j = 0; j < bookList.getSize(); j++) {
                Book[] books=bookList.getBooks();
                if (bookList.getBook(j).getName().equals(newName)){
                    System.out.println("已有此書,錄入失敗");
                    return;
                }
            }
 
            System.out.println("請輸入修改後價格");
            int prize=sc.nextInt();
            \\添加書到書架
            Book book=new Book(newName,prize);
            bookList.setBook(i,book);
            System.out.println("修改成功");
            return;
        }
 
    }
    public int findBook(BookList bookList,String name){
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books=bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)){
                return i;
            }
        }
        System.out.println("沒有此書!!修改失敗");
        return -1;
    }
}

刪除圖書(管理員)

與修改差不多,在此不過多贅述

package Ac;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class removeOperate extends Operate{
    @Override
    public void work(BookList bookList) {
        System.out.println("請輸入要刪除的圖書");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int i=findBook(bookList,name);
        if(i!=-1){
            for (int j = i; j < bookList.getSize()-1; j++) {
                Book book=bookList.getBook(j+1);
                bookList.setBook(j,book);
            }
            bookList.setSize(bookList.getSize()-1);
            bookList.removeBook(bookList.getSize());
            System.out.println("刪除成功");
        }
 
    }
    public int findBook(BookList bookList,String name){
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books=bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)){
                return i;
            }
        }
        System.out.println("沒有此書!!刪除失敗");
        return -1;
    }
}

查找某一本書(普通用戶)

package Pc;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class findOperate extends Operate{
    @Override
    public void work(BookList bookList) {
        System.out.println("請輸入您要查找的書名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        findBook(bookList,name);
    }
    public void findBook(BookList bookList,String name){
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books=bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)){
                System.out.println("找到瞭!!");
                System.out.println(bookList.getBook(i));
                return;
            }
        }
        System.out.println("沒有此書!!");
 
    }
}

借出圖書(普通用戶)

借書原理很簡單,芝士把某一本書拿出來修改狀態再塞回書架

package Pc;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class borrowBook extends Operate{
    @Override
    public void work(BookList bookList) {
        System.out.println("請輸入您要借書的書名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int i=findBook(bookList,name);
        if (i!=-1){
            bookList.getBook(i).setYesBorrowed();;
            System.out.println("成功借出");
            return;
        }
 
    }
    public int findBook(BookList bookList,String name){
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books=bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)){
                return i;
            }
        }
        System.out.println("沒有此書!!");
        return -1;
    }
}

歸還圖書(普通用戶)

與上面的借書操作原理相同

package Pc;
 
import BOOK.Book;
import BOOK.BookList;
 
import java.util.Scanner;
 
public class returnBook extends Operate{
    @Override
    public void work(BookList bookList) {
        System.out.println("請輸入您要歸還的書名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int i=findBook(bookList,name);
        if (i!=-1){
            bookList.getBook(i).setNoBorrowed();;
            System.out.println("成功歸還");
            return;
        }
 
    }
    public int findBook(BookList bookList,String name){
        for (int i = 0; i < bookList.getSize(); i++) {
            Book[] books = bookList.getBooks();
            if (bookList.getBook(i).getName().equals(name)) {
                return i;
            }
        }
        System.out.println("沒有此書!!");
        return -1;
    }
}

到此這篇關於Java實現圖書管理系統的示例代碼的文章就介紹到這瞭,更多相關Java圖書管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: