温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何实现简单图书借阅系统

发布时间:2022-03-11 09:51:32 来源:亿速云 阅读:203 作者:iii 栏目:开发技术

这篇文章主要介绍“Java如何实现简单图书借阅系统”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java如何实现简单图书借阅系统”文章能帮助大家解决问题。

为图书阅览室开发一个图书借阅系统,最多可存50本图书,实现图书的管理。图书借阅系统具备以下主要功能。

u功能

借出排行榜

新增图书

查看图书

删除图书

借出图书

归还图书

退出

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];//图书借阅状态 状态0:已借出,1:可借
        int[] counts=new int[50];//图书借阅次数
        String[] name=new String[50];//图书名称
        String[] dates=new String[50];//图书日期
        //初始化图书
        states[0]=0;
        counts[0]=15;
        name[0]="数据结构";
        dates[0]="2018-7-15";
        
        states[1]=1;
        counts[1]=12;
        name[1]="数据库";
        dates[1]=null;
        
        states[2]=2;
        counts[2]=30;
        name[2]="离散数学";
        dates[2]=null;
        //外观界面
        Scanner input=new Scanner(System.in);
        int num=-1;//用户输入0返回主菜单
        boolean flage=false;//记录用户是否退出系统,true为退出,false为不退出
        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、退出");
            int choose=input.nextInt();
            switch(choose){
            case 0:
                int number=0;
                for(;name[number]!=null;number++) {
                    
                }//求出当前书目总数
                int[] sortBook=new int[number];
                printBook(name,counts,number,sortBook);
                break;
            case 1:
                System.out.println("------>新增图书");
                int a=0;
                for(;a<name.length;a++) {
                    if(name[a]==null) {
                        System.out.println("请输入图书名称");
                        name[a]=input.next();//录入书名
                        System.out.println("/n"+"新增《"+name[a]+"》成功!!!");
                        
                        //将图书信息存入数组
                        states[a]=1;
                        counts[a]=0;
                        dates[a]=null;
                        break;
                    }
                }
                if(a==50) {
                    System.out.println("书架已满,勿加");
                }
                System.out.println("******************************");
                break;
            case 2:
                System.out.println("------>查看图书");
                System.out.println("序号\t状态\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("------->删除图书");
                System.out.println("请输入图书名称");
                String book=input.next();
                boolean check1=false;//判断是否找到删除图书名称,false找不到
                for(int b=0;name[b]!=null;b++) {
                    if(name[b].equals(book)) {
                        check1=true;
                        if(states[b]==1) {
                            //图书未借出,可以删除
                            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];
                            }//将数组内容前移
                            break;
                        }else {
                            System.out.println("图书已借出,无法删除!");
                            break;
                        }
                    }
                }
                if(check1==false) {
                    System.out.println("没有找到匹配信息!");
                }
                System.out.println("*************************");
                break;
            case 4:
                System.out.println("--------->借出图书");
                System.out.println("请输入图书名称:");
                String back=input.next();
                boolean check2=false;//判断想要借出的书能否找到,false找不到,true找到
                for(int b=0;name[b]!=null;b++) {
                   if(name[b].equals(back)) {//书存在
                       check2=true;
                       if(states[b]==1) {
                           System.out.println("请输入借出日期(年-月-日):");
                           dates[b]=input.next();
                           while(judge(dates[b])==false) {
                               System.out.println("日期非法,请重新输入");
                               dates[b]=input.next();
                           }
                           states[b]=0;//将当前图书状态调成借出
                           counts[b]++;//当前图书借出次数加一
                       }else {
                           System.out.println(name[b]+"已被借出!");
                       }
                       break;
                   }
                }
                if(check2==false) {
                    System.out.println("没有找到匹配信息!");
                }
                System.out.println("*********************************");
                break;
            case 5:
                System.out.println("--------->归还图书");
                System.out.println("请输入图书名称:");
                String back1=input.next();
                boolean check3=false;//判断归还的书能否找到,false找不到,true找到
                for(int b=0;name[b]!=null;b++) {
                       if(name[b].equals(back1)) {//书存在
                           check3=true;
                           if(states[b]==0) {//如果书借出
                               System.out.println("请输入归还日期(年-月-日):");
                               String returnDate=input.next();
                               while(judge(returnDate)==false) {
                                   System.out.println("日期非法,请重新输入");
                                   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("该书没有被借出,无法执行操作");
                               }
                           break;
                           }
                       }
                if(check3==false) {
                    System.out.println("没有找到匹配信息!");
                }
                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);//指定日期时间是否合格,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();//复制数组
        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("序号\t"+"书名\t"+"借出次数");
        for(int x=(number-1);x>=0;x--) {//借出次数排行榜
            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));
    }

}

关于“Java如何实现简单图书借阅系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI