Book.java
import java.util.List;
import java.util.Scanner;
public class Book {
private String name;
private String author;
Scanner scanner = new Scanner(System.in);
// List booklist = new ArrayList(); 这句没有 因为如果有了 我book中函数查的booklist和主函数是两个list
public boolean select(String findname, List booklist) {
boolean a = true;
for (Object book : booklist) {
if (((Book)book).name.equals(findname)) {
a = false;
System.out.println("找到了书籍!");
return !a;
}
}
if (a) {
System.out.println("此书不存在!");
return !a;
} else {
return a;
}
}
public Book() {
}
public Book(String name, String author) {
this.name = name;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
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;
}
}
MainClass.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Book booksystem = new Book();
List booklist = new ArrayList(); //新建booklist
while (true){
System.out.println("---------------图书管理系统---------------");
System.out.println("1. 增加图书");
System.out.println("2. 查找图书");
System.out.println("3. 修改图书");
System.out.println("4. 删除图书");
System.out.println("5. 列出所有图书");
System.out.println("6. 退出系统");
System.out.println("----------------------------------------");
System.out.println("请输入选择的功能:");
int i = scanner.nextInt();
if (i == 1){
System.out.println("请输入新的书名");
String bookname = scanner.next(); //获取输入的书名
System.out.println("请输入书的作者");
String bookauthor = scanner.next(); //获取输入的作者名
Book newbook = new Book(bookname,bookauthor);
booklist.add(newbook);
System.out.println("添加成功");
} else if (i == 2){
System.out.println("请输入想要查找的书名");
String findname = scanner.next(); //获取想要查找的名
booksystem.select(findname,booklist);
} else if (i == 3){
System.out.println("请输入想要修改的书名");
String wantname = scanner.next();
boolean bool = booksystem.select(wantname,booklist);
if (bool){
for (Book book : booklist) {
if (book.getName().equals(wantname)){
System.out.println("请输入图书的新名字:");
String altername = scanner.next();
book.setName(altername);
System.out.println("此书已修改!!");
break;
}
}
}
}
else if (i == 4){
System.out.println("请输入想要删除的书名");
String deletename = scanner.next();
boolean bool = booksystem.select(deletename,booklist);
if (bool){
for (Book book : booklist) {
if (book.getName().equals(deletename)){
booklist.remove(book);
System.out.println("此书已删除!!");
break;
}
}
}
}
else if (i == 5){
for (Object o : booklist) {
System.out.println(o);
}
} else if (i == 6){
System.out.println("----------您已退出系统----------");
break;
}
}
}
}
删除的标准写法:迭代器
我的写法 不规范 会有问题 建议使用迭代器
public void deleteBooks() {
System.out.println("请输入要删除的书籍名:");
String delName = this.input.next();
Iterator bookIterator = this.bookList.iterator();
while(bookIterator.hasNext()) {
Book book = (Book)bookIterator.next();
if (book.getName().equals(delName)) {
System.out.println("找到图书");
bookIterator.remove();
System.out.println("成功删除!");
}
}
}
样例测试
添加
查找
修改
删除
列出
退出
大仁哥的样例jar包
链接:https://pan.baidu.com/s/1zaZ6wvN0BT71svERI0d_0g
提取码:8der
jar包的反编译器
链接:https://pan.baidu.com/s/1GMi1ZVM4y02n_Pjp3s4nhg
提取码:duva
操作方式:解压后双击exe文件打开——文件——反编译jar——选择jar包