本文实例为大家分享了Java实现图书借阅系统的具体代码,供大家参考,具体内容如下
为图书阅览室开发一个图书借阅系统,最多可存50本图书,实现图书的管理。图书借阅系统具备以下主要功能。
u功能
Ø借出排行榜
Ø新增图书
Ø查看图书
Ø删除图书
Ø借出图书
Ø归还图书
Ø退出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
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