使用java语言写一个图书管管理系统
时间: 2023-09-01 22:13:28 浏览: 80
用java写的一个图书管理系统
5星 · 资源好评率100%
好的,下面是一个简单的图书管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private ArrayList<Book> books;
public BookManagementSystem() {
books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public void displayAllBooks() {
System.out.println("All books:");
for (Book book : books) {
System.out.println(book);
}
}
public void displayAvailableBooks() {
System.out.println("Available books:");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println(book);
}
}
}
public void displayBorrowedBooks() {
System.out.println("Borrowed books:");
for (Book book : books) {
if (!book.isAvailable()) {
System.out.println(book);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BookManagementSystem system = new BookManagementSystem();
boolean running = true;
while (running) {
System.out.println("Commands:");
System.out.println("1. Add book");
System.out.println("2. Remove book");
System.out.println("3. Display all books");
System.out.println("4. Display available books");
System.out.println("5. Display borrowed books");
System.out.println("6. Exit");
int command = scanner.nextInt();
scanner.nextLine();
switch (command) {
case 1:
System.out.println("Enter book title:");
String title = scanner.nextLine();
System.out.println("Enter book author:");
String author = scanner.nextLine();
system.addBook(new Book(title, author));
break;
case 2:
System.out.println("Enter book title:");
title = scanner.nextLine();
System.out.println("Enter book author:");
author = scanner.nextLine();
Book bookToRemove = new Book(title, author);
if (system.books.contains(bookToRemove)) {
system.removeBook(bookToRemove);
} else {
System.out.println("Book not found!");
}
break;
case 3:
system.displayAllBooks();
break;
case 4:
system.displayAvailableBooks();
break;
case 5:
system.displayBorrowedBooks();
break;
case 6:
running = false;
break;
default:
System.out.println("Invalid command!");
break;
}
}
}
}
class Book {
private String title;
private String author;
private boolean available;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.available = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
@Override
public String toString() {
return title + " by " + author + " (" + (available ? "available" : "borrowed") + ")";
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
return false;
}
if ((this.author == null) ? (other.author != null) : !this.author.equals(other.author)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + (this.title != null ? this.title.hashCode() : 0);
hash = 31 * hash + (this.author != null ? this.author.hashCode() : 0);
return hash;
}
}
```
这是一个基本的命令行界面的图书管理系统,你可以根据需要进行修改和扩展。
阅读全文