importjava.util.Scanner;importjava.util.Arrays;public classbook {public static voidmain(String[] args){//初始化数据
String[] bookName = {"西游记", "红楼梦", "三国演义", "水浒传","海贼王",null};
String[] state= {"可借","可借","可借","可借","可借","可借"};int[] count = {0,0,0,0,0,0};intnum;do{
System.out.println("欢迎使用图书借阅系统");
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.退出图书");
System.out.println("-------------------------------");
System.out.println("请选择:");
Scanner scanner= newScanner(System.in);
num=scanner.nextInt();//打印输入的值//System.out.println(num);
switch(num) {case 1:
System.out.println("---》查看图书");
System.out.println("序号\t\t名称\t\t状态\t\t借出次数");//int i = 1;
for(int i = 1; i<=bookName.length; i++) {
System.out.print(i);
System.out.print("\t\t"+bookName[i-1]);
System.out.print("\t\t"+state[i-1]);
System.out.println("\t\t"+count[i-1]);
}/*for(String name:bookName) {
System.out.print(i);
System.out.print("\t\t"+name);
System.out.print("\t\t"+state[i-1]);
System.out.println("\t\t"+count[i-1]);
i++;
}*/
break;case 2:
System.out.println("---》新增图书");//判断数组中是否有位置增加书名
if(bookName[bookName.length-1] == null) {
System.out.println("请输入书名:");
String name=scanner.next();//检测数组中是否已经存在该书名//int index = Arrays.binarySearch(bookName, name);//默认数组中没有用户输入的书
boolean flag = false;for(String name2:bookName) {if(name.equals(name2)){
flag= true;break;
}
}if(flag) {
System.out.println("《"+name+"》"+"书名已经存在");break;
}else{for(int i = 0; i < bookName.length; i++) {if(bookName[i] == null) {
bookName[i]=name;break;
}
}
System.out.println("已添加该书名");
}
}else{
System.out.println("-----书架已满-----");
}break;case 3:
System.out.println("----》删除图书");//遍历图书
for(int i = 1; i<=bookName.length; i++) {
System.out.print(i);
System.out.println("\t"+bookName[i-1]);
}//检测书架是否为空
boolean flag = false;for(String name2:bookName) {if(name2 != null){
flag= true;break;
}
}if(flag) {
System.out.println("-------请输入你要删除的书名------");
String name=scanner.next();boolean flag2 = false; //证明该书是否存在
for(String name2:bookName) {if(name.equals(name2)) {
flag2= true;break;
}
}if(flag2) {//找到被删除的书的数组下标
int index = 0;for(int i = 0; i< bookName.length; i++) {if(name.equals(bookName[i])) {
index=i;break;
}
}//index之后的数据往前提
for(int i = index; i < bookName.length-1; i++) {
bookName[i]= bookName[i+1];
}//将数组最后一位置为null
bookName[bookName.length-1] = null;
System.out.println("删除成功");
}else{
System.out.println("该书不存在");
}
}else{
System.out.println("-------空书架-------");
}//for(int i = 1; i<=bookName.length; i++) {//System.out.print(i);//System.out.println("\t"+bookName[i-1]);//}//num = scanner.nextInt();//bookName[num-1] =null;
break;case 4:
System.out.println("借阅图书");//1.检测bookName是否为空
flag = false;for(String name:bookName) {if(name != null) {
flag= true;break;
}
}if(flag) {//2.检测用户输入的书是否存在
System.out.print("请输入你要借阅的书名:");
String name=scanner.next();boolean flag2 = false;for(String name2:bookName) {if(name.equals(name2)) {
flag2= true;break;
}
}if(flag2) {//将图书对应的状态改为已借
int index = 0;for(int i = 0; i < bookName.length; i++) {if(name.equals(bookName[i])) {
index=i;break;
}
}