Java-图书管理系统

Java-图书管理系统

模拟借书系统,任务要求:

这里写图片描述

感觉自己的写出来的代码不够简洁,参考了其他的案例写了一个,效果如下:
结构:
这里写图片描述
运行异常1:
这里写图片描述
运行异常2:
这里写图片描述
运行异常3:
这里写图片描述
正常运行:
1、名称查询
这里写图片描述
2、序号查询:
这里写图片描述
Book.java:

1
2
3
4
5
6
7
8
9
10
11
12
package com.tony;
/*
* 图书租赁系统
*/
public class Book {
public String bookName;
public int bookId;
public Book(String bookName, int bookId){
this.bookName = bookName;
this.bookId = bookId;
}
}

自定义异常类MyException.java:

1
2
3
4
5
6
7
8
9
10
package com.tony;
/*
* 自定义异常类
*/
public class MyException extends Exception{
//有参构造方法
public MyException(String message) {
super(message);
};
}

BorrowBook.java:

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
package com.tony;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/*
* 主程序
*/
public class BorrowBook {

Book book [] = {new Book("高等数学", 1), new Book("大学英语", 2), new Book("Android", 3), new Book("会计基础", 4)};
public List<Book> listBooks;
//无参构造方法
public BorrowBook() {
this.listBooks = new ArrayList<Book>();
}
public void addBook(){
listBooks.addAll(Arrays.asList(book));
}
public void systemOut() {
System.out.println("*******欢迎使用借书系统*******");
System.out.println("*-*-*-*图书一览表:*-*-*-*");
System.out.println(" 书名" + "\t\t" + "序号");
for(Book book:listBooks) {
System.out.println(book.bookName + "\t\t " +book.bookId);
}
}
private Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
BorrowBook borrowBook = new BorrowBook();
borrowBook.addBook();
borrowBook.systemOut();

while(true) {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
switch (borrowBook.scanf()) {
case 1://根据用户不同的输入内容执行
try {
System.out.println("书籍:" + borrowBook.findByName());
break;
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
case 2:
try {
System.out.println("书籍:" + borrowBook.findById());
break;
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
default:
System.out.println("命令错误,请重试.");
continue;
}
break;
}
borrowBook.scanner.close();
}
public String findByName() throws MyException{
System.out.println("*****请输入书籍的名称-->");
String name = scanner.next();
for (Book books : book) {
if (name.equals(books.bookName)) {
return books.bookName + "\t序号:" +books.bookId;
}
}
throw new MyException("名称错误,请重试.");
}
public String findById() throws MyException {
System.out.println("*****请输入书籍的序号-->");
int in = scanner.nextInt();
for (int i = 0; i < book.length; i++) {
if (in == (i + 1)) {
return book[i].bookName + "\t序号:" + (i+1);
}
}
throw new MyException("下标越界,请重试.");
}
public int scanf() {
try {
int in = scanner.nextInt();
return in;
} catch(Exception e) {
scanner = new Scanner(System.in);
return -1;
}
}
}
#

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×