java练习图书借阅管理系统
时间: 2023-10-17 17:05:10 浏览: 40
好的,下面是一个简单的Java图书借阅管理系统的代码示例,仅供参考:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Book> books = new ArrayList<Book>();
int choice = 0;
while (choice != 5) {
System.out.println("1. Add a book");
System.out.println("2. Search a book");
System.out.println("3. Display all books");
System.out.println("4. Remove a book");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = input.next();
System.out.print("Enter book author: ");
String author = input.next();
System.out.print("Enter book ISBN: ");
String isbn = input.next();
System.out.print("Enter book publisher: ");
String publisher = input.next();
Book book = new Book(title, author, isbn, publisher);
books.add(book);
break;
case 2:
System.out.print("Enter book title to search: ");
String searchTitle = input.next();
boolean found = false;
for (Book b : books) {
if (b.getTitle().equals(searchTitle)) {
System.out.println(b.toString());
found = true;
break;
}
}
if (!found) {
System.out.println("Book not found!");
}
break;
case 3:
for (Book b : books) {
System.out.println(b.toString());
}
break;
case 4:
System.out.print("Enter book title to remove: ");
String removeTitle = input.next();
boolean removed = false;
for (Book b : books) {
if (b.getTitle().equals(removeTitle)) {
books.remove(b);
System.out.println("Book removed successfully!");
removed = true;
break;
}
}
if (!removed) {
System.out.println("Book not found!");
}
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
class Book {
private String title;
private String author;
private String isbn;
private String publisher;
public Book(String title, String author, String isbn, String publisher) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public String getPublisher() {
return publisher;
}
public String toString() {
return "Title: " + title + "\nAuthor: " + author + "\nISBN: " + isbn + "\nPublisher: " + publisher + "\n";
}
}
```
这个简单的图书借阅管理系统允许你添加图书、搜索图书、显示所有图书和删除图书。你可以根据自己的需求对代码进行修改和扩展。