图书管理系统
基于于Collection集合完成图书管理系统
图书信息:图书名称、图书编号、图书简介、图书作者、出版日期、图书价格。
模块:
- 管理员登录
- 图书管理
- 图书增加
- 图书修改
- 图书删除
- 根据书名模糊查找
- 查询所有图书(3种排序方式)
- 价格从高到底排序
- 价格从低到高排序
- 新旧排序(出版日期排序)
实现代码:
图书类
//Book类
public class Book {
//图书名称
private String bName;
//图书编号
private int bNo;
//图书简介
private String introduce;
//图书作者
private String author;
//出版日期(类型可用date代替,输入String类型,使用dateFormat类使用parse方法进行格式化,返回date类保存)
private int date;
//图书价格
private int money;
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public int getbNo() {
return bNo;
}
public void setbNo(int bNo) {
this.bNo = bNo;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
//重写toString方法,用于返回内容
@Override
public String toString() {
return "Book{" +
"bName='" + bName + '\'' +
", bNo=" + bNo +
", introduce='" + introduce + '\'' +
", author='" + author + '\'' +
", date=" + date +
", money=" + money +
'}';
}
}
图书操作类
public class ControlBooks {
private ArrayList<Book> arrayList = new ArrayList<>();
//添加图书
public void addBook() {
//返回Book类型,直接添加至ArrayList中
Book book = Book();
arrayList.add(book);
}
//删除图书
public void deleteBook() {
//flag为判断是否找到需要删除的图书,找到则true。
boolean flag = false;
Scanner scanner = new Scanner(System.in);
System.out.println("输入删除的图书编号");
int bno = scanner.nextInt();
//迭代器,用于遍历ArrayList集合。
Iterator<Book> iterator = arrayList.iterator();
while (iterator.hasNext()) {
int temp = iterator.next().getbNo();
//判断是否为需要删除的编号
if (temp == bno) {
flag = true;
iterator.remove();
}
}
if (flag) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
//修改图书
public void updateBook() {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("请输入需要修改的图书编号");
int bno = scanner.nextInt();
//迭代器,用于遍历ArrayList集合
ListIterator<Book> iterator = arrayList.listIterator();
//循环至集合结束。hasNext()为判断下一个是否为空
while (iterator.hasNext()) {
//temp为临时存放编号的变量
int temp = iterator.next().getbNo();
if (temp == bno) {
Book book = new Book();
System.out.println("请输入书名");
book.setbName(scanner.next());
//默认编号为唯一,不可更改
book.setbNo(bno);
System.out.println("请输入简介");
book.setIntroduce(scanner.next());
System.out.println("请输入作者");
book.setAuthor(scanner.next());
System.out.println("请输入出版日期(20200713:");
book.setDate(scanner.nextInt());
System.out.println("请输入图书价格");
book.setMoney(scanner.nextInt());
//将Book修改至迭代器当前对象
iterator.set(book);
return;
}
}
}catch (InputMismatchException e){
System.out.println("输入错误");
updateBook();
return;
}
}
//创建book
public Book Book() {
try {
Book book = new Book();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名");
book.setbName(scanner.next());
System.out.println("请输入编号");
book.setbNo(scanner.nextInt());
System.out.println("请输入简介");
book.setIntroduce(scanner.next());
System.out.println("请输入作者");
book.setAuthor(scanner.next());
System.out.println("请输入出版日期(20200713:");
book.setDate(scanner.nextInt());
System.out.println("请输入图书价格");
book.setMoney(scanner.nextInt());
return book;
}catch (InputMismatchException e){
System.out.println("输入错误");
return Book();
}
}
//价格排序,从高到底
public void maxFindSortAll() {
//使用自定义Comparator对集合进行排序
Collections.sort(arrayList,new MaxComparator());
Iterator<Book> iterator = arrayList.iterator();
//遍历并输出
while (iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
//价格排序,从低到高
public void minFindSortAll(){
//使用自定义Comparator对集合进行排序
Collections.sort(arrayList,new MinComparator());
Iterator<Book> iterator = arrayList.iterator();
//遍历并输出
while (iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
//时间排序
public void dataFindSortAll(){
//使用自定义Comparator对集合进行排序
Collections.sort(arrayList,new DateComparator());
Iterator<Book> iterator = arrayList.iterator();
//遍历并输出
while (iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
//模糊查找
public void findBook(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名");
//用于临时存放书名的变量
String temp = scanner.next();
Iterator<Book> iterator = arrayList.iterator();
//对集合进行遍历
while (iterator.hasNext()){
Book book = iterator.next();
//调用String类中的contains方法判断内容是否存在temp的内容
if (book.getbName().contains(temp)){
System.out.println(book.toString());
}
}
}
}
自定义排序方法:
- 从高到底排序
//定义MaxComparator方法,继承Comparatro类,对Book进行比较,对compare方法进行重写。
public class MaxComparator implements Comparator<Book> {
@Override
public int compare(Book o1, Book o2) {
if (o1.getMoney() < o2.getMoney()) {
return 1;
}
if (o1.getMoney() == o2.getMoney()) {
return 0;
}
return -1;
}
}
- 价格从低到高排序
//定义MinComparator方法,继承Comparatro类,对Book进行比较,对compare方法进行重写。
public class MinComparator implements Comparator<Book> {
@Override
public int compare(Book o1, Book o2) {
if (o1.getMoney() > o2.getMoney()) {
return 1;
} else if (o1.getMoney() == o2.getMoney()) {
return 0;
}
return -1;
}
}
- 新旧排序(出版日期排序)
//定义DateComparator方法,继承Comparatro类,对Book进行比较,对compare方法进行重写。
public class DateComparator implements Comparator<Book> {
@Override
public int compare(Book o1, Book o2) {
if (o1.getDate() > o2.getDate()) {
return 1;
} else if ((o1.getDate() == o2.getDate())) {
return 0;
}
return -1;
}
}
如使用date作为日期存储,则需要将date类转换为时间戳后,在进行比较。
操作界面
public class BookTest {
private static ControlBooks controlBooks = new ControlBooks();
private static User user = new User("root","root");
//操作面板
public static void Books() {
Scanner scanner = new Scanner(System.in);
try {
loop:while (true) {
System.out.println("请输入选择 1.图书增加 2.图书修改 3.图书删除 4.图书查找 5.查询(价格高到底) 6.查询(价格低到高) 7.查询(日期) 8.退出");
int number = scanner.nextInt();
switch (number) {
case 1:controlBooks.addBook();break;
case 2:controlBooks.updateBook();break;
case 3:controlBooks.deleteBook();break;
case 4:controlBooks.findBook();break;
case 5:controlBooks.maxFindSortAll();break;
case 6:controlBooks.minFindSortAll();break;
case 7:controlBooks.dataFindSortAll();break;
case 8:break loop;
default:
System.out.println("输入错误");
}
}
}catch (InputMismatchException e){
System.out.println("输入错误");
Books();
return;
}
}
//登录界面 账号 root 密码root
public static void Login(){
while (true){
Scanner scanner = new Scanner(System.in);
User user1 = new User();
System.out.println("请输入账号");
user1.setAccount(scanner.next());
System.out.println("请输入密码");
user1.setPasswork(scanner.next());
if (user.equals(user1)){
Books();
return;
}else {
System.out.println("账号或密码错误");
}
}
}
public static void main(String[] args) {
Login();
}
}