java小项目---图书管理系统

这篇博客介绍了使用Java开发的一个小型图书管理系统。系统包含Library、User类,以及对应的Dao接口和实现,还有LibraryService业务逻辑层、LibraryUi用户界面和一些辅助工具类。通过这个项目,可以了解Java在数据库操作和简单UI设计上的应用。
摘要由CSDN通过智能技术生成

iLibrary

package com.Library.dao;

import com.Library.model.Library;
public interface iLibrary {
    void insertBook(Library newBook) throws Exception;

    void deleteBook(int id) throws Exception;

    void updateBook(int id,Library modBook) throws Exception;

    void selectByID(int id) throws Exception;

    void selectByName(String name) throws Exception;

    void selectByAuthor(String author) throws Exception;

    int selectByName1(String name) throws Exception;

    void addBorrowers(String name1,Library modBook) throws Exception;

    void bookReturns(String name1,Library modBook) throws Exception;

    void selectAllBook() throws  Exception;

    void sortAllBookByName() throws  Exception;

    void selectByConditions() throws  Exception;

    void sortAllBookByID() throws  Exception;
}

iUser

package com.Library.dao;
import com.Library.model.User;
public interface iUser {
    void insertUser(User newUser) throws Exception;  // 添加用户的方法

    void deleteUser(int id) throws Exception;  // 删除用户的方法

    void updateUser(int id,User modUser) throws Exception;  // 更新用户的方法

    void selectByID(int id) throws Exception;   // 根据id查询用户

     void insertUser1(User newUser) throws Exception;

    void selectAllUser() throws  Exception;// 查询全部用户
}

LibraryDao

package com.Library.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.Library.dao.iLibrary;
import com.Library.model.Library;
import com.Library.util.DBUtil;

public class LibraryDao implements iLibrary{
    DBUtil db = new DBUtil();

    @Override
    public void insertBook(Library newBook) throws Exception { // 增加图书方法的实现
        // 添加到数据库中进行保存
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert = "insert into book (name,author,type1,conditions) values(?,?,?,?)";
        pstm = conn.prepareStatement(sql_insert);
        pstm.setString(1, newBook.getName());
        pstm.setString(2, newBook.getAuthor());
        pstm.setString(3, newBook.getType());
        pstm.setInt(4,0);
        System.out.println(newBook.getName());
        System.out.println(newBook.getCondition());
        int row = pstm.executeUpdate();
        System.out.println("添加成功," + row + "行受到影响.");

        db.closeConn(null, pstm, conn);
    }

    @Override
    public void deleteBook(int id) throws Exception {
        // 根据主键编号删除
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_delete = "delete from book where id=?";

        pstm = conn.prepareStatement(sql_delete);
        pstm.setInt(1, id);

        int row = pstm.executeUpdate();
        if(row==0){
            System.out.println("没有这本书");
        }
        else { System.out.println("删除成功,"+row+"行受到影响");}

        db.closeConn(null, pstm, conn);
    }
    @Override
    public void addBorrowers(String name1, Library modBook)throws Exception{
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update book set Borrowers=?,tel=?,conditions=? where  name=?";

        pstm = conn.prepareStatement(sql_update);
        pstm.setString(1,modBook.getBorrowers());
        pstm.setString(2,modBook.getTel());
        pstm.setInt(3,1);
        pstm.setString(4,name1);

        int row = pstm.executeUpdate();
        if(row==0){
            System.out.println("没有这本书");
        }
        else { System.out.println("更新成功,"+row+"行受到影响");}

        db.closeConn(null, pstm, conn);
    }
    @Override
    public void bookReturns(String name1, Library modBook)throws Exception{
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update book set Borrowers=?,tel=?,conditions=? ,score=? where  name=?";

        pstm = conn.prepareStatement(sql_update);
        pstm.setString(1,null);
        pstm.setString(2,null);
        pstm.setInt(3,0);
        pstm.setFloat(4,modBook.getScore());
        pstm.setString(5,name1);

        int row = pstm.executeUpdate();
        if(row==0){
            System.out.println("没有这本书");
        }
        else { System.out.println("更新成功,"+row+"行受到影响");}

        db.closeConn(null, pstm, conn);
    }
    @Override
    public void updateBook(int id,Library modBook) throws Exception {  // 根据id修改图书方法的实现
        // 根据主键编号修改图书信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update book set name=?,author=?,type1=?,conditions=? where id=?";

        pstm = conn.prepareStatement(sql_update);
        pstm.setString(1,modBook.getName());
        pstm.setString(2,modBook.getAuthor());
        pstm.setString(3,modBook.getType());
        pstm.setInt(4,0);
        pstm.setInt(5,id);
        System.out.println(id);
        System.out.println(modBook.getName());

        int row = pstm.executeUpdate();
        if(row==0){
            System.out.println("没有这本书");
        }
        else { System.out.println("更新成功,"+row+"行受到影响");}


        db.closeConn(null, pstm, conn);
    }
    @Override
    public void selectByName(String name) throws Exception {  // 根据name单查方法的实现
        // 根据name查询信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectByName = "select * from book where name like ?";

        pstm = conn.prepareStatement(sql_selectByName);
        pstm.setString(1, "%"+name+"%");

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    public void selectByConditions() throws Exception {  // 根据name单查方法的实现
        // 根据name查询信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectByName = "select * from book where conditions like ?";

        pstm = conn.prepareStatement(sql_selectByName);
        pstm.setInt(1, 0);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    @Override
    public void selectByAuthor(String author) throws Exception {  // 根据name单查方法的实现
        // 根据name查询信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectByAuthor = "select * from book where author like ?";

        pstm = conn.prepareStatement(sql_selectByAuthor);
        pstm.setString(1, "%"+author+"%");

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    @Override
    public int selectByName1(String name) throws Exception {  // 根据name单查方法的实现
        // 根据name查询信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectByName1 = "select * from book where name=?";

        pstm = conn.prepareStatement(sql_selectByName1);
        pstm.setString(1, name);
        rs = pstm.executeQuery();
        int o=0;
        while (rs.next()){
        o=rs.getInt("conditions");

        }
        db.closeConn(rs, pstm, conn);
        return o;
    }

    @Override
    public void selectByID(int id) throws Exception {  // 根据id单查方法的实现
      
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById = "select * from book where id=?";

        pstm = conn.prepareStatement(sql_selectById);
        pstm.setInt(1, id);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }


    @Override
    public void selectAllBook() throws Exception {
        //
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from book";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("书库信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+"评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("Borrowers")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    @Override
    public void sortAllBookByName() throws Exception {
        //
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from book order by name";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("书库信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("Borrowers")+"\t\t\t"+rs.getString("tel")+"\t\t\t"+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    @Override
    public void sortAllBookByID() throws Exception {
        //
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from book order by id";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("书库信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("Borrowers")+"\t\t\t"+rs.getString("tel")+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
    public void sortAllBookByScore() throws Exception {
        //
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from book order by score";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("书库信息如下:");
        System.out.println("编号\t\t" + "名称\t\t\t\t\t"+ "作者\t\t\t\t" + "状态\t\t\t"+ "类型\t\t\t"+ "借阅者\t\t\t"+ "电话\t\t\t"+ "评分\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("author")+"\t\t\t"+(rs.getInt("conditions")==1?"借出":"未借出")+"\t\t\t"+rs.getString("type1")+"\t\t\t"+rs.getString("Borrowers")+"\t\t\t"+rs.getString("tel")+rs.getFloat("score")+"\t\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }
}

UserDao

package com.Library.daoimpl;
import com.Library.dao.iUser;
import com.Library.model.User;
import com.Library.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao implements iUser{
    DBUtil db = new DBUtil();
    private Connection conn;

    @Override
    public void insertUser(User newUser) throws Exception {
        // 将用户信息添加到后台数据库表中
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert = "insert into user(username,password,type) values(?,?,?)";
        pstm = conn.prepareStatement(sql_insert);

        pstm.setString(1, newUser.getUsername());
        pstm.setString(2, newUser.getPassword());
        pstm.setInt(3, newUser.getType());

        int row = pstm.executeUpdate();
        System.out.println("新增用户成功" + row + "行受到影响");
        db.closeConn(null, pstm, conn);
    }
    @Override
    public void insertUser1(User newUser) throws Exception {
        // 将用户信息添加到后台数据库表中
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert = "insert into user(username,password,type) values(?,?,0)";
        pstm = conn.prepareStatement(sql_insert);

        pstm.setString(1, newUser.getUsername());
        pstm.setString(2, newUser.getPassword());


        int row = pstm.executeUpdate();
        System.out.println("新增用户成功" + row + "行受到影响");
        db.closeConn(null, pstm, conn);
    }

    @Override
    public void deleteUser(int id) throws Exception {
        // 从数据表中删除用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_delete = "delete from user where id=?";
        pstm = conn.prepareStatement(sql_delete);

        pstm.setInt(1,id);

        int row = pstm.executeUpdate();
        System.out.println("删除用户成功"+row+"行受到影响");
        db.closeConn(null, pstm, conn);
    }

    @Override
    public void updateUser(int id, User modUser) throws Exception {
        // 从数据库中修改用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;




            String sql_update = "update user set username=? password=? type=? where id=?";
            pstm = conn.prepareStatement(sql_update);

            pstm.setString(1,modUser.getUsername());
            pstm.setString(2,modUser.getPassword());
            pstm.setInt(3,modUser.getType());
            pstm.setInt(4,id);


        int row = pstm.executeUpdate();
        System.out.println("修改用户成功"+row+"行受到影响");
        db.closeConn(null, pstm, conn);
    }



    @Override
    public void selectByID(int id) throws Exception {
        // 根据id单查用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById = "select * from user where id=?";
        pstm = conn.prepareStatement(sql_selectById);

        pstm.setInt(1,id);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("用户编号\t\t用户名称\t\t用户密码\t\t用户类型\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("username")+"\t\t"+rs.getString("password")+"\t\t"+(rs.getInt("type")==0?"管理员":"普通用户"));
        }
        db.closeConn(rs, pstm, conn);
    }

    @Override
    public void selectAllUser() throws Exception {
        // 查询全部用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectByAll = "select * from user";
        pstm = conn.prepareStatement(sql_selectByAll);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("用户编号\t\t用户名称\t\t用户密码\t\t用户类型\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("username")+"\t\t"+rs.getString("password")+"\t\t"+(rs.getInt("type")==0?"管理员":"普通用户"));
        }

        db.closeConn(rs, pstm, conn);
    }

    // 判断是否存在该用户,存在返回值为true,不存在返回值为false
    public boolean getHaveUser(User user) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_selectHave = "select * from user where username=? and password=? and type=?";
        pstm = conn.prepareStatement(sql_selectHave);

        pstm.setString(1,user.getUsername());
        pstm.setString(2,user.getPassword());
        pstm.setInt(3,user.getType());

        ResultSet rs = pstm.executeQuery();
        while(rs.next()){
            return true;
        }
        return false;
    }
}

class Library

package com.Library.model;

public class Library {
    //属性字段
    private int bookId;
    private String name;
    private  String author;
    private  int condition;
    private String type;
    private String Borrowers;
    private String tel;

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    private float score;

    public String getBorrowers() {
        return Borrowers;
    }

    public void setBorrowers(String borrowers) {
        Borrowers = borrowers;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getCondition() {
        return condition;
    }

    public void setCondition(int condition) {
        this.condition = condition;
    }







    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }


    public Library(){

    }
    public Library(int bookId,String name,String author,int condition){
        this.bookId=bookId;
        this.name=name;
        this.author=author;
       this.condition=condition;
    }
}

class User

package com.Library.model;

public class User {
    private int id;
    private String username;
    private String password;
    private int type;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    public User() {
    }

    public User(String username, String password, int type) {
        this.username = username;
        this.password = password;
        this.type = type;
    }
}

LibraryService

package com.Library.service;
import com.Library.model.User;
import java.util.Scanner;
import com.Library.daoimpl.LibraryDao;
import com.Library.daoimpl.UserDao;
import com.Library.model.Library;
import com.Library.model.User;
import com.Library.util.LibraryUtil;
import com.Library.util.UserUtil;


public class LibraryService {
    public Scanner sc=new Scanner(System.in);
    public void do_Login() throws Exception {
        System.out.println("1.注册");
        System.out.println("2.登录");
        System.out.println("3.退出");
        int a=sc.nextInt();
        if(a==1){
            User newUser = new UserUtil().getUser1("新增");
            new UserDao().insertUser1(newUser);

        }
        else if(a==3){
            System.exit(-1);
        }

            System.out.print("请输入用户名:");
            String uname = sc.next();
            System.out.print("请输入密码:");
            String upwd = sc.next();
            System.out.print("请输入用户类型:");
            int utype = sc.nextInt();

            User user = new User(uname, upwd, utype);
            boolean b = new UserDao().getHaveUser(user);
            if (b) {  // 登录成功
                System.out.println("登录成功");
                if (utype==0){
                    System.out.println("\t图书管理系统");
                    new LibraryService().do_getMenu();
                }
                else {
                    System.out.println("图书借阅系统");
                    new LibraryService().do_getMenu1();
                }

            } else {  //登录失败
                System.out.println("账号或密码错误");
                do_Login();
            }


    }

    private void do_getMenu1()throws Exception {
        boolean flag=true;
      String id;
        while(flag){
            System.out.println("1.查图书\t2.借阅图书\t3.归还图书\t4.按名字排序\t5.按id排序\t6.按评分排序\t7.未借出的图书\t8.图书列表\t9.返回登录界面");
            System.out.print("请选择菜单(1-7):");

            int choice = sc.nextInt();

            switch (choice) {
               case 1:
                   System.out.println("请选择按什么查询");
                   System.out.println("1.按书名筛选");
                   System.out.println("2.按作者筛选");
                   System.out.println("3.按id查询");
                   int q=sc.nextInt();
                   if(q==1){
                       id = new LibraryUtil().getBookName();
                       new LibraryDao().selectByName(id);
                   }
                   else if(q==2){
                   id = new LibraryUtil().getBookAuthor();
                   new LibraryDao().selectByAuthor(id);
                   }
                   else if(q==3){
                       int e = new LibraryUtil().getBookId();
                       new LibraryDao().selectByID(e);
                   }

                    break;
                case 2:
                    id = new LibraryUtil().getBookName();
                    int i=new LibraryDao().selectByName1(id);
                    System.out.println(id);
                    Library newBook2 = new LibraryUtil().getBorrow("借阅",i);
                    new LibraryDao().addBorrowers(id,newBook2);
                    break;
                case 3:
                    id = new LibraryUtil().getBookName();
                    int a=new LibraryDao().selectByName1(id);

                    Library newBook3 = new LibraryUtil().getReturn("归还",a);
                    new LibraryDao().bookReturns(id,newBook3);
                    break;
                case 4:
                    new LibraryDao().sortAllBookByName();
                    break;
                case 5:
                    new LibraryDao().sortAllBookByID();
                    break;
                case 6:
                    new LibraryDao().sortAllBookByScore();
                    break;
                case 7:
                    new LibraryDao().selectByConditions();
                    break;

                case 8:
                    new LibraryDao().selectAllBook();
                    break;
                case 9:
                    do_Login();
                    break;
            }
        }
    }

    public void do_getMenu() throws Exception {

        boolean flag = true;
        while (flag) {
            int id = 0;

            System.out.println("1.图书管理\t2.用户管理\t3.退出系统");

            System.out.print("请选择菜单(1-3):");
            int choice = sc.nextInt();
            if (choice == 1) {
                while(flag){
                    System.out.println("1.增加图书\t2.删除图书\t3.修改图书信息\t4.单查图书\t5.图书列表\t6.返回上级");
                    System.out.print("请选择菜单(1-6):");

                    choice = sc.nextInt();

                    switch (choice) {
                        case 1:
                            Library newBook = new LibraryUtil().getLibrary("新增");
                            new LibraryDao().insertBook(newBook);
                            break;
                        case 2:
                            id = new LibraryUtil().getBookId("删除");
                            new LibraryDao().deleteBook(id);
                            break;
                        case 3:
                            id = new LibraryUtil().getBookId("修改");
                            Library newBook2 = new LibraryUtil().getLibrary("修改后");
                            new LibraryDao().updateBook(id, newBook2);
                            break;
                        case 4:
                            id = new LibraryUtil().getBookId("查询");
                            new LibraryDao().selectByID(id);
                            break;
                        case 5:
                            new LibraryDao().selectAllBook();
                            break;
                        case 6:
                            new LibraryService().do_getMenu();
                            break;
                    }
                }


            } else if (choice == 2) {  // 用户管理
                while(flag){
                    System.out.println("1.增加用户\t2.删除用户\t3.修改用户\t4.单查用户\t5.全查用户\t6.返回上级");
                    System.out.print("请选择菜单(1-6):");

                    choice = sc.nextInt();

                    switch (choice) {
                        case 1:
                            User newUser = new UserUtil().getUser("新增");
                            new UserDao().insertUser(newUser);
                            break;
                        case 2:
                            id = new UserUtil().getId("删除");
                            new UserDao().deleteUser(id);
                            break;
                        case 3:
                            id = new UserUtil().getId("修改");
                            User newUser2 = new UserUtil().getUser("修改后");
                            new UserDao().updateUser(id, newUser2);
                            break;
                        case 4:
                            id = new UserUtil().getId("查询");
                            new UserDao().selectByID(id);
                            break;
                        case 5:
                            new UserDao().selectAllUser();
                            break;
                        case 6:
                            new LibraryService().do_getMenu();
                            break;
                    }
                }

            } else if (choice == 3) {  // 退出系统
                do_Login();
            }
        }
    }

}

LibraryUi

package com.Library.ui;
import com.Library.service.LibraryService;
public class LibraryUi {
    public static void main(String[] args) throws Exception {
        new LibraryService().do_Login();
    }
}

package com.Library.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;

public class DBUtil {
    public String url="jdbc:mysql://localhost:3306/libraryuser";
    public String username="root";
    public String password="123";


    public Connection getConn(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("连接数据库成功.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }
    public void closeConn(ResultSet rs, PreparedStatement pstm, Connection conn) throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

LibraryUtil

package com.Library.util;

import com.Library.daoimpl.LibraryDao;
import com.Library.model.Library;
import com.Library.model.User;

import java.util.Scanner;

public class LibraryUtil {
    public  static float [][]s= new float[10][100];
    public  static int c=0;
    Scanner sc = new Scanner(System.in);
    public Library getLibrary(String str){

        Library newBook=new Library();
       System.out.println("请输入"+str+"书名:");
        newBook.setName(sc.next());
        System.out.println("请输入"+str+"作者:");
        newBook.setAuthor(sc.next());
        System.out.println("请输入"+str+"类型:");
        newBook.setType(sc.next());
       newBook.setCondition(1);
        newBook.setBorrowers(null);
        newBook.setTel(null);

        return newBook;
    }
    public Library getBorrow(String str,int i){
        Library newBook=new Library();


          if(i==1){
              System.out.println("已被借出");
          }
          else {
              System.out.println("请输入"+str+"姓名:");
              newBook.setBorrowers(sc.next());
              System.out.println("请输入"+str+"电话:");
              newBook.setTel(sc.next());
              String a=newBook.getBorrowers();
              newBook.setCondition(1);
          }







        return newBook;
    }
    public float sumAvg(float a[]){
        float sum=0;
        for (float v : a) {
            sum+=v;
        }
        return  sum/c;
        }








    public Library getReturn(String str,int i){
        Library newBook=new Library();


        if(i==0){
            System.out.println("该书未被借出");
        }
        else {
            System.out.println("请输入"+str+"评分:");
            s[newBook.getBookId()][c]=sc.nextFloat();
            c++;
            newBook.setScore(sumAvg(s[newBook.getBookId()]));
            String a=newBook.getBorrowers();
            newBook.setCondition(0);
        }







        return newBook;
    }

    public  int getBookId(String str){
        System.out.println("请输入需要" + str + "的图书编号:");
        int id = sc.nextInt();
        return id;
    }
    public  int getBookId(){
        System.out.println("请输入需要的图书编号:");
        int id = sc.nextInt();
        return id;
    }
    public  String getBookName(){
        System.out.println("请输入需要的书名");
        String  a = sc.next();
        return a;
    }
    public  String getBookAuthor(){
        System.out.println("请输入喜欢的作者");
        String  a = sc.next();
        return a;
    }
    public  float getScore1(){
        System.out.println("请输入您的评分");
        float a = sc.nextFloat();
        return a;
    }

}

UserUtil

package com.Library.util;
import com.Library.model.User;
import java.util.Scanner;

public class UserUtil {
    public Scanner input = new Scanner(System.in);

    /**
     * 1. 提供User类参数
     */
    public User getUser(String str){
        User newUser = new User();
        System.out.println("请输入"+str+"用户名称:");
        newUser.setUsername(input.next());
        System.out.println("请输入"+str+"用户密码:");
        newUser.setPassword(input.next());
        System.out.println("请输入"+str+"用户类型:");
        newUser.setType(input.nextInt());   // 0-代表管理员,1-代表普通用户

        return newUser;
    }
    public User getUser1(String str){
        User newUser = new User();
        System.out.println("请输入"+str+"用户名称:");
        newUser.setUsername(input.next());
        System.out.println("请输入"+str+"用户密码:");
        newUser.setPassword(input.next());


        return newUser;
    }

    /**
     * 2. 提供id参数
     */
    public int getId(String str){
        System.out.println("请输入需要"+str+"的用户id:");
        int id  = input.nextInt();

        return id;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值