图书管理系统

// 图书管理类
public class BookManage {
    BookSet bookSet = new BookSet();

    /**
     * 初始化
     */
    public void initial() {
        book java = new book();
        java.setBook("java",1,2021/4/15",20);
        book renyue = new book();
        renyue.name = "程序语言基础";
        renyue.setBook(."程序语言基础",1 ,null,0);
        book jichu = new book();
        jichu.setBook("计算机基础",1,null,0);
        bookSet.books[0] = java ;
        bookSet.books[1] = renyue ;
        bookSet.books[2] = jichu ;
    }

/*
菜单切换
 */
    public void startMenu(){
        boolean flag = true ;
        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 退出");
            System.out.println("*********************");
            System.out.println("请选择");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt() ;
            switch (choice){
                case 1:
                    System.out.println("新增图书");
                    add();
                    break;
                case 2:
                    System.out.println("查看图书");
                    chakan();
                    break;
                case 3:
                    System.out.println("删除图书");
                    shan();
                    break;
                case 4:
                    System.out.println("借出图书");
                    jie();
                    break;
                case 5:
                    System.out.println("归还图书");
                    huan();
                    break;
                case 6:
                    System.out.println("退出,谢谢使用");
                default:
                    System.out.println("输入不符合要求请重新选择");
            }
        }while (flag) ;
    }

/**
 * 查看图书信息
 */
    public void chakan(){
        System.out.println("编号 \t\t\t  名称 \t\t\t 状态 \t\t\t 借出日期 \t\t\t 库存");
        for (int i = 0 ; i < this.bookSet.books.length ; i ++){
            if ( this.bookSet.books[i]!=null){
                this.bookSet.books[i].dayin(i+1);

                }
            }
        }

    /**
     * 添加图书
     */
        public void add(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入图书的名称:");
            String name = scanner.next();
            System.out.println("请输入库存:");
            int number = scanner.nextInt() ;
            book newBook = new book();
            newBook.setBook(name , newBook.state, newBook.date, newBook.number);

            for (int i = 0 ; i < bookSet.books.length ; i++){
                if (bookSet.books[i] == null){
                    bookSet.books[i] = newBook ;  //把新建的对象放在数组中的第一个空位置
                    bookSet.books[i].number = number ; //库存
                    System.out.println("添加成功");
                    break; //后续的空位置直接跳过
                }
            }
        }
    /**
     * 归还
     */
        public void huan(){
            System.out.println("请输入要归还书的名称");
            Scanner scanner = new Scanner(System.in);
            String huan = scanner.next();
            boolean flag = true ;  //判断是否归还成功
            System.out.println("请输入归还日期");
            String riqi = scanner.next() ;
            for (int i = 0 ; i < bookSet.books.length ; i ++){
                if ( bookSet.books[i].name.equals(huan) &&bookSet.books[i].state == 0 ) { // 在书的状态为一借出的状态下可以归还
                    bookSet.books[i].state = 1 ;
                    bookSet.books[i].date = riqi ;

                }else {
                    System.out.println("归还成功");
                    flag = true ;
                    break;
                }
            }
        }
/**
 * 借书
 */

    public void jie(){
        System.out.println("请输入要借书的名称");
        Scanner scanner = new Scanner(System.in);
        String jie = scanner.next();
        boolean flag = true ;
        System.out.println("请输入借书的日期");
        String riqi = scanner.next() ;
        for (int i = 0 ; i < bookSet.books.length ; i ++){
            if (bookSet.books[i] != null && bookSet.books[i].name.equals(jie)){
                if (bookSet.books[i].state == 1){
                    bookSet.books[i].state = 0 ;
                    flag = true ;
                    bookSet.books[i].date=riqi;
                }
                break;
            }

            }



         if (flag){
             System.out.println("借出成功");
        }else {
             System.out.println("借出失败");
         }

    }
/**
 * 删除
 */
    public void shan(){
        System.out.println("请输入删除图书的编号:");
        Scanner scanner = new Scanner(System.in);
        int bianhao = scanner.nextInt();
        boolean flag = true ;
        for (int i = 0 ; i< bookSet.books.length ; i ++){
            if (bookSet.books[i] != null &&(i+1) == bianhao){
                if (bookSet.books[i].state != 0) {
                    int j = i;
                    while (bookSet.books[j + 1] != null) {
                        //后面元素向前移动
                        bookSet.books[j] = bookSet.books[j + 1];
                        j++;
                    }
                    bookSet.books[j] = null;
                    flag = true;
                    break;  //后面元素没必要执行
                }else {
                    System.out.println("图书为借出状态不能删除");
                    flag= true ;
                }
            }else {
                flag = false ;
            }

        }
        if (flag){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 13,585评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 9,750评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,789评论 2 7