Java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)借閱系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖書(shū)借閱系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
為圖書(shū)閱覽室開(kāi)發(fā)一個(gè)圖書(shū)借閱系統(tǒng),最多可存50本圖書(shū),實(shí)現(xiàn)圖書(shū)的管理。圖書(shū)借閱系統(tǒng)具備以下主要功能。
u功能
Ø借出排行榜
Ø新增圖書(shū)
Ø查看圖書(shū)
Ø刪除圖書(shū)
Ø借出圖書(shū)
Ø歸還圖書(shū)
Ø退出
package com.daiinfo.seninorjava.ken8.implentment.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Scanner; public class Bookborrowing { ?? ?public static void main(String[] args){ ?? ??? ?int[] states=new int[50];//圖書(shū)借閱狀態(tài) 狀態(tài)0:已借出,1:可借 ?? ??? ?int[] counts=new int[50];//圖書(shū)借閱次數(shù) ?? ??? ?String[] name=new String[50];//圖書(shū)名稱 ?? ??? ?String[] dates=new String[50];//圖書(shū)日期 ?? ??? ?//初始化圖書(shū) ?? ??? ?states[0]=0; ?? ??? ?counts[0]=15; ?? ??? ?name[0]="數(shù)據(jù)結(jié)構(gòu)"; ?? ??? ?dates[0]="2018-7-15"; ?? ??? ? ?? ??? ?states[1]=1; ?? ??? ?counts[1]=12; ?? ??? ?name[1]="數(shù)據(jù)庫(kù)"; ?? ??? ?dates[1]=null; ?? ??? ? ?? ??? ?states[2]=2; ?? ??? ?counts[2]=30; ?? ??? ?name[2]="離散數(shù)學(xué)"; ?? ??? ?dates[2]=null; ?? ??? ?//外觀界面 ?? ??? ?Scanner input=new Scanner(System.in); ?? ??? ?int num=-1;//用戶輸入0返回主菜單 ?? ??? ?boolean flage=false;//記錄用戶是否退出系統(tǒng),true為退出,false為不退出 ?? ??? ?do { ?? ??? ??? ?System.out.println("*************************************"); ?? ??? ??? ?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、退出"); ?? ??? ??? ?int choose=input.nextInt(); ?? ??? ??? ?switch(choose){ ?? ??? ??? ?case 0: ?? ??? ??? ??? ?int number=0; ?? ??? ??? ??? ?for(;name[number]!=null;number++) { ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?}//求出當(dāng)前書(shū)目總數(shù) ?? ??? ??? ??? ?int[] sortBook=new int[number]; ?? ??? ??? ??? ?printBook(name,counts,number,sortBook); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 1: ?? ??? ??? ??? ?System.out.println("------>新增圖書(shū)"); ?? ??? ??? ??? ?int a=0; ?? ??? ??? ??? ?for(;a<name.length;a++) { ?? ??? ??? ??? ??? ?if(name[a]==null) { ?? ??? ??? ??? ??? ??? ?System.out.println("請(qǐng)輸入圖書(shū)名稱"); ?? ??? ??? ??? ??? ??? ?name[a]=input.next();//錄入書(shū)名 ?? ??? ??? ??? ??? ??? ?System.out.println("/n"+"新增《"+name[a]+"》成功?。?!"); ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?//將圖書(shū)信息存入數(shù)組 ?? ??? ??? ??? ??? ??? ?states[a]=1; ?? ??? ??? ??? ??? ??? ?counts[a]=0; ?? ??? ??? ??? ??? ??? ?dates[a]=null; ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(a==50) { ?? ??? ??? ??? ??? ?System.out.println("書(shū)架已滿,勿加"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?System.out.println("******************************"); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?System.out.println("------>查看圖書(shū)"); ?? ??? ??? ??? ?System.out.println("序號(hào)\t狀態(tài)\t名稱\t借出日期\t"); ?? ??? ??? ??? ?for(int i=0;name[i]!=null;i++) { ?? ??? ??? ??? ??? ?String situation=(states[i]==0)?"已借出":"可借"; ?? ??? ??? ??? ??? ?System.out.println((i+1)+"\t"+situation+"\t《"+name[i]+"》\t"); ?? ??? ??? ??? ??? ?if(states[i]==0) { ?? ??? ??? ??? ??? ??? ?System.out.println(dates[i]); ?? ??? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ??? ?System.out.println(); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?System.out.println("*******************************"); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 3: ?? ??? ??? ??? ?System.out.println("------->刪除圖書(shū)"); ?? ??? ??? ? ? ?System.out.println("請(qǐng)輸入圖書(shū)名稱"); ?? ??? ??? ? ? ?String book=input.next(); ?? ??? ??? ? ? ?boolean check1=false;//判斷是否找到刪除圖書(shū)名稱,false找不到 ?? ??? ??? ? ? ?for(int b=0;name[b]!=null;b++) { ?? ??? ??? ? ? ??? ?if(name[b].equals(book)) { ?? ??? ??? ? ? ??? ??? ?check1=true; ?? ??? ??? ? ? ??? ??? ?if(states[b]==1) { ?? ??? ??? ? ? ??? ??? ??? ?//圖書(shū)未借出,可以刪除 ?? ??? ??? ? ? ??? ??? ??? ?System.out.println("刪除《"+book+"》成功!"); ?? ??? ??? ? ? ??? ??? ??? ?int i=b; ?? ??? ??? ? ? ??? ??? ??? ?for(;i<name.length-1;i++) { ?? ??? ??? ? ? ??? ??? ??? ??? ?states[i]=states[i+1]; ?? ??? ??? ? ? ??? ??? ??? ??? ?name[i]=name[i+1]; ?? ??? ??? ? ? ??? ??? ??? ??? ?dates[i]=dates[i+1]; ?? ??? ??? ? ? ??? ??? ??? ??? ?counts[i]=counts[i+1]; ?? ??? ??? ? ? ??? ??? ??? ?}//將數(shù)組內(nèi)容前移 ?? ??? ??? ? ? ??? ??? ??? ?break; ?? ??? ??? ? ? ??? ??? ?}else { ?? ??? ??? ? ? ??? ??? ??? ?System.out.println("圖書(shū)已借出,無(wú)法刪除!"); ?? ??? ??? ? ? ??? ??? ??? ?break; ?? ??? ??? ? ? ??? ??? ?} ?? ??? ??? ? ? ??? ?} ?? ??? ??? ? ? ?} ?? ??? ??? ? ? ?if(check1==false) { ?? ??? ??? ? ? ??? ?System.out.println("沒(méi)有找到匹配信息!"); ?? ??? ??? ? ? ?} ?? ??? ??? ? ? ?System.out.println("*************************"); ?? ??? ??? ? ? ?break; ?? ??? ??? ?case 4: ?? ??? ??? ??? ?System.out.println("--------->借出圖書(shū)"); ?? ??? ??? ??? ?System.out.println("請(qǐng)輸入圖書(shū)名稱:"); ?? ??? ??? ??? ?String back=input.next(); ?? ??? ??? ??? ?boolean check2=false;//判斷想要借出的書(shū)能否找到,false找不到,true找到 ?? ??? ??? ??? ?for(int b=0;name[b]!=null;b++) { ?? ? ? ? ? ? ? ? ? if(name[b].equals(back)) {//書(shū)存在 ?? ? ? ? ? ? ? ??? ? ? check2=true; ?? ? ? ? ? ? ? ??? ? ? if(states[b]==1) { ?? ? ? ? ? ? ? ??? ??? ? ? System.out.println("請(qǐng)輸入借出日期(年-月-日):"); ?? ? ? ? ? ? ? ??? ??? ? ? dates[b]=input.next(); ?? ? ? ? ? ? ? ??? ??? ? ? while(judge(dates[b])==false) { ?? ? ? ? ? ? ? ??? ??? ??? ? ? System.out.println("日期非法,請(qǐng)重新輸入"); ?? ? ? ? ? ? ? ??? ??? ??? ? ? dates[b]=input.next(); ?? ? ? ? ? ? ? ??? ??? ? ? } ?? ? ? ? ? ? ? ??? ??? ? ? states[b]=0;//將當(dāng)前圖書(shū)狀態(tài)調(diào)成借出 ?? ? ? ? ? ? ? ??? ??? ? ? counts[b]++;//當(dāng)前圖書(shū)借出次數(shù)加一 ?? ? ? ? ? ? ? ??? ? ? }else { ?? ? ? ? ? ? ? ??? ??? ? ? System.out.println(name[b]+"已被借出!"); ?? ? ? ? ? ? ? ??? ? ? } ?? ? ? ? ? ? ? ??? ? ? break; ?? ? ? ? ? ? ? ? ? } ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(check2==false) { ?? ??? ??? ??? ??? ?System.out.println("沒(méi)有找到匹配信息!"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?System.out.println("*********************************"); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 5: ?? ??? ??? ??? ?System.out.println("--------->歸還圖書(shū)"); ?? ??? ??? ? ? ?System.out.println("請(qǐng)輸入圖書(shū)名稱:"); ?? ??? ??? ? ? ?String back1=input.next(); ?? ??? ??? ? ? ?boolean check3=false;//判斷歸還的書(shū)能否找到,false找不到,true找到 ?? ??? ??? ? ? ?for(int b=0;name[b]!=null;b++) { ?? ??? ? ? ? ? ? ? ? ? if(name[b].equals(back1)) {//書(shū)存在 ?? ??? ? ? ? ? ? ? ??? ? ? check3=true; ?? ??? ? ? ? ? ? ? ??? ? ? if(states[b]==0) {//如果書(shū)借出 ?? ??? ? ? ? ? ? ? ??? ??? ? ? System.out.println("請(qǐng)輸入歸還日期(年-月-日):"); ?? ??? ? ? ? ? ? ? ??? ??? ? ? String returnDate=input.next(); ?? ??? ? ? ? ? ? ? ??? ??? ? ? while(judge(returnDate)==false) { ?? ??? ? ? ? ? ? ? ??? ??? ??? ? ? System.out.println("日期非法,請(qǐng)重新輸入"); ?? ??? ? ? ? ? ? ? ??? ??? ??? ? ? returnDate=input.next(); ?? ??? ? ? ? ? ? ? ??? ??? ? ? } ?? ??? ? ? ? ? ? ? ??? ??? ? ? System.out.println("歸還《"+back1+"》成功!"); ?? ??? ? ? ? ? ? ? ??? ??? ? ? System.out.println("借出日期"+dates[b]); ?? ??? ? ? ? ? ? ? ??? ??? ? ? System.out.println("歸還日期"+returnDate); ?? ??? ? ? ? ? ? ? ??? ??? ? ? int money=0; ?? ??? ? ? ? ? ? ? ??? ??? ? ? try { ?? ??? ? ? ? ? ? ? ??? ??? ??? ? ? money=daysBetween(dates[b],returnDate); ?? ??? ? ? ? ? ? ? ??? ??? ??? ? ? }catch(Exception e) { ?? ??? ? ? ? ? ? ? ??? ??? ??? ??? ? ? e.printStackTrace(); ?? ??? ? ? ? ? ? ? ??? ??? ??? ? ? } ?? ??? ? ? ? ? ? ? ??? ??? ? ? System.out.println("該書(shū)沒(méi)有被借出,無(wú)法執(zhí)行操作"); ?? ??? ? ? ? ? ? ? ??? ??? ? ? } ?? ??? ? ? ? ? ? ? ??? ? ? break; ?? ??? ? ? ? ? ? ? ??? ? ? } ?? ??? ? ? ? ? ? ? ? ? } ?? ??? ??? ? ? ?if(check3==false) { ?? ??? ??? ??? ??? ?System.out.println("沒(méi)有找到匹配信息!"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?System.out.println("*********************************"); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 6: ?? ??? ??? ??? ?flage=true; ?? ??? ??? ??? ?break; ?? ??? ??? ??? ?default: ?? ??? ??? ??? ?flage=true; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?if(flage==true) { ?? ??? ??? ??? ?break; ?? ??? ??? ?}else { ?? ??? ??? ??? ?System.out.println("輸入0返回"); ?? ??? ??? ??? ?num=input.nextInt(); ?? ??? ??? ?} ?? ??? ?}while(num==0); ?? ??? ?System.out.println("謝謝使用!"); ?? ?} ?? ?private static boolean judge(String str) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?SimpleDateFormat sd=new SimpleDateFormat("yy-MM-dd");//日期格式 ?? ??? ?try { ?? ??? ??? ?sd.setLenient(false);//指定日期時(shí)間是否合格,true不合格,false合格 ?? ??? ??? ?sd.parse(str); ?? ??? ?}catch(Exception e){ ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?return true; ?? ?} ?? ?public static void printBook(String[] names,int[] sortBook,int number,int[] counts) { ?? ??? ?int[] another=counts.clone();//復(fù)制數(shù)組 ?? ??? ?int i=0; ?? ??? ?int max=another[0]; ?? ??? ?for(int p=0;p<=max;p++) { ?? ??? ??? ?for(int q=0;q<number;q++) { ?? ??? ??? ??? ?if(counts[q]==p) { ?? ??? ??? ??? ??? ?sortBook[i++]=q; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("序號(hào)\t"+"書(shū)名\t"+"借出次數(shù)"); ?? ??? ?for(int x=(number-1);x>=0;x--) {//借出次數(shù)排行榜 ?? ??? ??? ?System.out.println((number-x)+"\t"+names[sortBook[x]]+"\t\t"+counts[sortBook[x]]); ?? ??? ?} ?? ??? ?System.out.println("******************"); ?? ?} ?? ?public static int daysBetween(String smdate,String bdate) throws Exception{ ?? ??? ?SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd"); ?? ??? ?Calendar cal=Calendar.getInstance(); ?? ??? ?cal.setTime(sdf.parse(smdate)); ?? ??? ?long time1=cal.getTimeInMillis(); ?? ??? ?cal.setTime(sdf.parse(bdate)); ?? ??? ?long time2=cal.getTimeInMillis(); ?? ??? ?long between_days=(time2-time1)/(1000*3600*24); ?? ??? ?return Integer.parseInt(String.valueOf(between_days)); ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java類(lèi)的繼承實(shí)例詳解(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
在Java開(kāi)發(fā)中,我們常常用到繼承這一概念,可以說(shuō)繼承是Java這類(lèi)面向?qū)ο缶幊陶Z(yǔ)言的基石,今天小編一起和大家一起學(xué)習(xí)java類(lèi)的繼承2017-04-04Java實(shí)現(xiàn)簡(jiǎn)單雙色球搖獎(jiǎng)功能過(guò)程解析
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單雙色球搖獎(jiǎng)功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢
本文主要介紹了MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11Java開(kāi)啟新線程并傳參方法代碼實(shí)現(xiàn)
這篇文章主要介紹了Java開(kāi)啟新線程并傳參方法代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java黑盒測(cè)試之nextDate函數(shù)測(cè)試
這篇文章主要介紹了Java黑盒測(cè)試之nextDate函數(shù)測(cè)試,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java黑盒測(cè)試的小伙伴們有很大的幫助哦,需要的朋友可以參考下2021-05-05