1. 程式人生 > 其它 >java模擬實現圖書管理系統

java模擬實現圖書管理系統

文章目錄


一、使用者登入系統

package com.obj;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public interface Obj {
    BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));

}

package com.login;

import com.obj.Obj;

public interface Login extends
Obj { //資料地址 public static final String DATA = "E:\\Project\\Book\\user.data"; //登入功能 public void login()throws Exception; //註冊功能 public void enroll() throws Exception; //判斷該使用者是否已存在 public boolean exist(String name) throws Exception; //檢索使用者編號 public int
number()throws Exception; //啟動 public void start()throws Exception; void method01()throws Exception; }

實現使用者的登入、資料儲存及密碼修改

package com.login;

import java.io.*;

public class Login1 implements Login {
    private int existingUsersNum;   //現登入使用者編號


    //啟動
    public void start() throws Exception {
System.out.println("請輸入你需要的服務編號:" + "\n 1. 登入 2.建立使用者 3.退出"); int a = im(1, 3); service(a); } public void method01() throws Exception { System.out.println("您是否需要修改密碼,請選擇: 1. 是 2.暫不修改"); int a = im(1, 2); if (a == 1) { coodChange(); } } //輸入編號 public int im(int min, int max) throws Exception { int aa; while (true) { String str = bu.readLine(); try { int a = Integer.valueOf(str); if (a >= min && a <= max) { aa = a; break; } else { System.out.println("請輸入合理的服務標號"); } } catch (Exception e) { System.out.println("格式錯誤,請重寫輸入"); } } return aa; } //服務 public void service(int a) throws Exception { switch (a) { case 1: login(); //登入 ,這裡應該獲取標號 break; case 2: enroll(); //建立 break; } } //登入 public void login() throws Exception { int cc = -1; File ff = new File(DATA); if (ff.exists()) { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); String str = "使用者名稱"; String name = storage(str, 2, 18); //輸入使用者名稱 login01(oi, name, cc); oi.close(); } } public void login01(ObjectInputStream oi, String name, int cc) throws Exception { while (true) { //沒有-1所以需要用EOFException異常來中斷程式 try { Object obj = oi.readObject(); if (obj instanceof User) { User us = (User) obj; if (name.equals(us.getName())) { //是否有這個使用者 if (ww(us)) { //返回為false則登入成功; start(); } else { existingUsersNum = us.getNum(); break; } } } } catch (EOFException e) { System.out.println("您輸入的賬戶不存在,請確認後重新輸入"); start(); break; } } } //當有該賬號時,比較密碼 public boolean ww(User us) throws Exception { String str1 = "密碼"; String cood = storage(str1, 10, 18); if (cood.equals(us.getCood())) { System.out.println("登入成功"); return false; } else { System.out.println("密碼錯誤,請確認後,重新登入"); return true; } } //註冊,將使用者物件寫入檔案中 public void enroll() throws Exception { int cc = -1; String name; String cood; while (true) { String str = ""; str = "使用者名稱"; name = storage(str, 2, 18); if (exist(name)) { //不重名再進行下面的判斷 System.out.println("使用者名稱重複,請重新輸入"); } else { String str1 = "密碼"; cood = storage(str1, 10, 18); break; } } int num = number(); User us = new User(); us.setName(name); us.setCood(cood); us.setNum(num + 1); acquire(us); System.out.println("恭喜您建立使用者成功,您的編號為:" + us.getNum() + " ,您的賬戶為:" + us.getName() + " ,您的密碼為:" + us.getCood()); this.existingUsersNum = us.getNum(); } //輸入資訊並作合理性判定 public String storage(String str, int min, int max) throws Exception { String str1; while (true) { System.out.println("請輸入您的" + str + " ,要求:" + min + "位以上," + max + "位以下"); str1 = bu.readLine(); //整行錄入 if ((str1 != null) && (str1.trim().length() >= min && str1.length() <= max)) { break; } else { System.out.println("輸入不合理,請重寫輸入"); } } return str1; } //有一個檔案作為資料的儲存位置,先將他全部讀出,寫入可變長陣列中,再加上要新增的資料,然後再從陣列中將資料讀取出來寫入檔案中 public void acquire(User us) throws Exception { //寫入 File ff = new File(DATA); ByteArrayOutputStream bao = new ByteArrayOutputStream(); //需要單獨定義 ObjectOutputStream ou = new ObjectOutputStream(new BufferedOutputStream(bao)); //寫入陣列中 //再讀取檔案的時候,需要做存在性判定,寫出不需要 if (ff.exists()) { ObjectInputStream oi = new ObjectInputStream(new FileInputStream(ff)); //讀取檔案 while (true) { try { Object obj = oi.readObject(); ou.writeObject(obj); } catch (EOFException e) { break; } } oi.close(); } ou.writeObject(us); ou.close(); //從陣列中讀取並寫入檔案 ObjectInputStream oi1 = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(bao.toByteArray()))); //從陣列中讀取資料 ObjectOutputStream ou1 = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(DATA)));//寫出 while (true) { try { Object obj = oi1.readObject(); ou1.writeObject(obj); } catch (EOFException e) { break; } } oi1.close(); ou1.close(); } //判定輸入的使用者是否存在 public boolean exist(String name) throws Exception { boolean aa = false; //讀取檔案 File ff = new File(DATA); if (ff.exists()) { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); while (true) { //沒有-1所以需要用EOFException異常來中斷程式 try { Object obj = oi.readObject(); if (obj instanceof User) { User us = (User) obj; if (name.equals(us.getName())) { aa = true; break; } } } catch (EOFException e) { break; } } oi.close(); } return aa; } //檢索使用者編號 public int number() throws Exception { int max = -1; File ff = new File(DATA); if (ff.exists()) { //讀取檔案,搜尋檔案中編號最大的使用者,則新註冊的使用者,最大編號加1即可 ObjectInputStream oi = new ObjectInputStream(new FileInputStream(DATA)); while (true) { try { Object obj = oi.readObject(); if (obj instanceof User) { User us = (User) obj; if (max < us.getNum()) { max = us.getNum(); } } } catch (EOFException e) { break; } } oi.close(); } return max; } //列印所有使用者資訊: private void list() throws Exception { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); while (true) { try { Object obj = oi.readObject(); if (obj instanceof User) { User us = (User) obj; System.out.println(us); } } catch (EOFException e) { break; } } } //獲取當前使用者資訊 public void whoi() throws Exception { ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); while (true) { try { Object obj = in.readObject(); User us = (User) obj; if (us.getNum() == existingUsersNum) { System.out.println(us); break; } } catch (EOFException e) { break; } } in.close(); } //修改密碼 public void coodChange() throws Exception { String str = storage("之前密碼", 10, 18); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); while (true) { try { Object obj = in.readObject(); if (obj instanceof User) { User us = (User) obj; if (us.getNum() == existingUsersNum) { //尋找當前使用者 if (str.equals(us.getCood())) { //判斷輸入的密碼是否正確 String str1 = storage("新密碼", 10, 18); //新密碼; //要把新密碼寫入檔案中 coodChange01(str1); System.out.println("密碼修改成功,請保護好您的密碼"); break; } else { System.out.println("您輸入的當前密碼不正確"); method01(); break; } } } } catch (EOFException e) { break; } } in.close(); } //修改密碼後重新寫入,先寫入快取陣列中,再從陣列中讀取,再寫入檔案 public void coodChange01(String str1) throws Exception { File ff = new File(DATA); if (ff.exists()) { ByteArrayOutputStream by = new ByteArrayOutputStream(); ObjectOutputStream ou = new ObjectOutputStream(by); ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(DATA))); while (true) { //讀一次讀完 try { Object obj = oi.readObject(); if (obj instanceof User) { User us = (User) obj; if (us.getNum() == existingUsersNum) { us.setCood(str1); //設定新密碼 ou.writeObject(us); } else { ou.writeObject(obj); } } } catch (EOFException e) { break; } } ou.close(); oi.close(); ObjectInputStream oi1 = new ObjectInputStream(new ByteArrayInputStream(by.toByteArray())); ObjectOutputStream ou1 = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(DATA))); while (true) { try { Object obj = oi1.readObject(); ou1.writeObject(obj); } catch (EOFException e) { break; } } oi1.close(); ou1.close(); } } //測試的主方法 public static void main(String[] args) throws Exception { Login lo = new Login1(); lo.start(); } }

使用者類

package com.login;

import java.io.Serializable;

public class User implements Serializable {
    private String name;        //使用者名稱
    private String cood;        //使用者密碼
    private int num;            //使用者編號
    private boolean status=false;     //身份是否為管理員

    public String getName() {
        return name;
    }

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

    public String getCood() {
        return cood;
    }

    public void setCood(String cood) {
        this.cood = cood;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public User() {
    }

    public User(String name, String cood, int num, boolean status) {
        this.name = name;
        this.cood = cood;
        this.num = num;
        this.status = status;
    }

    @Override
    public String toString() {
        return "使用者編號:" + num +
                ",使用者名稱:" + name +
                "使用者密碼:" + cood +
                ", 是否為管理員" + status;
    }
}

二、實現對書籍的管理

書籍類

package com.book;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 圖書:有名字,狀態(借出去沒),日期,借出次數;
 */

public class Book {
    private String name;
    private String state = "在";                                        // 狀態,圖書在不在
    private Date date;                                                // 借出日期
    private int frequency = 0;                                        // 借出次數 ,每借出一次++
    private long money = 0;
    //設定天數取整,一個月內不收費,超出一個月一天一毛
    private String str;
    SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public long getMoney() {
        return money;
    }


    public String getName() {
        return name;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

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

    public String getState() {
        return state;
    }

    public void setState(String state) {
        if ("不在".equals(state) || "在".equals(state)) {
            this.state = state;
        } else {
            System.out.println("輸入錯誤,請重新輸入");
        }
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency() {
        frequency++;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    //借書
    public void getBook() {
        frequency++;
        state = "不在";
        date = new Date();
        str = si.format(date);
    }

    public void setBook() {
        state = "在";
        date = null;
        str = null;
    }

    public void Money01() {
        Date date01 = new Date();
        long aa = date01.getTime() - date.getTime();
        String str1 = si.format(date);
        String str2 = si.format(date01);
        long bb = aa / (1000 * 60 * 60 * 24);            //天數
        if (bb > 30) {
            money = (bb - 30) * 1;                   //分
            System.out.println("您歸還的書籍借閱時間為:" + str1 + "歸還時間為:" + str2);
            System.out.println("您所借閱的書籍,產生的費用為:" + money / 10 + "元" + money % 10 + "分,請繳費");
        } else {
            System.out.println("您歸還的書籍借閱時間為:" + str1 + "歸還時間為:" + str2);
            System.out.println("感謝您遵守約定,歡迎您的下次借閱");
        }

    }

    @Override
    public String toString() {
        return "書名 :" + name + "; 狀態 :" + state + " ;借出日期 :" + str + ";借出次數=" + frequency;
    }

}

抽象書架類

package com.bookshelf;

import com.book.Book;
import com.obj.Obj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.Scanner;

/**
 * 書架:存放圖書的陣列,最多存放多少本書,目前有多少本書 ;書架可以新增圖書
 * 可以把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊。
 * 有借出時間和歸還時間,能管理書架的費用,可以初始化系統裡的書,能借書,還書,增加圖書,
 * 刪除圖書,費用統計,
 * 排行榜,增加刪除借出歸還統計圖書等。
 */
public abstract class BookShelf implements Obj {           //書架,將書籍分類

    abstract void methood();

    abstract void start() throws Exception;

    public void method() throws Exception {
        System.out.println("請選擇您需要的服務:");
        System.out.println("1.查巡相關      2.借出與歸還      3.新增或刪除    4.返回上一級");
        int a = shuRu(1, 4);
        if (a != 4) {
            panDuan(a);
        }
    }
    //輸入服務編號
    public int shuRu(int min, int max) throws Exception {
        while (true) {
            try {
                String str = bu.readLine();
                int a = Integer.valueOf(str);
                if (a >= min && a <= max) {
                    return a;
                } else {
                    System.out.println("請輸入合理的服務編號");
                }
            } catch (Exception e) {
                System.out.println("輸入格式錯誤,請重新輸入");
            }
        }
    }

    //選擇服務
    public void panDuan(int a) throws Exception {
        switch (a) {
            case 1:         //檢視相關
                method01();
                method();
                break;
            case 2:         //2.借出與歸還
                method03();
                method();
                break;
            case 3:         //3.新增或刪除
                method04();
                method();
                break;
            case 4:
                break;
        }
    }

    //2.借出與歸還
    public void method03() throws Exception {
        System.out.println("請選擇您需要的服務: 1.借書    2.還書     3.返回上一級");
        int a = shuRu(1, 3);
        String str;
        switch (a) {
            case 1:         //借書
                str = "借";
                jieHuanShu(str);
                method03();
                break;
            case 2:         //還書
                str = "還";
                jieHuanShu(str);
                method03();
                break;
            case 3:
                break;
        }
    }

    //新增與刪除
    public void method04() throws Exception {
        System.out.println("請選擇您需要的服務: 1.新增圖書    2.刪除圖書     3.返回上一級");
        int a = shuRu(1, 3);
        String str;
        switch (a) {
            case 1:         //新增圖書
                str = "新增";
                String name = shuBook(str);
                add(name);
                break;
            case 2:         //刪除圖書
                str = "刪除";
                String name1 = shuBook(str);
                reMove(name1);
                break;
            case 3:
                break;
        }
    }

    //服務1 檢視相關
    public void method01() throws Exception {
        System.out.println("請選擇您需要的服務:");
        System.out.println("1.書架存放上限    2.書架目前有多少書   3.獲取書籍列表   4.按借出按次數排序    5.返回上一級");
        int aa = shuRu(1, 5);
        switch (aa) {
            case 1:         //1.書架存放上限
                length();
                method01();
                break;
            case 2:         //2.書架目前有多少書
                System.out.println("該書架現存書籍為:" + how() + "本");
                method01();
                break;
            case 3:         //3.獲取書籍列表
                method02();
                method01();
                break;
            case 4:         //4.按借出按次數排序
                getname();
                method01();
                break;
            case 5:
                break;
        }

    }

    //輸入書籍名稱
    public String shuBook(String str) throws Exception {
        System.out.println("請輸入要" + str + "的書籍名稱");
        String str1;
        while (true) {
            str1 = bu.readLine();
            if (str1.equals(null)) {
                System.out.println("書名不能為空");
            } else {
                return str1;
            }
        }
    }

    public String shuBook01(String str) throws Exception {
        String str1;
        while (true) {
            System.out.println("請輸入您想" + str + "的書籍");
            str1 = bu.readLine();
            if (str1.equals(null)) {
                System.out.println("書名不能為空");
            } else {
                return str1;
            }
        }
    }

    public abstract void length();                     //獲取書架可以裝多少本書

    public abstract int how();                           //獲取書架有多少本書

    public abstract void method02();        //獲取書籍列表

    public abstract void jieHuanShu(String str) throws Exception;        //借還書

    public abstract void getname();                    //把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊

    public abstract void add(String name);

    //刪除
    public abstract void reMove(String name);

}

科普類書籍

package com.bookshelf;

import com.book.Book;

import java.util.Objects;


public class Science extends BookShelf {
    private Book[] bo1 = new Book[100];                      //科普類
    public void methood() {                //給書架裡新增基本預設書籍
        add("自然選擇");
        add("昆蟲記");
        add("世界100自然奇觀");
        add("自中國未解之謎");
        add("見微知著");
        add("現實不似你所見");
        add("漫步到宇宙盡頭");
        add("萬物起源");

    }
    public void start()throws Exception {
        methood();
        method();
    }
    public void length() {                        //獲取書架可以裝多少本書
        System.out.println("該書架存放書籍上限為:" + bo1.length + "本");
    }

    public int how() {                            //獲取書架有多少本書
        int a = 0;
        for (; a < bo1.length; a++) {
            if (Objects.equals(bo1[a], null)) {
                break;
            }
        }
        return a;
    }

    public void method02() {         //獲取書籍列表
        for (int a = 0; a < how(); a++) {
            System.out.println(bo1[a]);
        }
    }

    public void jieHuanShu(String str) throws Exception {           //借還書
        String str1 = shuBook01(str);
        for (int a = 0; a < how(); a++) {
            if (bo1[a].getName().equals(str1)) {
                if ("借".equals(str)) {
                    if (bo1[a].getState().equals("在")) {
                        bo1[a].getBook();
                        System.out.println("借書成功,您借的書籍為:" + bo1[a].getName() + ",借出日期為:" + bo1[a].getStr() + ",借出次數為:" + bo1[a].getFrequency());
                        break;
                    } else if (bo1[a].getState().equals("不在"))
                        System.out.println("很遺憾,您想借的書籍已借出,請確認後重新輸入");
                    break;
                } else if ("還".equals(str)) {
                    if (bo1[a].getState().equals("在")) {
                        System.out.println("您想要歸還的書籍並未借出,請確認後再輸入");
                        break;
                    } else if (bo1[a].getState().equals("不在")) {
                        bo1[a].Money01();
                        bo1[a].setBook();
                        System.out.println("還書成功,您歸還的書籍為:" + bo1[a].getName());
                        break;
                    }
                }

            } else {
                System.out.println("您想" + str + "的書籍不存在,請確認後重新輸入");
                break;
            }
        }
    }



    public void getname() {                    //把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊
        int bb = how();
        for (int a = 1; a < bb; a++) {            //只對書架上現有的書籍進行排序
            for (int b = 0; b < bb - a; b++) {
                if (bo1[b].getFrequency() < bo1[b + 1].getFrequency()) {
                    Book dd = bo1[b];
                    bo1[b] = bo1[b + 1];
                    bo1[b + 1] = dd;
                }
            }
        }
        for (int a = 0; a < how(); a++) {
            System.out.println(bo1[a]);
        }
    }

    public void add(String name) {
        Book book = new Book();
        book.setName(name);
        //新增新書籍,輸入書籍的名字,其他狀態均為預設
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo1[a].getName())) {
                System.out.println("您要新增的書籍已存在,請確認後重新輸入");
                break;
            } else if (!name.equals(bo1[a].getName())) {
                int aa = how();
                bo1[aa] = book;
                break;
            }
        }
        if (how() == 0) {
            bo1[0] = book;
        }
    }

    //刪除
    public void reMove(String name) {
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo1[a].getName())) {
                Book[] bo1 = new Book[100];
                for (int aa = 0; aa < how(); aa++) {
                    if (aa < a) {
                        bo1[aa] = bo1[aa];
                    } else if (aa == a) {
                        continue;
                    } else if (aa > a) {
                        bo1[aa - 1] = bo1[aa];
                    }
                }
                bo1 = bo1;
                System.out.println("刪除成功");
                break;
            } else {
                System.out.println("您要刪除的書籍不存在,請確認後重新輸入");
                break;
            }
        }

    }

}

教學類書籍

package com.bookshelf;

import com.book.Book;

import java.util.Objects;

public class Theaching  extends BookShelf {
    private Book[] bo = new Book[100];                      //教學類
    public void methood() {                //給書架裡新增基本預設書籍
        add("高等數學");
        add("大學英語");
        add("計算機基礎");
        add("線性代數");
    }
    public void start() throws Exception{
        methood();
        method();
    }

    public void length() {                        //獲取書架可以裝多少本書
        System.out.println("該書架存放書籍上限為:" + bo.length + "本");
    }

    public int how() {                            //獲取書架有多少本書
        int a = 0;
        for (; a < bo.length; a++) {
            if (Objects.equals(bo[a], null)) {
                break;
            }
        }
        return a;
    }

    public void method02() {         //獲取書籍列表
        for (int a = 0; a < how(); a++) {
            System.out.println(bo[a]);
        }
    }

    public void jieHuanShu(String str) throws Exception {           //借還書
        String str1 = shuBook01(str);
        for (int a = 0; a < how(); a++) {
            if (bo[a].getName().equals(str1)) {
                if ("借".equals(str)) {
                    if (bo[a].getState().equals("在")) {
                        bo[a].getBook();
                        System.out.println("借書成功,您借的書籍為:" + bo[a].getName() + ",借出日期為:" + bo[a].getStr() + ",借出次數為:" + bo[a].getFrequency());
                        break;
                    } else if (bo[a].getState().equals("不在"))
                        System.out.println("很遺憾,您想借的書籍已借出,請確認後重新輸入");
                    break;
                } else if ("還".equals(str)) {
                    if (bo[a].getState().equals("在")) {
                        System.out.println("您想要歸還的書籍並未借出,請確認後再輸入");
                        break;
                    } else if (bo[a].getState().equals("不在")) {
                        bo[a].Money01();
                        bo[a].setBook();
                        System.out.println("還書成功,您歸還的書籍為:" + bo[a].getName());
                        break;
                    }
                }

            } else {
                System.out.println("您想" + str + "的書籍不存在,請確認後重新輸入");
                break;
            }
        }
    }



    public void getname() {                    //把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊
        int bb = how();
        for (int a = 1; a < bb; a++) {            //只對書架上現有的書籍進行排序
            for (int b = 0; b < bb - a; b++) {
                if (bo[b].getFrequency() < bo[b + 1].getFrequency()) {
                    Book dd = bo[b];
                    bo[b] = bo[b + 1];
                    bo[b + 1] = dd;
                }
            }
        }
        for (int a = 0; a < how(); a++) {
            System.out.println(bo[a]);
        }
    }

    public void add(String name) {
        Book book = new Book();
        book.setName(name);
        //新增新書籍,輸入書籍的名字,其他狀態均為預設
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo[a].getName())) {
                System.out.println("您要新增的書籍已存在,請確認後重新輸入");
                break;
            } else if (!name.equals(bo[a].getName())) {
                int aa = how();
                bo[aa] = book;
                break;
            }
        }
        if (how() == 0) {
            bo[0] = book;
        }
    }

    //刪除
    public void reMove(String name) {
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo[a].getName())) {
                Book[] bo1 = new Book[100];
                for (int aa = 0; aa < how(); aa++) {
                    if (aa < a) {
                        bo1[aa] = bo[aa];
                    } else if (aa == a) {
                        continue;
                    } else if (aa > a) {
                        bo1[aa - 1] = bo[aa];
                    }
                }
                bo = bo1;
                System.out.println("刪除成功");
                break;
            } else {
                System.out.println("您要刪除的書籍不存在,請確認後重新輸入");
                break;
            }
        }

    }


}

文學類書籍

package com.bookshelf;

import com.book.Book;

import java.util.Objects;
import java.util.Scanner;

public class Letter extends BookShelf {          //文學類
    private Book[] bo2 = new Book[100];
    public void methood() {//給書架裡新增基本預設書籍
        add("鋼鐵是怎樣練成的");
        add("百年孤獨");
        add("論語");
        add("傲慢與偏見");
        add("紅與黑");
        add("平凡的世界");
        add("簡愛");
        add("詩經");
        add("生死場");
        add("一千零一夜");
        add("老人與海");
    }
    public void start() throws Exception {
        methood();
        method();
    }

    public void add(String name) {
        Book book = new Book();
        book.setName(name);
        //新增新書籍,輸入書籍的名字,其他狀態均為預設
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo2[a].getName())) {
                System.out.println("您要新增的書籍已存在,請確認後重新輸入");
                break;
            } else if (!name.equals(bo2[a].getName())) {
                int aa = how();
                bo2[aa] = book;
                break;
            }
        }
        if (how() == 0) {
            bo2[0] = book;
        }
    }

    public void length() {                        //獲取書架可以裝多少本書
        System.out.println("該書架存放書籍上限為:" + bo2.length + "本");
    }

    public int how() {                            //獲取書架有多少本書
        int a = 0;
        for (; a < bo2.length; a++) {
            if (Objects.equals(bo2[a], null)) {
                break;
            }
        }
        return a;
    }

    public void method02() {         //獲取書籍列表
        for (int a = 0; a < how(); a++) {
            System.out.println(bo2[a]);
        }
    }

    public void jieHuanShu(String str) throws Exception {           //借還書
        String str1 = shuBook01(str);
        for (int a = 0; a < how(); a++) {
            if (this.bo2[a].getName().equals(str1)) {
                if ("借".equals(str)) {
                    if (bo2[a].getState().equals("在")) {
                        bo2[a].getBook();
                        System.out.println("借書成功,您借的書籍為:" + bo2[a].getName() + ",借出日期為:" + bo2[a].getStr() + ",借出次數為:" + bo2[a].getFrequency());
                        break;
                    } else if (bo2[a].getState().equals("不在"))
                        System.out.println("很遺憾,您想借的書籍已借出,請確認後重新輸入");
                    break;
                } else if ("還".equals(str)) {
                    if (bo2[a].getState().equals("在")) {
                        System.out.println("您想要歸還的書籍並未借出,請確認後再輸入");
                        break;
                    } else if (bo2[a].getState().equals("不在")) {
                        bo2[a].Money01();
                        bo2[a].setBook();
                        System.out.println("還書成功,您歸還的書籍為:" + bo2[a].getName());
                        break;
                    }
                }

            } else {
                System.out.println("您想" + str + "的書籍不存在,請確認後重新輸入");
                break;
            }
        }
    }


    public void getname() {                    //把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊
        int bb = how();
        for (int a = 1; a < bb; a++) {            //只對書架上現有的書籍進行排序
            for (int b = 0; b < bb - a; b++) {
                if (bo2[b].getFrequency() < bo2[b + 1].getFrequency()) {
                    Book dd = bo2[b];
                    bo2[b] = bo2[b + 1];
                    bo2[b + 1] = dd;
                }
            }
        }
        for (int a = 0; a < how(); a++) {
            System.out.println(bo2[a]);
        }
    }



    //刪除
    public void reMove(String name) {
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo2[a].getName())) {
                Book[] bo1 = new Book[100];
                for (int aa = 0; aa < how(); aa++) {
                    if (aa < a) {
                        bo1[aa] = bo2[aa];
                    } else if (aa == a) {
                        continue;
                    } else if (aa > a) {
                        bo1[aa - 1] = bo2[aa];
                    }
                }
                bo2 = bo1;
                System.out.println("刪除成功");
                break;
            } else {
                System.out.println("您要刪除的書籍不存在,請確認後重新輸入");
                break;
            }
        }

    }

}

娛樂類書籍

package com.bookshelf;

import com.book.Book;

import java.util.Objects;

public class Recreation extends BookShelf {
    private Book[] bo3 = new Book[100];                      //y娛樂
    public void methood() {                //給書架裡新增基本預設書籍
        add("求魔");
        add("將夜");
        add("劍來");
        add("慶餘年");

    }
    public void start()throws Exception {
        methood();
        method();
    }
    public void length() {                        //獲取書架可以裝多少本書
        System.out.println("該書架存放書籍上限為:" + bo3.length + "本");
    }

    public int how() {                            //獲取書架有多少本書
        int a = 0;
        for (; a < bo3.length; a++) {
            if (Objects.equals(bo3[a], null)) {
                break;
            }
        }
        return a;
    }

    public void method02() {         //獲取書籍列表
        for (int a = 0; a < how(); a++) {
            System.out.println(bo3[a]);
        }
    }

    public void jieHuanShu(String str) throws Exception {           //借還書
        String str1 = shuBook01(str);
        for (int a = 0; a < how(); a++) {
            if (bo3[a].getName().equals(str1)) {
                if ("借".equals(str)) {
                    if (bo3[a].getState().equals("在")) {
                        bo3[a].getBook();
                        System.out.println("借書成功,您借的書籍為:" + bo3[a].getName() + ",借出日期為:" + bo3[a].getStr() + ",借出次數為:" + bo3[a].getFrequency());
                        break;
                    } else if (bo3[a].getState().equals("不在"))
                        System.out.println("很遺憾,您想借的書籍已借出,請確認後重新輸入");
                    break;
                } else if ("還".equals(str)) {
                    if (bo3[a].getState().equals("在")) {
                        System.out.println("您想要歸還的書籍並未借出,請確認後再輸入");
                        break;
                    } else if (bo3[a].getState().equals("不在")) {
                        bo3[a].Money01();
                        bo3[a].setBook();
                        System.out.println("還書成功,您歸還的書籍為:" + bo3[a].getName());
                        break;
                    }
                }

            } else {
                System.out.println("您想" + str + "的書籍不存在,請確認後重新輸入");
                break;
            }
        }
    }



    public void getname() {                    //把借出的圖書按次數從高到低排序,可以檢視所有圖書的資訊
        int bb = how();
        for (int a = 1; a < bb; a++) {            //只對書架上現有的書籍進行排序
            for (int b = 0; b < bb - a; b++) {
                if (bo3[b].getFrequency() < bo3[b + 1].getFrequency()) {
                    Book dd = bo3[b];
                    bo3[b] = bo3[b + 1];
                    bo3[b + 1] = dd;
                }
            }
        }
        for (int a = 0; a < how(); a++) {
            System.out.println(bo3[a]);
        }
    }

    public void add(String name) {
        Book book = new Book();
        book.setName(name);
        //新增新書籍,輸入書籍的名字,其他狀態均為預設
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo3[a].getName())) {
                System.out.println("您要新增的書籍已存在,請確認後重新輸入");
                break;
            } else if (!name.equals(bo3[a].getName())) {
                int aa = how();
                bo3[aa] = book;
                break;
            }
        }
        if (how() == 0) {
            bo3[0] = book;
        }
    }

    //刪除
    public void reMove(String name) {
        for (int a = 0; a < how(); a++) {
            if (name.equals(bo3[a].getName())) {
                Book[] bo1 = new Book[100];
                for (int aa = 0; aa < how(); aa++) {
                    if (aa < a) {
                        bo1[aa] = bo3[aa];
                    } else if (aa == a) {
                        continue;
                    } else if (aa > a) {
                        bo1[aa - 1] = bo3[aa];
                    }
                }
                bo3 = bo1;
                System.out.println("刪除成功");
                break;
            } else {
                System.out.println("您要刪除的書籍不存在,請確認後重新輸入");
                break;
            }
        }

    }
}

書架啟動類

package com.bookshelf;

import com.obj.Obj;

import java.io.IOException;

public class Administrators implements Obj {
    BookShelf bo1 = new Theaching();                //教學類
    BookShelf bo2 = new Science();                  //科普
    BookShelf bo3 = new Letter();                   //文學類
    BookShelf bo4 =new Recreation();                //娛樂

    public void method01() throws Exception {
        System.out.println("請選擇您需要的服務 : 1.前往圖書館開啟智慧人生           2.殘忍拒絕,我要離開        ");
        int b =method(1,2);
        if(b==1){
            methodoo();
        }
    }

    public void methodoo()throws Exception{
        System.out.println("請選擇您想訪問的書架編號: 1.教學類      2.科普類       3.文學類      4.娛樂類       5.退出");
        int a = method(1,5);
        method01(a);
    }


    public int method(int min ,int max ) throws IOException {
        int a;
        while (true) {
            try {
                String str = bu.readLine();
                a = Integer.valueOf(str);
                if (a >= min && a <= max) {
                    break;
                } else {
                    System.out.println("請輸入合理的服務編號");
                }
            } catch (Exception e) {
                System.out.println("格式錯誤,請重新輸入");

            }
        }
        return a;
    }

    public void method01(int a) throws Exception {
        switch (a) {
            case 1:
                bo1.start();
                methodoo();
                break;
            case 2:
                bo2.start();
                methodoo();
                break;
            case 3:
                bo3.start();
                methodoo();
                break;
            case 4:
                bo4.start();
                methodoo();
                break;
        }
    }
}

三、啟動類

package com.start;

import com.bookshelf.*;
import com.login.Login;
import com.login.Login1;

public class Start {
    public static void main(String[] args)throws Exception {
        Administrators ad =new Administrators();
        Login lo = new Login1();
        lo.start();
        lo.method01();
        ad. method01();

    }
}