设计一个Java图书信息管理系统
时间: 2024-05-06 19:16:23 浏览: 137
基于spring boot -MySQL -Java实现图书信息管理系统(可作为毕业设计)
5星 · 资源好评率100%
好的,下面是一个简单的Java图书信息管理系统设计:
1. 首先,需要设计一个Book类来表示图书信息,包括书名、作者、出版社、ISBN号、价格等属性。可以使用getter和setter方法来访问这些属性。
2. 接着,设计一个BookList类来管理图书信息,包括添加图书、删除图书、查找图书等功能。可以使用ArrayList来存储图书信息。
3. 然后,设计一个Menu类来显示菜单选项,包括添加图书、删除图书、查找图书、显示所有图书、退出程序等选项。可以使用Scanner类来接收用户输入的选项。
4. 最后,设计一个主程序类来运行整个图书信息管理系统,包括创建BookList对象、创建Menu对象、显示菜单选项、根据用户输入执行相应的操作等。
以下是代码示例:
Book.java:
```
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
private double price;
public Book(String name, String author, String publisher, String isbn, double price) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author + ", publisher=" + publisher + ", isbn=" + isbn
+ ", price=" + price + "]";
}
}
```
BookList.java:
```
import java.util.ArrayList;
public class BookList {
private ArrayList<Book> bookList;
public BookList() {
bookList = new ArrayList<Book>();
}
public void addBook(Book book) {
bookList.add(book);
System.out.println("添加成功!");
}
public void removeBook(String name) {
for (Book book : bookList) {
if (book.getName().equals(name)) {
bookList.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该图书!");
}
public void searchBook(String name) {
for (Book book : bookList) {
if (book.getName().equals(name)) {
System.out.println(book);
return;
}
}
System.out.println("未找到该图书!");
}
public void displayAllBooks() {
for (Book book : bookList) {
System.out.println(book);
}
}
}
```
Menu.java:
```
import java.util.Scanner;
public class Menu {
private Scanner scanner;
public Menu() {
scanner = new Scanner(System.in);
}
public void displayMenu() {
System.out.println("图书信息管理系统");
System.out.println("1. 添加图书");
System.out.println("2. 删除图书");
System.out.println("3. 查找图书");
System.out.println("4. 显示所有图书");
System.out.println("5. 退出程序");
}
public int getChoice() {
System.out.print("请选择操作:");
int choice = scanner.nextInt();
return choice;
}
public Book getBookInfo() {
System.out.print("请输入书名:");
String name = scanner.next();
System.out.print("请输入作者:");
String author = scanner.next();
System.out.print("请输入出版社:");
String publisher = scanner.next();
System.out.print("请输入ISBN号:");
String isbn = scanner.next();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
Book book = new Book(name, author, publisher, isbn, price);
return book;
}
public String getBookName() {
System.out.print("请输入书名:");
String name = scanner.next();
return name;
}
}
```
Main.java:
```
public class Main {
public static void main(String[] args) {
BookList bookList = new BookList();
Menu menu = new Menu();
while (true) {
menu.displayMenu();
int choice = menu.getChoice();
switch (choice) {
case 1:
Book book = menu.getBookInfo();
bookList.addBook(book);
break;
case 2:
String name = menu.getBookName();
bookList.removeBook(name);
break;
case 3:
name = menu.getBookName();
bookList.searchBook(name);
break;
case 4:
bookList.displayAllBooks();
break;
case 5:
System.out.println("退出程序!");
System.exit(0);
break;
default:
System.out.println("无效的选项,请重新选择!");
break;
}
}
}
}
```
这个Java图书信息管理系统可以根据实际需要进行修改和适应,但是基本的设计框架应该是相似的。
阅读全文