圖書管理系統(tǒng)

public class 圖書管理系統(tǒng){
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Register().setVisible(true);
}
});

// 圖書管理類
public class BookManage {
    BookSet bookSet = new BookSet();

    /**
     * 初始化
     */
    public void initial() {
        book java = new book();
        java.setBook("java",1,2021/4/15",20);
        book renyue = new book();
        renyue.name = "程序語言基礎(chǔ)";
        renyue.setBook(."程序語言基礎(chǔ)",1 ,null,0);
        book jichu = new book();
        jichu.setBook("計算機(jī)基礎(chǔ)",1,null,0);
        bookSet.books[0] = java ;
        bookSet.books[1] = renyue ;
        bookSet.books[2] = jichu ;
    }

/*
菜單切換
 */
    public void startMenu(){
        boolean flag = true ;
        do {
            System.out.println("*******************");
            System.out.println("1 新增圖書");
            System.out.println("2 查看圖書");
            System.out.println("3 刪除圖書");
            System.out.println("4 借出圖書");
            System.out.println("5 歸還圖書");
            System.out.println("6 退出");
            System.out.println("*********************");
            System.out.println("請選擇");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt() ;
            switch (choice){
                case 1:
                    System.out.println("新增圖書");
                    add();
                    break;
                case 2:
                    System.out.println("查看圖書");
                    chakan();
                    break;
                case 3:
                    System.out.println("刪除圖書");
                    shan();
                    break;
                case 4:
                    System.out.println("借出圖書");
                    jie();
                    break;
                case 5:
                    System.out.println("歸還圖書");
                    huan();
                    break;
                case 6:
                    System.out.println("退出,謝謝使用");
                default:
                    System.out.println("輸入不符合要求請重新選擇");
            }
        }while (flag) ;
    }

/**
 * 查看圖書信息
 */
    public void chakan(){
        System.out.println("編號 \t\t\t  名稱 \t\t\t 狀態(tài) \t\t\t 借出日期 \t\t\t 庫存");
        for (int i = 0 ; i < this.bookSet.books.length ; i ++){
            if ( this.bookSet.books[i]!=null){
                this.bookSet.books[i].dayin(i+1);

                }
            }
        }

    /**
     * 添加圖書
     */
        public void add(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("請輸入圖書的名稱:");
            String name = scanner.next();
            System.out.println("請輸入庫存:");
            int number = scanner.nextInt() ;
            book newBook = new book();
            newBook.setBook(name , newBook.state, newBook.date, newBook.number);

            for (int i = 0 ; i < bookSet.books.length ; i++){
                if (bookSet.books[i] == null){
                    bookSet.books[i] = newBook ;  //把新建的對象放在數(shù)組中的第一個空位置
                    bookSet.books[i].number = number ; //庫存
                    System.out.println("添加成功");
                    break; //后續(xù)的空位置直接跳過
                }
            }
        }
    /**
     * 歸還
     */
        public void huan(){
            System.out.println("請輸入要?dú)w還書的名稱");
            Scanner scanner = new Scanner(System.in);
            String huan = scanner.next();
            boolean flag = true ;  //判斷是否歸還成功
            System.out.println("請輸入歸還日期");
            String riqi = scanner.next() ;
            for (int i = 0 ; i < bookSet.books.length ; i ++){
                if ( bookSet.books[i].name.equals(huan) &&bookSet.books[i].state == 0 ) { // 在書的狀態(tài)為一借出的狀態(tài)下可以歸還
                    bookSet.books[i].state = 1 ;
                    bookSet.books[i].date = riqi ;

                }else {
                    System.out.println("歸還成功");
                    flag = true ;
                    break;
                }
            }
        }
/**
 * 借書
 */

    public void jie(){
        System.out.println("請輸入要借書的名稱");
        Scanner scanner = new Scanner(System.in);
        String jie = scanner.next();
        boolean flag = true ;
        System.out.println("請輸入借書的日期");
        String riqi = scanner.next() ;
        for (int i = 0 ; i < bookSet.books.length ; i ++){
            if (bookSet.books[i] != null && bookSet.books[i].name.equals(jie)){
                if (bookSet.books[i].state == 1){
                    bookSet.books[i].state = 0 ;
                    flag = true ;
                    bookSet.books[i].date=riqi;
                }
                break;
            }

            }



         if (flag){
             System.out.println("借出成功");
        }else {
             System.out.println("借出失敗");
         }

    }
/**
 * 刪除
 */
    public void shan(){
        System.out.println("請輸入刪除圖書的編號:");
        Scanner scanner = new Scanner(System.in);
        int bianhao = scanner.nextInt();
        boolean flag = true ;
        for (int i = 0 ; i< bookSet.books.length ; i ++){
            if (bookSet.books[i] != null &&(i+1) == bianhao){
                if (bookSet.books[i].state != 0) {
                    int j = i;
                    while (bookSet.books[j + 1] != null) {
                        //后面元素向前移動
                        bookSet.books[j] = bookSet.books[j + 1];
                        j++;
                    }
                    bookSet.books[j] = null;
                    flag = true;
                    break;  //后面元素沒必要執(zhí)行
                }else {
                    System.out.println("圖書為借出狀態(tài)不能刪除");
                    flag= true ;
                }
            }else {
                flag = false ;
            }

        }
        if (flag){
            System.out.println("刪除成功");
        }else {
            System.out.println("刪除失敗");
        }
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容