java 图书馆初级编写

import java.util.Scanner;
import java.util.Arrays;
public class book {
    public static void main(String[] args){
        //初始化数据
        String[] bookName = {"西游记", "红楼梦", "三国演义", "水浒传","海贼王",null};
        String[] state = {"可借","可借","可借","可借","可借","可借"};
        int[] count = {0,0,0,0,0,0};
        int num;
        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 = new Scanner(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;
                        }
                    }

             //判断该书的状态
             if(state[index].equals("已借出")) {
                System.out.println("------该图书已借出------");
             }else{
                state[index] = "已借出";
                //将图书对应的借出数量加
                count[index] = ++count[index];
                System.out.println("该书已借阅成功");
             }

                }else{
                    System.out.println("你输入的书名不存在");
                }
            }else{
                System.out.println("书架已空,没有可以借阅的书籍");
            }
            
            break;
        case 5:
            System.out.println("----》归还图书");
            //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;
                        }
                    }
                    if(state[index].equals("可借")) {
                        System.out.println("该书已归还");
                    }else{
                        state[index] = "可借";
                        System.out.println("该书已归还");
                    }
                }else{
                    System.out.println("该书不存在");
                }
            break;
        case 6:
            System.out.println("退出系统");
                return;
        }
        System.out.println("输入0返回");
        num = scanner.nextInt();
        System.out.println(num);
        int bb = 0;
        while(num !=0) {
            System.out.println("----------输入错误请继续输入-----------");
            System.out.println("\t\t\t需要输入0才能返回");
            num = scanner.nextInt();
            if(bb >= 3){
                System.out.print("再输入错误就要打屁屁了");
            }
            bb++;
        }
       }while(num == 0 );
    }
}

 

posted @ 2019-08-02 11:37  林中有风  阅读(630)  评论(0编辑  收藏  举报