```
//第一部分
package tushuguananli;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TuShu {
// 状态 名称 借出时间
public String state;
public String name;
public Date date;
public void setTuShu(String zhuangtai , String mingzi, Date riqi){
state=zhuangtai;
name=mingzi;
date=riqi;
}
public void print( int index){
System.out.println(index+"\t"+name+"\t"+state+"\t"+date);
}
}
//第二部分
package tushuguananli;
public class TuShuSet {
TuShu[] tushus=new TuShu[30];
}
//第三部分
package tushuguananli;
import sun.util.calendar.BaseCalendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class TuShuManage {
TuShuSet tushuset=new TuShuSet();
//仓库初始化 放入商品
public void initila(){
TuShu tushu1=new TuShu();
tushu1.setTuShu( "已借出" ,"<<java基础教程>>", new Date());
TuShu tushu2=new TuShu();
tushu2.setTuShu("可借", "<<数据库技术>>", null);
TuShu tushu3=new TuShu();
tushu3.setTuShu("可借", "<<人与神话>>" ,null);
tushuset.tushus[0]=tushu1;
tushuset.tushus[1]=tushu2;
tushuset.tushus[2]=tushu3;
}
//菜单
public void startMenu() throws ParseException {
System.out.println("欢迎使用图书管理系统");
System.out.println("————————————————————————————");
boolean flag=true;
do{
System.out.println("1.新增图书");
System.out.println("2.查看图书");
System.out.println("3.删除图书");
System.out.println("4.借出图书");
System.out.println("5.归还图书");
System.out.println("6.退出");
System.out.println("请选择:");
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
switch (num){
case 1:
System.out.println("--->新增图书");
xinzeng();
break;
case 2:
System.out.println("--->查看图书");
chakan();
break;
case 3:
System.out.println("--->删除图书");
shanchu();
break;
case 4:
System.out.println("---借出图书");
jiechu();
break;
case 5:
System.out.println("--->归还图书");
guihuan();
break;
case 6:
System.out.println("--->退出");
flag=false;
break;
default:
System.out.println("请再次输入");
}
}while(flag);
}
public void xinzeng(){
System.out.println("请输入图书名称");
Scanner scanner=new Scanner(System.in);
String mingzi1=scanner.next();
System.out.println("新增"+mingzi1+"成功");
TuShu tuShu4=new TuShu();
tuShu4.setTuShu("可借",mingzi1,null);
for (int i = 0; i < tushuset.tushus.length; i++) {
if (tushuset.tushus[i] == null) {
tushuset.tushus[i] = tuShu4; // 把新建的对象放在数组中的第一个空位置
break; // 后续的空位置直接跳过
}
}
}
public void chakan(){
System.out.println("序号"+"\t"+"状态"+"\t"+"名称"+"\t"+"借出日期");
for(int i=0;i<tushuset.tushus.length;i++){
if(tushuset.tushus[i]!=null){
tushuset.tushus[i].print(i+1);
}
}
}
public void shanchu(){
System.out.println("请输入图书名称");
Scanner scanner=new Scanner(System.in);
String tushu5=scanner.next();
for(int i=0; i<tushuset.tushus.length;i++){
if(tushuset.tushus[i]!=null&&tushuset.tushus[i].name==tushu5&&
tushuset.tushus[i].state=="可借"){
int j=i;
while ( tushuset.tushus[j+1]!=null){
tushuset.tushus[j]=tushuset.tushus[j+1];
j++;
}
tushuset.tushus[j]=null;
System.out.println("删除"+tushu5+"成功");
break;
}
else {
System.out.println(tushu5 + "为借出状态,不能删除");
}
}
}
public void jiechu() throws ParseException {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入借出书名称:");
String tushu6=scanner.next();
System.out.println("请输借出日期(年-月-日):");
String time = scanner.next();
DateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
Date date1=format.parse(time);
for(int i=0; i<tushuset.tushus.length;i++){
if (tushuset.tushus[i] != null && tushuset.tushus[i].name==tushu6&&
tushuset.tushus[i].state=="可借"){
tushuset.tushus[i].state="已借出";
tushuset.tushus[i].date=date1;
System.out.println("借出"+tushu6+"成功");
}
}
}
public void guihuan() throws ParseException {
int num=0;
Scanner scanner=new Scanner(System.in);
System.out.println("请输入图书名称");
String tushu7= scanner.next();
System.out.println("请输入归还日期(年-月-日)");
String time2=scanner.next();
DateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
Date date2=format.parse(time2);
for (int i=0;i<tushuset.tushus.length;i++){
if(tushuset.tushus[i].name==tushu7&&tushuset.tushus[i]!=null&&
tushuset.tushus[i].state=="已借出"){
System.out.println("借出时间"+tushuset.tushus[i].date);
System.out.println("归还时间"+date2);
long alnteger=((tushuset.tushus[i].date.getTime()-date2.getTime())/(24*60*60*1000));
num=(int)alnteger;
System.out.println("需要支付"+num+"元");
System.out.println("归还"+tushu7+"成功");
tushuset.tushus[i].state="可借";
tushuset.tushus[i].date=null;
}
}
}
}
//第四部分
package tushuguananli;
import java.text.ParseException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws ParseException {
TuShuManage thshumanage=new TuShuManage();
thshumanage.initila();
thshumanage.startMenu();
}
}
```