1. 需求
主界面:1.添加图书; 2.查找图书; 3.修改图书; 4.删除图书; 5.显示所有图书; 6.退出系统
添加图书:1. 图书ID;2.书名; 3.作者名;4.数量
查找图书:1.根据图书ID进行查找,返回图书的图书ID、书名、作者、数量;2.根据书名查找,支持模糊查询
修改图书:输入要修改的图书编号,选择要修改的内容,输入修改内容。
删除图书:输入要删除的图书ID,删除这个图书
2. 代码
package BookDemo;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//建立一个图书类,用于描述图书信息
class Book{
private int id; //图书ID
private String name; //书名
private String author; //作者
private int num; //数量
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getNum() {
return num;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setNum(int num) {
this.num = num;
}
public String toString(){
return "图书ID:"+this.id+"; 书名:"+this.name+"; 作者名:"+this.author+"; 数量:"+this.num;
}
}
public class BookDemo_V0 {
static Scanner scanner = new Scanner(System.in); // 接受输入
public static void main(String[] args) {
ArrayList<Book> dataSet = new ArrayList<>(); //用于存放图书数据
boolean flag = true;
System.out.println("-----------------------------------------图书管理系统--------------------------------");
while (flag){
display();
int i = scanner.nextInt();
if(i == 1){ //添加图书
Book b = addBook();
dataSet.add(b);
}
else if(i == 2){ //查找图书
System.out.println("-------------查找图书-------------");
System.out.println("1.根据图书ID查找; 2.根据图书查找");
System.out.println("请选择:");
int j = scanner.nextInt();
if(j == 1){
System.out.println("输入要查找的图书ID:");
int m = scanner.nextInt();
Book b = selectById(dataSet,m);
showBook(b);
}
else if(j == 2){
System.out.println("输入要查找的书名:");
String s = scanner.next();
Book b = selectByName(dataSet,s);
showBook(b);
}
else System.out.println("其他功能暂未开发");
}
else if(i == 3) updateBook(dataSet);
else if(i == 4) deleteBook(dataSet);
else if(i == 5) showAllBook(dataSet);
else break;
}
}
//展示系统所有功能
public static void display(){
System.out.println("1.添加图书; 2.查找图书; 3.修改图书; 4.删除图书; 5.显示所有图书; 6.退出系统");
System.out.println("请选择输入的功能:");
}
//1.添加图书
public static Book addBook(){
System.out.println("-------------添加图书-------------");
Book b = new Book();
System.out.println("图书ID:");
int id = scanner.nextInt();
b.setId(id);
System.out.println("书名:");
String name = scanner.next();
b.setName(name);
System.out.println("作者名");
String author = scanner.next();
b.setAuthor(author);
System.out.println("数量:");
int num = scanner.nextInt();
b.setNum(num);
System.out.println("-------------已添加-------------");
return b;
}
//2.1查找图书,根据ID查找
public static Book selectById(ArrayList<Book> dataSet, int id){
int size = dataSet.size();
Book b = null;
for(int i=0; i<size; i++) {
if (dataSet.get(i).getId() == id){
b = dataSet.get(i);
break;
}
}
return b;
}
//2.2查找图书,根据书名查找,模糊查询
public static Book selectByName(ArrayList<Book> dataSet, String name){
Pattern pattern = Pattern.compile(name);
Book b = null;
int size = dataSet.size();
for(int i=0; i<size; i++){
Matcher matcher = pattern.matcher(dataSet.get(i).getName());
if(matcher.find()) {
b = dataSet.get(i);
break;
}
}
return b;
}
// 3.修改图书
public static void updateBook(ArrayList<Book> dataSet){
System.out.println("-------------修改图书-------------");
System.out.println("输入需要修改的图书ID:");
int m = scanner.nextInt();
Book b = selectById(dataSet,m);
System.out.println("请输入需要修改的内容:");
System.out.println("1.书名; 2.作者名; 3.数量");
int n = scanner.nextInt();
System.out.println("修改为:");
if(n == 1){
String s = scanner.next();
b.setName(s);
}
else if(n == 2) {
String s = scanner.next();
b.setAuthor(s);
}
else{
int t = scanner.nextInt();
b.setNum(t);
}
System.out.println("-------------已修改-------------");
}
//4.删除图书
public static void deleteBook(ArrayList<Book> dataSet){
System.out.println("-------------删除图书-------------");
System.out.println("输入需要删除的图书ID:");
int m = scanner.nextInt();
Book b = selectById(dataSet,m);
dataSet.remove(b);
System.out.println("-------------已删除-------------");
}
public static void showBook(Book b){
if(b == null){
System.out.println("没有找到!!!");
System.out.println("-----------------------------------------");
return;
}
System.out.println(b);
System.out.println("-----------------------------------------");
}
public static void showAllBook(ArrayList<Book> dataSet){
System.out.println("---------------显示所有图书---------------");
int size = dataSet.size();
if(size == 0){
System.out.println("数据库中没有图书!!!");
return;
}
for(int i=0; i<size; i++) showBook(dataSet.get(i));
}
}