用Java语言创建一个图书管理系统使用控制台来模拟其部分功能
时间: 2023-09-16 20:13:40 浏览: 105
好的,我可以帮你解答这个问题。首先,我们需要设计一个图书类Book,包含图书的基本信息,例如书名、作者、出版社、出版日期、价格等属性。代码如下:
```java
public class Book {
private String title; // 书名
private String author; // 作者
private String publisher; // 出版社
private String publishDate; // 出版日期
private double price; // 价格
// 构造方法
public Book(String title, String author, String publisher, String publishDate, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.publishDate = publishDate;
this.price = price;
}
// getter和setter方法
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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 getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 重写toString方法
@Override
public String toString() {
return "书名:" + title + ",作者:" + author + ",出版社:" + publisher + ",出版日期:" + publishDate + ",价格:" + price;
}
}
```
接下来,我们需要设计一个图书管理系统类BookManager,包含以下功能:
1. 添加图书
2. 删除图书
3. 修改图书信息
4. 查询图书信息
5. 显示所有图书
代码如下:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManager {
private ArrayList<Book> books = new ArrayList<>();
// 添加图书
public void addBook() {
Scanner input = new Scanner(System.in);
System.out.println("请输入图书信息:");
System.out.print("书名:");
String title = input.next();
System.out.print("作者:");
String author = input.next();
System.out.print("出版社:");
String publisher = input.next();
System.out.print("出版日期:");
String publishDate = input.next();
System.out.print("价格:");
double price = input.nextDouble();
Book book = new Book(title, author, publisher, publishDate, price);
books.add(book);
System.out.println("添加成功!");
}
// 删除图书
public void deleteBook() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除的图书的书名:");
String title = input.next();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getTitle().equals(title)) {
books.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该图书!");
}
// 修改图书信息
public void modifyBook() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要修改的图书的书名:");
String title = input.next();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getTitle().equals(title)) {
Book book = books.get(i);
System.out.println("请输入修改后的图书信息:");
System.out.print("书名:");
book.setTitle(input.next());
System.out.print("作者:");
book.setAuthor(input.next());
System.out.print("出版社:");
book.setPublisher(input.next());
System.out.print("出版日期:");
book.setPublishDate(input.next());
System.out.print("价格:");
book.setPrice(input.nextDouble());
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该图书!");
}
// 查询图书信息
public void queryBook() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查询的图书的书名:");
String title = input.next();
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getTitle().equals(title)) {
System.out.println(books.get(i));
return;
}
}
System.out.println("未找到该图书!");
}
// 显示所有图书
public void displayBooks() {
if (books.size() == 0) {
System.out.println("暂无图书!");
return;
}
System.out.println("所有图书信息如下:");
for (int i = 0; i < books.size(); i++) {
System.out.println(books.get(i));
}
}
}
```
最后,我们需要在主函数中实现菜单功能,代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookManager bookManager = new BookManager();
Scanner input = 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 = input.nextInt();
switch (choice) {
case 1:
bookManager.addBook();
break;
case 2:
bookManager.deleteBook();
break;
case 3:
bookManager.modifyBook();
break;
case 4:
bookManager.queryBook();
break;
case 5:
bookManager.displayBooks();
break;
case 0:
System.out.println("谢谢使用!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
```
以上就是用Java语言创建一个图书管理系统使用控制台来模拟其部分功能的实现过程。希望对你有所帮助。
阅读全文