圖書(shū)管理系統(tǒng)

筆記

package 圖書(shū)管理系統(tǒng);

import java.lang.reflect.Array;
import java.util.Scanner;

public class BookMgr {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        //數(shù)據(jù)初始化,最多6本書(shū)
        //圖書(shū):圖書(shū)名稱(chēng)   圖書(shū)借閱狀態(tài)  圖書(shū)借閱日期  圖書(shū)借閱次數(shù)
        String[] names = new String[6];
        int[] states = new int[6];  //0可借   1已借出
        int[] dates = new int[6];
        int[]counts = new int[6];
        
        names[0] = "白雪公主";
        dates[0] = 5;
        states[0] = 1;
        counts[0] = 10;
        
        names[1] = "mybook";
        dates[1] = 0;
        states[1] = 0;
        counts[1] = 16;
        
        names[2] = "舒克貝塔";
        dates[2] = 0;
        states[2] = 0;
        counts[2] = 78;
        
        //用戶(hù)是否退出系統(tǒng)
        boolean flag = true;
        //用戶(hù)輸入0返回主菜單
        int num = 0;
        //圖書(shū)管理相關(guān)操作:查看圖書(shū)信息   新增圖書(shū)    借閱圖書(shū)    歸還圖書(shū)    刪除圖書(shū)    退出系統(tǒng)
        do 
        {
            System.out.println("**********歡迎使用迷你圖書(shū)管理系統(tǒng)**********");
            System.out.println("1.查看圖書(shū)");
            System.out.println("2.新增圖書(shū)");
            System.out.println("3.借閱圖書(shū)");
            System.out.println("4.歸還圖書(shū)");
            System.out.println("5.刪除圖書(shū)");
            System.out.println("6.退出系統(tǒng)");
            System.out.print("請(qǐng)選擇您的操作:");
            int choose = in.nextInt();
            switch ( choose )
            {
                case 1:
                    //查看圖書(shū)
                    System.out.println("\n圖書(shū)信息列表-->");
                    System.out.println("圖書(shū)序號(hào)\t圖書(shū)名稱(chēng)\t圖書(shū)狀態(tài)\t借閱日期\t借閱次數(shù)");
                    for ( int i=0; i<names.length; i++ )
                    {
                        if( names[i] != null )
                        {
                            String state = ( states[i] == 0 ) ? "可借閱" : "已借出";
                            String date = ( dates[i] == 0 ) ? "" : dates[i]+"日";
                            String count = counts[i] + "次";
                            System.out.println( ( i+1 ) + "\t" + names[i] + "\t" + state + "\t" +  date + "\t" + count );
                        }
                        else
                        {
                            //遇到第一個(gè)為null的圖書(shū)名稱(chēng),意味著后面的圖書(shū)名稱(chēng)也為null,不必要循環(huán)了
                            break;
                        }
                    }
                    break;
                case 2:
                    //新增圖書(shū)
                    System.out.println("\n新增圖書(shū)-->");
                    System.out.println("請(qǐng)輸入新增圖書(shū)名稱(chēng):");
                    String name = in.next();
                    //是否能新增圖書(shū),如果貨架滿(mǎn)了(6),則無(wú)法添加false,反之,則能添加true
                    boolean flagAdd = false;
                    for ( int i=0; i<names.length; i++ )
                    {
                        if ( names[i] == null )
                        {
                            flagAdd = true ;
                            names[i] = name;
                            System.out.println("圖書(shū)《"+name+"》添加成功");
                            break;
                        }
                    }
                    if ( !flagAdd )
                    {
                        System.out.println("對(duì)不起,貨架已滿(mǎn),無(wú)法添加圖書(shū)!");
                    }
                    break;
                case 3:
                    //借閱圖書(shū)
                    System.out.println("\n借閱圖書(shū)圖書(shū)-->");
                    System.out.println("請(qǐng)輸入借閱圖書(shū)的名稱(chēng):");
                    String want = in.next();
                    for ( int i=0; i<names.length; i++ )
                    {
                        if ( names[i] == null )
                        {
                            //沒(méi)有找到你要的書(shū)
                            System.out.println("沒(méi)有找到您要借的書(shū)!");
                            break;
                        }
                        else if ( names[i].equals(want) && states[i] == 0 )
                        {
                            //找到了您要借的書(shū),書(shū)的狀態(tài)是可借的,可以借
                            System.out.println("請(qǐng)輸入借閱日期:");
                            dates[i] = in.nextInt();
                            while ( dates[i]<1 || dates[i]>31 )
                            {
                                System.out.println("數(shù)字格式不正確,請(qǐng)輸入1-31之間的數(shù)字:");
                                dates[i] = in.nextInt();
                            }
                            System.out.println("借出《"+want+"》成功!");
                            states[i] = 1;  //將書(shū)的狀態(tài)修改為已借出
                            counts[i]++;    //將書(shū)的借閱次數(shù)累加
                            break;
                        }
                        else if ( names[i].equals(want) && states[i] == 1 )
                        {
                            //找到了您要借的書(shū),書(shū)的狀態(tài)是已借出的,不可以借
                            System.out.println("該書(shū)已經(jīng)借出去了!");
                            break;
                        }
                    }
                    break;
                case 4:
                    //歸還圖書(shū)
                    System.out.println("\n歸還圖書(shū)-->");
                    System.out.println("請(qǐng)輸入歸還圖書(shū)名稱(chēng)-->");
                    String returnBook = in.next();
                    for ( int i=0; i<names.length; i++ )
                    {
                        if ( names[i] == null )
                        {
                            //沒(méi)有找到你要的書(shū)
                            System.out.println("這不是我們的書(shū)不用歸還");
                            break;
                        }
                        else if ( names[i].equals( returnBook ) && states[i] == 1 )
                        {
                            System.out.println("請(qǐng)輸入歸還日期:");
                            int date = in.nextInt();
                            while ( date<1 || date>31 || date<dates[i] )
                            {
                                if ( date<1 || date>31 )
                                {
                                    System.out.println("數(shù)字格式不正確,請(qǐng)輸入1-31之間的數(shù)字:");
                                    date = in.nextInt();
                                }
                                else if ( date<dates[i] )
                                {
                                    System.out.println("歸還日期不能小于借閱日期,請(qǐng)重新輸入:");
                                    date = in.nextInt();
                                }
                                
                            }
                            
                            System.out.println("歸還《"+returnBook+"》成功!");
                            states[i] = 0;  //重新設(shè)置借閱狀態(tài)
                            //計(jì)算租金
                            int rent = ( date-dates[i] )*1;
                            System.out.println("應(yīng)付租金(元):"+rent);
                            dates[i] = 0;   //重新設(shè)置借閱日期
                            break;
                        }
                        else if ( names[i].equals( returnBook ) && states[i] == 0 )
                        {
                            //找到了您要借的書(shū),書(shū)的狀態(tài)是已借出的,不可以借
                            System.out.println("該書(shū)尚未借出,無(wú)需歸還!");
                            break;
                        }
                    }
                    break;
                case 5:
                    //刪除圖書(shū)
                    System.out.println("\n刪除圖書(shū)-->");
                    System.out.println("請(qǐng)輸入刪除圖書(shū)名稱(chēng)-->");
                    String deleteBook = in.next();
                    //要?jiǎng)h除圖書(shū)的位置
                    int index = -1;
                    for ( int i=0; i<names.length; i++ )
                    {
                        if ( names[i] == null )
                        {
                            //沒(méi)找到要?jiǎng)h除的書(shū)
                            System.out.println("沒(méi)找到你要?jiǎng)h除的書(shū),刪不了");
                            break;
                        }
                        else if ( names[i].equals(deleteBook) && states[i] == 1 )
                        {
                            //找到了你要?jiǎng)h的書(shū),但是該書(shū)已經(jīng)借出去了
                            System.out.println("該書(shū)已經(jīng)借出去了,無(wú)法刪除!");
                            break;
                        }
                        else if ( names[i].equals(deleteBook) && states[i] == 0 )
                        {
                            //找到了要?jiǎng)h除的書(shū),并且沒(méi)借出去
                            //記錄該書(shū)的位置
                            index = i;
                            break;
                        }
                    }
                    //index可能依然是-1,也可能是一個(gè)正常的位置
                    //根據(jù)圖書(shū)的位置進(jìn)行刪除,后續(xù)的圖書(shū)位置依次往前覆蓋
                    if ( index != -1 ) 
                    {
                        //從index-數(shù)組的最后一本書(shū)一次往前覆蓋
                        for ( int i=index; i<names.length; i++ )
                        {
                            if ( i != names.length-1)
                            {
                                names[i] = names[i+1];
                                counts[i] = counts[i+1];
                                dates[i] = dates[i+1];
                                states[i] = states[i+1];
                            }
                            //將最后一個(gè)元素置空
                            names[names.length-1] = null;
                            counts[names.length-1] = 0;
                            dates[names.length-1] = 0;
                            states[names.length-1] = 0;
                        }
                        System.out.println("刪除成功");
                    }
                    
                    break;
                case 6:
                    //跳出循環(huán)
                    flag = false;
                    break;
                default:
                    //輸入了錯(cuò)誤數(shù)字
                    flag = false;
                    break;
                        
            }
            //如果flag == false,則結(jié)束循環(huán),跳出系統(tǒng)
            if ( !flag )
            {
                //跳出循環(huán)
                break;
            }
            else
            {
                //用戶(hù)不結(jié)束使用系統(tǒng),代表用戶(hù)想返回主菜單繼續(xù)操作
                System.out.println("請(qǐng)輸入0返回");
                num = in.nextInt();
            }
            
        }
        while( num == 0 );
        System.out.println("謝謝,歡迎使用!");
        
    }

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

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