用Java写一个图书管理系统
时间: 2023-10-04 17:03:33 浏览: 41
好的,下面是一个简单的图书管理系统的Java代码示例,包含基本的图书信息管理和借阅还书功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Book {
private String name;
private String author;
private int id;
private boolean borrowed;
public Book(String name, String author, int id) {
this.name = name;
this.author = author;
this.id = id;
this.borrowed = false;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getId() {
return id;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
}
public class Library {
private ArrayList<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public void listBooks() {
for (Book book : books) {
System.out.println(book.getId() + ": " + book.getName() + " by " + book.getAuthor() + " ("
+ (book.isBorrowed() ? "borrowed" : "available") + ")");
}
}
public void borrowBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
if (!book.isBorrowed()) {
book.setBorrowed(true);
System.out.println("You have successfully borrowed " + book.getName());
} else {
System.out.println("Sorry, " + book.getName() + " is already borrowed.");
}
return;
}
}
System.out.println("Sorry, there is no book with id " + id);
}
public void returnBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
if (book.isBorrowed()) {
book.setBorrowed(false);
System.out.println("You have successfully returned " + book.getName());
} else {
System.out.println("Sorry, " + book.getName() + " was not borrowed.");
}
return;
}
}
System.out.println("Sorry, there is no book with id " + id);
}
}
public class Main {
public static void main(String[] args) {
Library library = new Library();
// Add some books to the library
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald", 1));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee", 2));
library.addBook(new Book("Pride and Prejudice", "Jane Austen", 3));
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 5) {
System.out.println("Select an option:");
System.out.println("1. List all books");
System.out.println("2. Borrow a book");
System.out.println("3. Return a book");
System.out.println("4. Add a book");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
library.listBooks();
break;
case 2:
System.out.println("Enter the id of the book you want to borrow:");
int borrowId = scanner.nextInt();
library.borrowBook(borrowId);
break;
case 3:
System.out.println("Enter the id of the book you want to return:");
int returnId = scanner.nextInt();
library.returnBook(returnId);
break;
case 4:
System.out.println("Enter the name of the book:");
String name = scanner.next();
System.out.println("Enter the author of the book:");
String author = scanner.next();
System.out.println("Enter the id of the book:");
int id = scanner.nextInt();
library.addBook(new Book(name, author, id));
break;
case 5:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
这个图书管理系统包含了Book类,表示一本书的基本信息,Library类,表示整个图书馆,以及一个Main类,作为程序入口,提供了菜单选项,可以进行图书列表、添加、借阅和归还等操作。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)