用java写图书管理系统
时间: 2023-11-17 19:07:24 浏览: 100
好的,下面是一个简单的图书管理系统的 Java 代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
// 图书类
public static class Book {
private String title;
private String author;
private int numCopies;
public Book(String title, String author, int numCopies) {
this.title = title;
this.author = author;
this.numCopies = numCopies;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getNumCopies() {
return numCopies;
}
public void setNumCopies(int numCopies) {
this.numCopies = numCopies;
}
}
// 图书馆类
public static class Library {
private ArrayList<Book> books;
public Library(ArrayList<Book> books) {
this.books = books;
}
public ArrayList<Book> getBooks() {
return books;
}
// 添加图书
public void addBook(Book book) {
books.add(book);
}
// 删除图书
public void removeBook(Book book) {
books.remove(book);
}
// 显示所有图书
public void displayBooks() {
System.out.println("图书列表:");
for (Book book : books) {
System.out.println(book.getTitle() + " - " + book.getAuthor() + " - " + book.getNumCopies() + "本");
}
}
// 借出图书
public void borrowBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
if (book.getNumCopies() > 0) {
book.setNumCopies(book.getNumCopies() - 1);
System.out.println("借书成功!");
return;
} else {
System.out.println("该书已全部借出!");
return;
}
}
}
System.out.println("未找到该书!");
}
// 归还图书
public void returnBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
book.setNumCopies(book.getNumCopies() + 1);
System.out.println("还书成功!");
return;
}
}
System.out.println("未找到该书!");
}
}
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<>();
books.add(new Book("Java编程思想", "Bruce Eckel", 2));
books.add(new Book("Java核心技术", "Cay S. Horstmann", 3));
Library library = new Library(books);
Scanner scanner = new Scanner(System.in);
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. 归还图书");
System.out.println("0. 退出程序");
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("请输入图书数量:");
int numCopies = scanner.nextInt();
scanner.nextLine(); // 处理掉输入缓冲区中的回车符
library.addBook(new Book(title, author, numCopies));
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的图书名称:");
String titleToDelete = scanner.nextLine();
for (Book book : library.getBooks()) {
if (book.getTitle().equals(titleToDelete)) {
library.removeBook(book);
System.out.println("删除成功!");
break;
}
}
System.out.println("未找到该书!");
break;
case 3:
library.displayBooks();
break;
case 4:
System.out.println("请输入要借出的图书名称:");
String titleToBorrow = scanner.nextLine();
library.borrowBook(titleToBorrow);
break;
case 5:
System.out.println("请输入要归还的图书名称:");
String titleToReturn = scanner.nextLine();
library.returnBook(titleToReturn);
break;
case 0:
System.exit(0);
default:
System.out.println("无效输入,请重新输入!");
break;
}
}
}
}
```
这是一个简单的控制台程序,可以通过输入数字来选择不同的操作。其中,图书类和图书馆类都是内部类,可以根据需要进行修改和扩展。
阅读全文