java书馆管理系统代码
javaimport java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private int year;
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Year: " + year;
}
}
class Library {
private ArrayList<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void displayBooks() {
System.out.println("Books available in the library:");
for (Book book : books) {
System.out.println(book);
}
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
boolean exit = false;
while (!exit) {
System.out.println("\nLibrary Management System Menu:");
System.out.println("1. Add Book");
System.out.println("2. Display Books");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter title: ");
scanner.nextLine(); // Consume newline
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter year: ");
int year = scanner.nextInt();
library.addBook(new Book(title, author, year));
System.out.println("Book added successfully!");
break;
case 2:
library.displayBooks();
break;
case 3:
exit = true;
break;
default:
System.out.println("Invalid choice! Please enter again.");
}
}
System.out.println("Thank you for using the Library Management System.");
scanner.close();
}
}
这个简单的图书馆管理系统允许用户添加书籍,显示库中的所有书籍,并退出系统。您可以根据需要扩展和修改此代码。
删除书籍功能:允许用户从图书馆中删除书籍。搜索书籍功能:允许用户按标题、作者或年份搜索书籍。保存和加载数据:允许将图书馆数据保存到文件中,并在启动时加载已保存的数据。书籍借阅和归还功能:为书籍添加借阅状态,允许用户借阅和归还书籍。用户身份验证:允许管理员和用户登录,管理员可以进行更高级的操作。图形用户界面:使用Swing或JavaFX等库创建一个图形用户界面,使用户能够更轻松地与系统交互。
javaimport java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
class Book {
private String title;
private String author;
private int year;
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Year: " + year;
}
}
class Library {
private ArrayList<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(String title) {
books.removeIf(book -> book.getTitle().equalsIgnoreCase(title));
}
public void displayBooks() {
System.out.println("Books available in the library:");
for (Book book : books) {
System.out.println(book);
}
}
public void saveLibraryToFile(String filename) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(books);
System.out.println("Library data saved successfully!");
} catch (IOException e) {
System.out.println("Error saving library data: " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
public void loadLibraryFromFile(String filename) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
books = (ArrayList<Book>) ois.readObject();
System.out.println("Library data loaded successfully!");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading library data: " + e.getMessage());
}
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
boolean exit = false;
while (!exit) {
System.out.println("\nLibrary Management System Menu:");
System.out.println("1. Add Book");
System.out.println("2. Remove Book");
System.out.println("3. Display Books");
System.out.println("4. Save Library to File");
System.out.println("5. Load Library from File");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter title: ");
scanner.nextLine(); // Consume newline
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter year: ");
int year = scanner.nextInt();
library.addBook(new Book(title, author, year));
System.out.println("Book added successfully!");
break;
case 2:
System.out.print("Enter title of the book to remove: ");
scanner.nextLine(); // Consume newline
String titleToRemove = scanner.nextLine();
library.removeBook(titleToRemove);
System.out.println("Book removed successfully!");
break;
case 3:
library.displayBooks();
break;
case 4:
library.saveLibraryToFile("library.dat");
break;
case 5:
library.loadLibraryFromFile("library.dat");
break;
case 6:
exit = true;
break;
default:
System.out.println("Invalid choice! Please enter again.");
}
}
System.out.println("Thank you for using the Library Management System.");
scanner.close();
}
}