SpringBoot控制檯版圖書借閱程序

// 實驗存檔。。。java

效果圖:spring

完整程序:https://pan.baidu.com/s/1-d1J90dkEtM0WKkABu0K0Q mybatis

提取碼:hcnm app

DAO層代碼由MyBatis Generator生成,僅補充若干自定義代碼。主要的代碼只有下面這個:ide

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("一、管理書");
            System.out.println("二、管理用戶");
            System.out.println("三、借書");
            System.out.println("四、還書");
            System.out.println("五、退出程序");
            command = scan.nextInt();
            if (command == 1) {
                System.out.println("請問你下一步要幹嗎?");
                System.out.println("一、增書");
                System.out.println("二、改書");
                System.out.println("三、刪書");
                System.out.println("四、查書");
                System.out.println("五、瀏覽書");
                command = scan.nextInt();
                if (command == 1) {
                    System.out.println("一、請你依次輸入書的書名、做者、出版社、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("一、增用戶");
                System.out.println("二、改用戶");
                System.out.println("三、刪用戶");
                System.out.println("四、查用戶");
                System.out.println("五、瀏覽用戶");
                command = scan.nextInt();
                if (command == 1) {
                    System.out.println("一、請你依次輸入用戶的身份證號碼、姓名、出生年月、性別");
                    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^");
        }
    }
}
相關文章
相關標籤/搜索