图书管理系统java
时间: 2023-07-09 20:05:48 浏览: 86
好的,你需要一个图书管理系统的Java代码是吗?那么我们可以一步一步来实现。
首先,你需要定义一个Book类,来表示图书的属性:
```java
public class Book {
private String title;
private String author;
private String ISBN;
private int quantity;
public Book(String title, String author, String ISBN, int quantity) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
this.quantity = quantity;
}
// Getter and setter methods for all attributes
}
```
然后,你需要创建一个BookList类,来管理所有的图书:
```java
import java.util.ArrayList;
public class BookList {
private ArrayList<Book> books;
public BookList() {
books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getTitle().equals(title)) {
result.add(book);
}
}
return result;
}
public ArrayList<Book> searchByAuthor(String author) {
ArrayList<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
}
public ArrayList<Book> searchByISBN(String ISBN) {
ArrayList<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getISBN().equals(ISBN)) {
result.add(book);
}
}
return result;
}
// Other methods to manage the book list
}
```
最后,你需要创建一个Main类,来运行整个程序:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BookList bookList = new 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. 按ISBN查找");
System.out.println("6. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入书名:");
String title = scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入ISBN:");
String ISBN = scanner.nextLine();
System.out.println("请输入数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
Book book = new Book(title, author, ISBN, quantity);
bookList.addBook(book);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入ISBN:");
ISBN = scanner.nextLine();
ArrayList<Book> result = bookList.searchByISBN(ISBN);
if (result.size() == 0) {
System.out.println("没有找到该图书!");
} else {
for (Book b : result) {
bookList.removeBook(b);
System.out.println("删除成功!");
}
}
break;
case 3:
System.out.println("请输入书名:");
title = scanner.nextLine();
result = bookList.searchByTitle(title);
if (result.size() == 0) {
System.out.println("没有找到该图书!");
} else {
for (Book b : result) {
System.out.println(b.toString());
}
}
break;
case 4:
System.out.println("请输入作者:");
author = scanner.nextLine();
result = bookList.searchByAuthor(author);
if (result.size() == 0) {
System.out.println("没有找到该图书!");
} else {
for (Book b : result) {
System.out.println(b.toString());
}
}
break;
case 5:
System.out.println("请输入ISBN:");
ISBN = scanner.nextLine();
result = bookList.searchByISBN(ISBN);
if (result.size() == 0) {
System.out.println("没有找到该图书!");
} else {
for (Book b : result) {
System.out.println(b.toString());
}
}
break;
case 6:
System.out.println("程序已退出!");
return;
default:
System.out.println("无效的选项!");
break;
}
}
}
}
```
这就是一个简单的图书管理系统的Java代码,你可以根据需求进行修改和扩展。
阅读全文