SpringBoot控制台版图书借阅程序
时间:2019-06-12
本文章向大家介绍SpringBoot控制台版图书借阅程序,主要包括SpringBoot控制台版图书借阅程序使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
// 虽然代码野鸡,但是用于提交作业却完全足够。
效果图:
完整程序:https://pan.baidu.com/s/1-d1J90dkEtM0WKkABu0K0Q
提取码:hcnm
DAO层代码由MyBatis Generator生成,仅补充若干自定义代码。主要的代码只有下面这个:
package com.book; import com.book.dao.BookMapper; import com.book.dao.StudentMapper; import com.book.dao.lendingRecordMapper; import com.book.model.Book; import com.book.model.Student; import com.book.model.lendingRecord; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Scanner; @SpringBootApplication @MapperScan("com.book.dao") public class MyApplication implements CommandLineRunner { @Autowired private BookMapper bookMapper; @Autowired private StudentMapper studentMapper; @Autowired private lendingRecordMapper lendingRecordMapper; public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... strings) throws Exception { serve(); } private void serve() { Scanner scan = new Scanner(System.in); int command; while (true) { 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、退出程序"); command = scan.nextInt(); if (command == 1) { System.out.println("请问你下一步要干嘛?"); System.out.println("1、增书"); System.out.println("2、改书"); System.out.println("3、删书"); System.out.println("4、查书"); System.out.println("5、浏览书"); command = scan.nextInt(); if (command == 1) { System.out.println("1、请你依次输入书的书名、作者、出版社、ISBN号、数量"); System.out.println("注意一行一个:"); scan.nextLine(); String name = scan.nextLine(); System.out.println("书名" + name + "已录入。"); String author = scan.nextLine(); String press = scan.nextLine(); String isbn = scan.nextLine(); int num = scan.nextInt(); Book book = new Book(name, author, press, isbn, num); bookMapper.insert(book); System.out.println(book + "已录入!!!"); } else if (command == 2) { System.out.println("想改书?请填入书的id以及欲改字段"); System.out.print("书的id:"); long id = scan.nextLong(); System.out.print("书名:"); String name = scan.next(); System.out.print("作者:"); String author = scan.next(); System.out.print("出版社:"); String press = scan.next(); System.out.print("isbn:"); String isbn = scan.next(); System.out.print("数量:"); int num = scan.nextInt(); Book book = new Book(name, author, press, isbn, num); book.setId(id); System.out.println(book + "修改成功!!!"); // bookMapper.updateByPrimaryKeySelective(); } else if (command == 3) { System.out.println("你想删书?请直接输入书的id:"); long id = scan.nextLong(); System.out.println("恭喜你,删除成功!!!"); } else if (command == 4) { System.out.println("你想查书?请直接输入书的id:"); long id = scan.nextLong(); Book book = bookMapper.selectByPrimaryKey(id); if (book == null) { System.out.println("不好意思没有这本书"); } else { System.out.println("找到这本书了,信息如下:"); System.out.println(book); } } else if (command == 5) { List<Book> list = bookMapper.selectAll(); for (Book x : list) { System.out.println(x); } } } else if (command == 2) { System.out.println("请问你下一步要干嘛?"); System.out.println("1、增用户"); System.out.println("2、改用户"); System.out.println("3、删用户"); System.out.println("4、查用户"); System.out.println("5、浏览用户"); command = scan.nextInt(); if (command == 1) { System.out.println("1、请你依次输入用户的身份证号码、姓名、出生年月、性别"); System.out.println("注意一行一个:"); scan.nextLine(); String idCard = scan.nextLine(); System.out.println("idCard" + idCard + "已录入。"); String name = scan.nextLine(); String birth = scan.nextLine(); String sex = scan.nextLine(); Student student = new Student(name, sex, idCard, birth); studentMapper.insert(student); System.out.println(student + "已录入!!!"); } else if (command == 2) { System.out.println("想改用户?请填入用户的id以及欲改字段"); System.out.print("用户的id:"); long id = scan.nextLong(); System.out.print("身份证号码:"); String idCard = scan.next(); System.out.print("姓名:"); String name = scan.next(); System.out.print("性别:"); String sex = scan.next(); System.out.print("出生年月:"); String birth = scan.next(); Student student = new Student(name, sex, idCard, birth); student.setId(id); System.out.println(student + "修改成功!!!"); // bookMapper.updateByPrimaryKeySelective(); } else if (command == 3) { System.out.println("你想删用户?请直接输入用户的id:"); long id = scan.nextLong(); System.out.println("恭喜你,删除成功!!!"); } else if (command == 4) { System.out.println("你想查用户?请直接输入用户的id:"); long id = scan.nextLong(); Student student = studentMapper.selectByPrimaryKey(id); if (student == null) { System.out.println("不好意思没有这个用户"); } else { System.out.println("找到这个用户了,信息如下:"); System.out.println(student); } } else if (command == 5) { List<Student> list = studentMapper.selectAll(); for (Student x : list) { System.out.println(x); } } } else if (command == 3) { System.out.print("读者id:"); Long uid = scan.nextLong(); System.out.print("书的id:"); Long bookId = scan.nextLong(); System.out.print("借书本数:"); int num = scan.nextInt(); borrowBooks(uid, bookId, num); } else if (command == 4) { System.out.print("读者id:"); Long uid = scan.nextLong(); System.out.print("书的id:"); Long bookId = scan.nextLong(); returnBooks(uid, bookId); } else if (command == 5) { break; } } } @Transactional private void borrowBooks(Long uid, Long bookId, int num) { Student student = studentMapper.selectByPrimaryKey(uid); if (student == null) { System.out.println("读者身份有错!!!请检查一遍再重新输入"); return; } Book book = bookMapper.selectByPrimaryKey(bookId); if (book == null || book.getNum() < num) { System.out.println("书籍不存在或者数量不够!!!"); return; } book.setNum(book.getNum() - num); bookMapper.updateByPrimaryKey(book); System.out.println("书籍数量已更新"); lendingRecord record = lendingRecordMapper.selectByUidAndBookId(uid, bookId); if (record != null) { // 如果借过同类的书,那就更新记录 record.setNum(record.getNum() + num); lendingRecordMapper.updateByPrimaryKey(record); } else { // 如果没有,创建新记录 record = new lendingRecord(uid, bookId, num); lendingRecordMapper.insert(record); } System.out.println("恭喜你!借书成功咯!要提醒读者按时还书哦!"); } @Transactional private void returnBooks(Long uid, Long bookId) { // 可能出现Expected one result (or null) to be returned by selectOne(), but found: 2 // 需要保证数据干净 lendingRecord record = lendingRecordMapper.selectByUidAndBookId(uid, bookId); if (record == null) { System.out.println("不好意思,查不到该条借书记录~"); } else { // 增加库存 Book book = bookMapper.selectByPrimaryKey(bookId); book.setNum(book.getNum() + record.getNum()); bookMapper.updateByPrimaryKey(book); // 删除记录 lendingRecordMapper.deleteByPrimaryKey(record.getId()); System.out.println("恭喜你,还书成功~~~"); System.out.println("^o^"); } } }
原文地址:https://www.cnblogs.com/xkxf/p/11010843.html
- Android系统线控和歌曲信息屏显的那点事
- 利用Spring中同名Bean相互覆盖的特性,定制平台的类内容。
- 关于java传参
- 浅析ButterKnife
- SQL Server常用语句
- pyecharts(一):Python可视化利器
- 如何在Python中保存ARIMA时间序列预测模型
- SVN+Apache域用户认证配置方法_Windows(转,重新排版,部分内容更新优化)
- sum(x) over( partition by y ORDER BY z ) 分析
- Android DataBinding 数据绑定
- Flask拾遗笔记之上下文
- switch.....case....使用最容易犯错的地方
- electron 构建跨平台桌面应用
- 两个关于字符串的经典例子
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Kubernetes 临时存储需要限制吗?
- PHP安全:变量的前世今生
- 如何使用ParamSpider在Web文档中搜索敏感参数
- Spring 的 Controller 是单例还是多例?怎么保证并发的安全
- 《剑指offer》第28天:最长上升子序列(高频)
- 《剑指offer》第27天:三角形最小路径和
- 《剑指offer》第26天:最大子序和
- Threat Dragon:一款针对OWASP的威胁模型构建平台
- 《剑指offer》第25天:最简单的动态规划
- 基于web页面开发串口程序界面---html代码
- Debotnet:一款针对Windows10隐私设置和数据的保护工具
- 基于web页面开发串口程序界面---代码实现
- 《剑指offer》第24天:链表相加
- 《剑指offer》第23天:删除链表倒数第N个节点
- 《剑指offer》第22天:链表成环的新解法