用Java 语言设计一个无图形界面的图书管理系统
时间: 2024-02-09 18:10:08 浏览: 66
好的,以下是用Java语言设计的无图形界面的图书管理系统:
1. 定义一个Book类,包含书名、作者、出版社、出版日期、ISBN、价格等属性,并提供相应的get/set方法。
2. 定义一个BookManager类,包含添加书籍、删除书籍、修改书籍信息、查询书籍、显示所有书籍、保存数据、加载数据等方法。具体实现如下:
```java
import java.io.*;
import java.util.*;
public class BookManager {
private List<Book> books;
private File file;
public BookManager() {
books = new ArrayList<>();
file = new File("books.txt");
load();
}
public void addBook(Book book) {
books.add(book);
}
public void deleteBook(String isbn) {
Iterator<Book> iterator = books.iterator();
while (iterator.hasNext()) {
Book book = iterator.next();
if (book.getIsbn().equals(isbn)) {
iterator.remove();
break;
}
}
}
public void updateBook(String isbn, Book book) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getIsbn().equals(isbn)) {
books.set(i, book);
break;
}
}
}
public Book searchBook(String keyword) {
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword) || book.getPublisher().contains(keyword)) {
return book;
}
}
return null;
}
public List<Book> getAllBooks() {
return books;
}
public void save() {
try (PrintWriter writer = new PrintWriter(new FileWriter(file))) {
for (Book book : books) {
writer.println(book.getTitle() + "," + book.getAuthor() + "," + book.getPublisher() + "," + book.getPublicationDate() + "," + book.getIsbn() + "," + book.getPrice());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void load() {
if (file.exists()) {
try (Scanner scanner = new Scanner(new FileInputStream(file))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(",");
String title = fields[0];
String author = fields[1];
String publisher = fields[2];
String publicationDate = fields[3];
String isbn = fields[4];
double price = Double.parseDouble(fields[5]);
Book book = new Book(title, author, publisher, publicationDate, isbn, price);
books.add(book);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
3. 定义一个BookTest类,用于测试BookManager类的各个方法。具体实现如下:
```java
public class BookTest {
public static void main(String[] args) {
BookManager manager = new BookManager();
Book book1 = new Book("Java从入门到精通", "张三", "清华大学出版社", "2021-01-01", "978-7-302-12345-6", 59.9);
Book book2 = new Book("Java编程思想", "李四", "机械工业出版社", "2020-03-01", "978-7-121-18888-8", 89.9);
Book book3 = new Book("Java核心技术", "王五", "人民邮电出版社", "2020-07-01", "978-7-115-48282-2", 69.9);
manager.addBook(book1);
manager.addBook(book2);
manager.addBook(book3);
System.out.println("添加书籍后的所有书籍:");
List<Book> books = manager.getAllBooks();
for (Book book : books) {
System.out.println(book);
}
System.out.println("删除一本书籍后的所有书籍:");
manager.deleteBook("978-7-302-12345-6");
books = manager.getAllBooks();
for (Book book : books) {
System.out.println(book);
}
System.out.println("修改一本书籍信息后的所有书籍:");
Book newBook = new Book("Java从入门到精通(第2版)", "张三", "清华大学出版社", "2021-07-01", "978-7-302-12345-6", 69.9);
manager.updateBook("978-7-302-12345-6", newBook);
books = manager.getAllBooks();
for (Book book : books) {
System.out.println(book);
}
System.out.println("查询一本书籍:");
Book searchedBook = manager.searchBook("Java编程思想");
System.out.println(searchedBook);
manager.save();
}
}
```
以上就是用Java语言设计的无图形界面的图书管理系统。
阅读全文