Java实现简单图书借阅系统

作者:易只锦小年 时间:2023-11-22 01:09:02 

本文实例为大家分享了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));
    }

}

来源:https://blog.csdn.net/qq_45924868/article/details/108618491

标签:java,借阅系统
0
投稿

猜你喜欢

  • springboot整合security和vue的实践

    2021-09-17 20:39:28
  • FeignClient如何通过配置变量调用配置文件url

    2023-05-07 08:19:25
  • java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决

    2022-06-14 23:21:51
  • Java模拟QQ桌面截图功能实现方法

    2021-09-19 16:30:02
  • 深入理解java动态代理的两种实现方式(JDK/Cglib)

    2023-11-26 13:29:52
  • Android中多个ContentProvider的初始化顺序详解

    2021-11-06 03:52:36
  • Java 回调函数深入理解

    2023-11-01 17:32:04
  • 解决异常FileNotFoundException:class path resource找不到资源文件的问题

    2021-12-26 18:24:14
  • Java多线程实现简易微信发红包的方法实例

    2023-04-16 11:46:15
  • 基于Hadoop实现Knn算法

    2023-11-27 04:01:20
  • Java算法设计与分析分治算法

    2022-04-02 08:07:15
  • SpringCloud Gateway 路由配置定位原理分析

    2022-06-10 19:57:47
  • java类中生成jfreechart,返回图表的url地址 代码分享

    2023-09-08 00:54:07
  • Seata AT模式TransactionHook被删除探究

    2022-01-12 14:56:49
  • Java状态设计模式实现对象状态转换的优雅方式

    2023-11-20 10:33:59
  • 详解springboot之jackson的两种配置方式

    2021-11-03 11:01:21
  • Android实现记住账号密码功能

    2021-10-02 01:51:24
  • Java Hutool工具实现验证码生成及Excel文件的导入和导出

    2023-02-04 22:49:32
  • Android 定时器实现图片的变换

    2021-05-28 00:33:13
  • Android图片添加水印图片并把图片保存到文件存储的实现代码

    2022-06-03 21:48:54
  • asp之家 软件编程 m.aspxhome.com