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查看图书");
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借出图书");
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=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实现图书借阅系统
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?

