jdbc实现图书馆借阅系统

时间:2022-08-26 21:19:37

前言

每位在学校学java的同学在期末会有java课程设计,而选题很可能就是图书管理系统,此篇文章可以帮助到你。能力有限,代码有bug可以下方的评论区指明

简介

图书馆管理系统分为用户登录和管理员登录,整个系统的控制是在控制台下进行操作的。

用户层

用户可以进行注册,登录,借书,查书,密码修改,还书等功能。

管理员层

管理员可以进行对管理书籍包括新书上架,修改库存信息等功能。

系统本身

系统本身可以根据用户是否逾期进行锁定禁止借书的功能等。

需要掌握的java知识

java基础语法,容器(list),jdbc,sql语句。

po层代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
 *
 * 对数据库的操作进行单独处理,更多的逻辑更改在更高的逻辑层编写。降低耦合
 *
 */
public class libararydao {
 
  /**
   * 注册图书
   * @param user 注册图书信息
   * @return
   */
  public boolean addbook(bookinfo bookinfo){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "insert into bookinfo (bookname,zuozhe,shuliang,shengyushuliang,date)"
          + " values (?,?,?,?,?) ";
      preparedstatement pre =con.preparestatement(sql);
      pre.setstring(1, bookinfo.getbookname());
      pre.setstring(2, bookinfo.getzuozhe());
      pre.setint(3, bookinfo.getshuliang());
      pre.setint(4, bookinfo.getshuliang());
      pre.setdate(5, new java.sql.date(bookinfo.getdata().gettime()));
      pre.execute();
 
    } catch (sqlexception e) {
      system.out.println("图书注册失败");
      e.printstacktrace();
      return false;
    }
    return true;
  }
 
  /**
   * 注册账号
   * @param user 注册信息
   * @return
   */
  public boolean adduser(user user){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "insert into userinfo (username,password,books) values (?,?,0) ";
      preparedstatement pre =con.preparestatement(sql);
      pre.setstring(1, user.getusername());
      pre.setstring(2, user.getpassword());
      pre.execute();
 
    } catch (sqlexception e) {
      system.out.println("注册失败");
      e.printstacktrace();
      return false;
    }
    return true;
  }
 
 
  /**
   * 查询用户
   * @param user 信息
   * @return
   */
  public resultset queryuser(user user){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "select * from userinfo where username = ? ";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      pre.setstring(1, user.getusername());
      resultset r =pre.executequery();
      return r;
    } catch (sqlexception e) {
      system.out.println("查询失败");
      e.printstacktrace();
      return null;
    }
  }
 
 
  /**
   * 更新持有书
   * @param user 信息
   * @return
   */
  public boolean updateuserbooks(user user){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "update userinfo set books = ? where username = ? ";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      pre.setint(1, user.getbooks());
      pre.setstring(2, user.getusername());
      int r =pre.executeupdate();
      if(r>0){
        return true;
      }else{
        return false;
      }
    } catch (sqlexception e) {
      system.out.println("查询失败");
      e.printstacktrace();
      return false;
    }
  }
 
  /**
   * 借书表中插入
   * @param username 借书人
   * @param bookname 书名
   * @return
   */
  public boolean addborrowinfo(string username,string bookname){
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "insert into borrowinfo (username,bookname,borrowdate,returndate)"
          + " values (?,?,?,?) ";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      pre.setstring(1, username);
      pre.setstring(2, bookname);
      pre.setdate(3, new java.sql.date(new date().gettime()));
      pre.setdate(4, new java.sql.date(dateutil.addmonth(new date()).gettime()));
      pre.execute();
 
    } catch (sqlexception e) {
      system.out.println("借书失败");
      e.printstacktrace();
      return false;
    }
    return true;
  }
 
  /**
   * 查书
   * @param bookname 书名
   * @return
   */
  public resultset querybook(string bookname){
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "select * from bookinfo where bookname ='"+bookname+"'";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      resultset r =pre.executequery();
      return r;
    }
    catch (sqlexception e) {
      system.out.println("借书失败");
      e.printstacktrace();
      return null;
    }}
  /**
   * 查询所有的书籍
   * @return
   */
  public resultset queryallbook(){
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "select * from bookinfo ";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      resultset r =pre.executequery();
      return r;
    }
    catch (sqlexception e) {
      system.out.println("借书失败");
      e.printstacktrace();
      return null;
    }
  }
  /**
   * 分页
   * @param page
   * @return
   */
    public resultset querysubbookinfo(int page){
      try {
 
        connection con =jdbcutil.creatconnection();
        preparedstatement pre;
        string sql = "select * from bookinfo limit ?,5 ";
 
        pre = con.preparestatement(sql);
        pre.setint(1, page);
        resultset r =pre.executequery();
 
        return r;
      }
      catch (sqlexception e) {
        system.out.println("借书失败");
        e.printstacktrace();
        return null;
      }
 
 
  }
  /**
   * 数据页数 五条数据为一组
   * @return
   */
  public int querypageinfo(){
 
    try {
      int zongshu = 0;
      connection con =jdbcutil.creatconnection();
      string sql1 = "select count(id) as cou from bookinfo ";
 
      statement sta =con.createstatement();
      resultset res =sta.executequery(sql1);
      if(res.next())
      {
        zongshu = res.getint("cou");
        if(zongshu == 0)return 0;
      }
      int a =0;
      if(zongshu%5==0){
        a = zongshu/5;
      }else{
        a = zongshu/5+1;
      }
 
      return a;
    }
    catch (sqlexception e) {
      system.out.println("借书失败");
      e.printstacktrace();
      return -1;
    }
 
  }
 
 
  /**
   * 更新剩余数量
   * @param bookname 书名
   * @param sysl 数量
   * @return
   */
  public boolean updatebookinfo(string bookname,int sysl){
    try {
      connection con =jdbcutil.creatconnection();
 
      string sql = "update bookinfo set shengyushuliang = ? where bookname = ?";
      preparedstatement pre;
      pre = con.preparestatement(sql);
      pre.setint(1,sysl);
      pre.setstring(2, bookname);
 
      int r =pre.executeupdate();
 
      if(r>0){
        return true;
      }else{
        return false;
      }
    } catch (sqlexception e) {
      system.out.println("借书失败");
      e.printstacktrace();
      return false;
    }
    }
 
 
 
    /*public boolean querybook(string...strings){
      try {
        connection con =jdbcutil.creatconnection();
        string sql = "select bookname from bookinfo where 1=1 ";
 
        preparedstatement pre;
        pre = con.preparestatement(sql);
        resultset r =pre.executequery();
        if(r.next()){
          return true;
        }else{
          return false;
        }
      } catch (sqlexception e) {
        system.out.println("借书失败");
        e.printstacktrace();
        return false;
      }*/
    /**
     * 查询用户的所有的借阅信息
     * @param username
     * @return
     */
  public resultset queryborrowinfo(string username){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "select * from borrowinfo where username = '"+username+"'";
      preparedstatement pre = con.preparestatement(sql);
      resultset r =pre.executequery();
      return r;
    } catch (sqlexception e) {
      system.out.println("查询失败");
      e.printstacktrace();
      return null;
    }
  }
  /**
   * 查询借阅信息
   * @param username
   * @return
   */
  public resultset queryborrowinfo(string username,string bookname){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "select * from borrowinfo where username = ? and bookname = ?";
      preparedstatement pre = con.preparestatement(sql);
      pre.setstring(1, username);
      pre.setstring(2, bookname);
      resultset r =pre.executequery();
      return r;
    } catch (sqlexception e) {
      system.out.println("查询失败");
      e.printstacktrace();
      return null;
    }
 
  }
  /**
   * 删除借书
   * @param username 人名
   * @param bookname 书名
   * @return
   */
  public boolean deleteborrowinfo(string username,string bookname){
 
    try {
      connection con =jdbcutil.creatconnection();
      string sql = "delete from borrowinfo where username = ? and bookname = ? ";
      preparedstatement pre = con.preparestatement(sql);
      pre.setstring(1, username);
      pre.setstring(2, bookname);
      pre.execute();
      return true;
    } catch (sqlexception e) {
      system.out.println("查询失败");
      e.printstacktrace();
      return false;
    }
  }
 
}

control层代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
 * asdfasd 发斯蒂芬
 *
 */
 
package library.controller;
 
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
import java.util.scanner;
 
import library.dao.libararydao;
import library.po.bookinfo;
import library.po.borrowinfo;
import library.po.user;
 
public class bookcontroller {
  private static libararydao bookdao;
  private borrowinfo b;
  public bookcontroller() {
    if(bookdao == null) bookdao = new libararydao();
  }
 
  public static void main(string[] args) {
  //system.out.println(new bookcontroller().adduser(new user("123", "dvdf")));
      //system.out.println(new bookcontroller().borrowbook("123", new user("123", "dvdf")));
  // new bookcontroller().fenyeinfo(2);
 
 
  }
 
  /**
   * 用户登录
   * @param user 传过来的账号密码
   * @return 返回1为登陆成功 2为不存在 3异常
   */
  public int loginuser(user user){
 
    resultset rs =bookdao.queryuser(user);//验证用户是否存在
    try {
      if(rs!=null&&rs.next()){
        boolean b =rs.getstring("password").equals(user.getpassword());
        if(b){
          return 1;
        }
      }
      return 2;
    } catch (sqlexception e) {
      e.printstacktrace();
      return 3;
    
 
  }
 
  /**
   * 管理员登陆
   * @param user 传过来的账号密码
   * @return 返回1为登陆成功 2为不存在 3异常
   */
  public int loginguanli(user user){
 
    resultset rs =bookdao.queryuser(user);//验证管理员是否存在
    try {
      if(rs!=null&&rs.next()){
        boolean b =rs.getstring("password").equals(user.getpassword());
        int b1 = rs.getint("isguanli");//管理员标志位1
        if(b&&b1==1){
          return 1;
        }
      }return 2;
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 3;
    
 
  }
  /**
   * 查询书籍列表
   * @return 返回1为查询成功并已经输出 2为失败或其他
   */
  public int querybookinfo(){
 
    try {
      resultset r =bookdao.queryallbook();
      while(r!=null&&r.next()){
        system.out.println("书名"+r.getstring("bookname")+"作者"+
            r.getstring("zuozhe")+"总数"+r.getint("shuliang")+"剩余数量"+r.getint("shengyushuliang"));
      }
      if(r==null)return 2;
      return 1;
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 2;
    }
 
 
 
  }
  /**
   * 查询书籍数量和剩余数量
   * @return 返回1为查询成功并已经输出 2为失败或其他
   */
  public resultset querybookk(string bookname){
 
      resultset r =bookdao.querybook(bookname);
      return r;
 
  }
  /**
   * 查询书籍
   * @return 返回1为查询成功并已经输出 2为失败或其他
   */
  public int querybook(string bookname){
    try {
      resultset r =bookdao.querybook(bookname);
      while(r!=null&&r.next()){
        system.out.println("书名"+r.getstring("bookname")+"作者"+
            r.getstring("zuozhe")+"总数"+r.getint("shuliang")+"剩余数量"+r.getint("shengyushuliang"));
      }
      if(r==null)return 2;
      return 1;
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 2;
    }
 
  }
 
  /**
   * 加入新的图书信息
   * @param bookinfo 书籍的详细信息
   * @return 1成功 2失败
   */
  public int addbookinfo(bookinfo bookinfo){
    boolean b = bookdao.addbook(bookinfo);
    if(b)return 1;
    return 2;
  }
 
 
 
 
  /**
   * 注册用户
   * @param user
   * @return
   */
  public int adduser(user user){
    resultset rs =bookdao.queryuser(user);
    try {
 
      if(rs!=null&&!rs.next()){
        boolean b =bookdao.adduser(user);
        if(b)return 1;
      }
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 3;
    
    return 2;
  }
 
  /**
   * 查询用户所有信息
   * @param user
   * @return
   */
  public int queryuser(user user){
    resultset r = bookdao.queryuser(user);
    try {
      if(r.next()){
        user.setbooks(r.getint("books"));
        system.out.println(user);
        return 1;
      }
        return 2;
 
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 2;
    }
 
 
  }
 
 
  /**
   * 借书 先检查书是否存在,再检查书籍是否有库存,然后检查是否已结借此书
   * 最后以上检查没有错误,就可以借书。 用户持有数量增加,借书信息详解录入,库存更新
   *
   * 注意事项
   * 1 resultset 默认指向数据前一个位置 必须 用 next
   * 2 需要加入事务 防止出现错误造成数据表更新不一致
   * @param bookname 书名
   * @return
   */
  public string borrowbook(string bookname,user user){
    try {
      if(!bookdao.querybook(bookname).next()){
        return "借书失败-书籍不存在";}
 
      resultset querybookrs = bookdao.querybook(bookname);
      while(querybookrs.next()){
        int t = querybookrs.getint("shengyushuliang");
        if(t<1){return "库存不足";}
 
        resultset rs =bookdao.queryborrowinfo(user.getusername());
 
        if(rs!=null&&!rs.next()){
 
 
          //加入借阅详细信息
          boolean b1 =bookdao.addborrowinfo(user.getusername(), bookname);
 
 
          //更新借书数量
          resultset rs1 = bookdao.queryuser(user);
          rs1.next();
          user.setbooks(rs1.getint("books")+1);
          bookdao.updateuserbooks(user);
 
 
          //更新库存
          boolean b2 =bookdao.updatebookinfo(bookname, t-1);
          //system.out.println(b1+" "+b2);
 
          return b1?(b2?"借书成功":"借书失败"):("借书失败");
 
        }
       }
 
      return null;
 
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return "借书失败-其他错误";
    }
 
 
  }
  /**还书
   * 更新用户持有数,归还书的库存,删除借书信息表中信息
   * @param user 更新数据数
   * @param bookinfo 更新库存后的数
   * @return
   */
  public int returnbook(user user,string bookname){
 
    boolean b1 = bookdao.deleteborrowinfo(user.getusername(),bookname);
    boolean b2 = false;
    boolean b3 = false;
    try {
      //更新库存
      resultset r = bookdao.querybook(bookname);
      if(r.next()){
        int i = r.getint("shengyushuliang");
        b2 = bookdao.updatebookinfo(bookname,i+1);
      }
 
      //更新持有书数
      r = bookdao.queryuser(user);
      if(r.next()){
        int i = r.getint("books");
        user.setbooks(i-1);
        b3 =bookdao.updateuserbooks(user);
      }
 
      if(!b1||!b2||!b3){
        return 2;
      }
      return 1;
    } catch (sqlexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
      return 2;
    }
 
  }
 
 
 
  /**
   * 查询用户的借书情况
   * @param user 用户
   * @return 返回借阅信息
   */
  public list<borrowinfo> queryborrowinfo(user user){
 
 
    resultset r =  bookdao.queryborrowinfo(user.getusername());
    list<borrowinfo> l = new arraylist<borrowinfo>();
    try {
      while(r!=null&&r.next()){
 
        b = new borrowinfo();
        b.setusername(r.getstring("username"));
        b.setbookname(r.getstring("bookname"));
        b.setborrowdate(r.getdate("borrowdate"));
        b.setreturndate(r.getdate("returndate"));
        l.add(b);
 
      }
      return l;
    } catch (sqlexception e) {
 
      e.printstacktrace();
      return null;
    }
  }
  /**
   * 指定页码
   * @param age
   */
  public void fenyeinfo(int age){
    int i =bookdao.querypageinfo();
    system.out.println("总页数:"+i);
    try {
      if(i>0){
        resultset r = bookdao.querysubbookinfo((age-1)*5);
        while(r!=null&&r.next()){
          system.out.println("书名"+r.getstring("bookname")+"作者"+
          r.getstring("zuozhe")+"总数"+r.getint("shuliang")+"剩余数量"+r.getint("shengyushuliang")); 
          }
      
 
    } catch (sqlexception e) {
      e.printstacktrace();
    }
 
  }
  /**
   * 指定上下页
   * @param age
   */
  public void fenyeinfo(string age){
 
 
  }
 
  /**
   * 更新图书信息
   * 更新作者,库存,剩余数量,不更新日期,书名(不就是添加了新的书籍了吗)
   * 可更新任务可以多选
   *
   * 书名进行判断是否存在
   * 库存和剩余数量进行逻辑上的验证
   */
  public void updatebookinfo(string bookname){
    scanner scan = new scanner(system.in);
 
    system.out.println("更新作者输入:1,跳过11");
    int i =scan.nextint();
    list<list<string>> list = new arraylist<list<string>>();
    list<string> list1 = null;
    if(i==1){
      string newbookname = scan.next();
      list1 = new arraylist<string>();
      list1.add("zuozhe");
      list1.add("'"+newbookname+"'");
      list.add(list1);
    }
    //如果更新了库存,剩余数量也会跟着更新 需要判断库存逻辑的正确性
    system.out.println("更新数量输入2,跳过22");
    int ii = scan.nextint();
    if(ii==2){
      system.out.println("请更新库存数量");
      int newsum = scan.nextint();
      //判断数量逻辑的正确性
      resultset r = querybookk(bookname);
      int oldsum=0;int sykc=0;//旧库存和剩余数量
      try {
        while(r.next()){
          oldsum = r.getint("shuliang");
          sykc = r.getint("shengyushuliang");
        }
        if(newsum>oldsum){
          sykc+=newsum-oldsum;//记录更新后的剩余数量
          //system.out.println("根据已有库存,库存总量为---"+newsum);
        }
        else if(newsum<oldsum&&newsum>=sykc){//如何新的库存少于就库存。判断新库存与剩余数量的关系
          sykc-=newsum;
        }else{
          system.out.println("输入错误");
          return ;
        }
 
 
 
      } catch (sqlexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
 
 
 
      list1 = new arraylist<string>();
      list1.add("shuliang");
      list1.add(string.valueof(newsum));
      list.add(list1);
      list1 = new arraylist<string>();
      list1.add("shengyushuliang");
      list1.add(string.valueof(sykc));
      list.add(list1);
    }
    bookdao.updatebookinfo(list, bookname);
  }
 
}

view层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package library.view;
 
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.date;
import java.util.inputmismatchexception;
import java.util.list;
import java.util.scanner;
 
import library.controller.bookcontroller;
import library.po.bookinfo;
import library.po.borrowinfo;
import library.po.user;
 
/**
 * 视图类,也是界面的显示层。程序运行的入口
 * 想说的话:
 * 如果使用web作为v层,只需要把bookrun方法拆开就可以了。对应会话跟踪,把 private static user user 存入session对象就可以了
 * 框架的话用springmvc 。对于dao加入orm框架(hibernate ,mybatis)。整合起来也就是常见的ssm体系或ssh体系
 * 数据库的选择更是随你心情。mysql sqlserver 等等。
 * 项目bug仔细看的话很多。本意想给入门的同学一个思路方向。对于一些变量的命名请不要学我(偷懒),养成一个良好的
 * 习惯。祝你在编程的道路上越走越远!!
 */
public class view {
  //controller 业务控制类
  private static bookcontroller bookc = new bookcontroller();
  //登陆后的用户跟踪类似session
  private static user user;
  //控制主界面的循环
  private static boolean flag = true;
  //接受控制台输入
  private static scanner scan = new scanner(system.in);
 
  public static void main(string[] args) {
   bookrun();
  }
 
 
 
  //主界面  应该考虑其他输入的限定 (当输入字符时,系统停止运行)
  public static void bookrun(){
      try{
        do{
          system.out.println("欢迎来到图书借阅系统\n登陆输入: 1\n注册输入: 2\n管理员管理:3");
          int i1 = scan.nextint();
 
          switch (i1) {
          case 1://登陆逻辑
            system.out.println("输入账号");
            string username = scan.next();
            system.out.println("密码");
            string password = scan.next();
 
            int i =bookc.loginuser(new user(username, password));
 
            if(i==1){
 
              system.out.println("登陆成功");
              user = new user(username, password);
              loginsuccess(user);
 
            }else if(i!=1){
              system.out.println("登陆失败,检查密码或者账号");
              }
            break;
 
          case 2:
            //注册逻辑 可以添加验证密码 进行密码的确定  java api有把明文进行处理的方法,请自行搜索
            //还可以加入正则表达式进行账号的限定(对于特殊字符输入的处理等)
            system.out.println("输入申请账号");
            string username1 = scan.next();
            system.out.println("密码");
            string password1 = scan.next();
            //此处可以再次确认密码的逻辑
            system.out.println(adduser(new user(username1, password1)));
            system.out.println("返回上一层");
 
            break;
 
          case 3://管理员逻辑
            system.out.println("输入管理员账号");
            string username2 = scan.next();
            system.out.println("密码");
            string password2 = scan.next();
            user = new user(username2, password2);
            int i2 =bookc.loginguanli(user);
            if(i2==1){
              loginguanlisuccess();
            }else{
              system.out.println("登陆失败,检查密码或者账号");
            }
 
            break;
 
          default :
            system.out.println("输入序号不正确");
            break;
          }
 
        }while(flag);
 
 
      }catch(inputmismatchexception e){
        system.out.println("输入异常,请重启系统");
      }
 
 
  }
 
  //此处可以添加新的功能而不影响已有功能 (可以改为switch语句)
  private static void loginguanlisuccess() {
    while(true){
      system.out.println("添加图书:1\n更改图书信息:2\n返回:3");
      int i = scan.nextint();
      if(i==1){
        addbook();
      }else if(i==2){
        updatebookinfo();
      }else if(i==3){
        return;
      }
    }
 
  }
 
//更新图书
/**
 * 更新图书 只有管理员权限可以操作
 */
  private static void updatebookinfo() {
 
 
 
    //应该判断书名是否存在 contro层设计
    system.out.println("请输入书名");
    string bookname = scan.next();
    int a = bookc.querybook(bookname);
    if(a!=1){
      system.out.println("不存在,已返回上一层");
      return;
    }
 
/*   system.out.println("更新作者输入1,跳过11");
    int i =scan.nextint();
    list<list<string>> list = new arraylist<list<string>>();
    list<string> list1 = null;
    if(i==1){
      string newbookname = scan.next();
      list1 = new arraylist<string>();
      list1.add("zuozhe");
      list1.add("'"+newbookname+"'");
      list.add(list1);
    }
    //如果更新了库存,剩余数量也会跟着更新 需要判断库存逻辑的正确性
    system.out.println("更新数量输入2,跳过22");
    int ii = scan.nextint();
    if(ii==2){
      //判断数量逻辑的正确性
      resultset r =bookc.querybookk(bookname);
      try {
        int i1=0;int i2=0;
        while(r.next()){
          i1 = r.getint("shuliang");
          i2 = r.getint("shengyushuliang");
        }
        if(ii>i1){
          i2+=ii-i1;
        }
        if(ii<i1&&ii>=i2){
 
        }
        system.out.println("更新后的数量错误");
        return;
 
 
 
      } catch (sqlexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
 
 
 
 
      string newkucun= scan.next();
      list1 = new arraylist<string>();
      list1.add("shuliang");
      list1.add(newkucun);
      list.add(list1);
    }*/
 
    bookc.updatebookinfo(bookname);
  }
 
 
//添加图书 需要保证名字唯一性
  private static void addbook() {
    system.out.println("图名");
    string bookname = scan.next();
    system.out.println("作者");
    string zuozhe = scan.next();
    system.out.println("数量");
    int  shuliang = scan.nextint();
    bookinfo bookinfo = new bookinfo(bookname, zuozhe, shuliang, shuliang,new date());
    int i =bookc.addbookinfo(bookinfo);
    if(i==1){
      system.out.println("添加成功");
 
    }else{
      system.out.println("错误");
    }
 
  }
 
  //注册界面的实现
  private static string adduser(user user){
    switch (bookc.adduser(user)) {
    case 1:
      return "注册成功";
 
    case 2:
      return "用户已注册";
 
    case 3:
      return "其他错误,重试";
    }
 
    return "其他错误,重试";
 
  }
  //登陆功能实现
  private static void loginsuccess(user user ){
    while (true){
      system.out.println("查看借阅信息请输入:1,借书:2,还书:3,账号信息:4,退出:5"
          + "查询图书:6修改密码:7\n");
      int i = scan.nextint();
      switch (i) {
      case 1:
        borrowinfo(user);
        break;
      case 2:
        borrowbook(user);
        break;
      case 3:
        returnbook(user);
        break;
      case 4:
        queryuser(user);
        break;
      case 5:
        return ;
      case 6:
        system.out.println("请输入书名");
 
        querybook(scan.next());
        break;
      case 7: break;
      default:
        system.out.println("输入错误");
        break;
      }
    }
 
 
  }
  //查询借阅信息
  private static void borrowinfo(user user ){
    list<borrowinfo> l=bookc.queryborrowinfo(user);
    if(l==null||l.isempty()){
      system.out.println("没有借阅信息");
      return;
    }
    for (borrowinfo borrowinfo : l) {
      system.out.println(borrowinfo);
    }
 
  }
  //借书
  private static void borrowbook(user user){
    system.out.println("请输入借阅的书籍名称");
    string bookname =scan.next();
    string flog = bookc.borrowbook(bookname, user);
    system.out.println(flog);
    system.out.println("是否继续借书:y|n");
    string flog1 = scan.next();
    if(flog1.equalsignorecase("y")){
      borrowbook(user);
    }
  }
  //还书
  private static void returnbook(user user1 ){
    list<borrowinfo> l=bookc.queryborrowinfo(user1);
    if(l==null||l.isempty()){
      system.out.println("没有借阅信息");
      return;
    }
    for (borrowinfo borrowinfo : l) {
      system.out.println(borrowinfo);
    }
    system.out.println("请输入要还书籍的名字");
    string bookname = scan.next();
    int flog =bookc.returnbook(user1, bookname);
    if(flog==1){
      system.out.println("还书成功");
    }else{
      system.out.println("失败");
    }
    system.out.println("是否继续还书:y|n");
    string flog1 = scan.next();
    if(flog1.equalsignorecase("y")){
      returnbook(user1);
    
 
  }
  //用户信息
  private static void queryuser(user user){
    int i =bookc.queryuser(user);
    if( i!=1){
      system.out.println("失败");
    }
  }
 
  private static void querybook(string bookname){
    int i = bookc.querybook(bookname);
    if(i!=1)system.out.println("书籍不存在");
  }
 
  private static void guanliface(){
    while(true){
      system.out.println("查询书籍列表请输入 :1;添加图书:2;使用分页查看书籍列表");
      int i =scan.nextint();
      switch (i) {
      case 1:
        int i1= bookc.querybookinfo();
        if(i1==2)system.out.println("错误");
        break;
      case 2:
        system.out.println("书名");
        string booknname1 = scan.next();
        system.out.println("作者");
        string zuozhe1 = scan.next();
        system.out.println("数量");
        int shuliang1 = scan.nextint();
        date date = new date();
        int i3 = bookc.addbookinfo(new bookinfo(booknname1, zuozhe1, shuliang1, shuliang1, date));
        if(i3==2)system.out.println("错误");
        break;
      case 3:
      default:
        system.out.println("错");
        break;
      }
    }
 
  }
 
  private static void fenyeinfo(){
 
  }
 
 
}

数据库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/用户表
create table `userinfo` (
 `id` int(10) not null auto_increment,
 `username` varchar(20) collate utf8_bin default null,
 `password` varchar(20) collate utf8_bin default null,
 `books` int(10) default null,
 `isguanli` int(10) not null default '0',
 primary key (`id`)
) engine=innodb auto_increment=21 default charset=utf8 collate=utf8_bin;
 
 
/借书信息
create table `borrowinfo` (
 `id` int(10) not null auto_increment,
 `username` varchar(20) collate utf8_unicode_ci default null,
 `bookname` varchar(20) collate utf8_unicode_ci default null,
 `borrowdate` date default null,
 `returndate` date default null,
 primary key (`id`)
) engine=innodb auto_increment=9 default charset=utf8 collate=utf8_unicode_ci;
 
/书籍信息 字段用的拼音:-d
create table `bookinfo` (
 `id` int(10) not null auto_increment,
 `bookname` varchar(20) collate utf8_unicode_ci not null,
 `zuozhe` varchar(20) collate utf8_unicode_ci default null,
 `shuliang` varchar(20) collate utf8_unicode_ci default null,
 `shengyushuliang` varchar(20) collate utf8_unicode_ci default null,
 `date` date default null,
 primary key (`id`)
) engine=innodb auto_increment=13 default charset=utf8 collate=utf8_unicode_ci;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。