Java写一个图书员管理系统
时间: 2023-06-13 07:02:36 浏览: 103
好的,我可以给你提供一个基本的图书员管理系统的代码示例,你可以根据自己的需求进行修改和完善。
首先,定义一个Book类来表示图书信息,包括书名、作者、出版社、价格等属性:
```java
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private double price;
// 构造函数
public Book(int id, String name, String author, String publisher, double price) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// toString方法,用于将图书信息转换为字符串
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", publisher=" + publisher + ", price="
+ price + "]";
}
}
```
然后,定义一个BookManager类来管理图书信息,包括添加图书、删除图书、修改图书、查询图书等操作:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManager {
private List<Book> bookList = new ArrayList<Book>();
// 添加图书
public void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入出版社:");
String publisher = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
int id = bookList.size() + 1; // 生成图书ID
Book book = new Book(id, name, author, publisher, price);
bookList.add(book);
System.out.println("添加成功!");
}
// 删除图书
public void deleteBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的图书ID:");
int id = scanner.nextInt();
boolean flag = false;
for (Book book : bookList) {
if (book.getId() == id) {
bookList.remove(book);
flag = true;
break;
}
}
if (flag) {
System.out.println("删除成功!");
} else {
System.out.println("没有找到该图书!");
}
}
// 修改图书
public void updateBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的图书ID:");
int id = scanner.nextInt();
boolean flag = false;
for (Book book : bookList) {
if (book.getId() == id) {
System.out.print("请输入新书名:");
String name = scanner.nextLine();
System.out.print("请输入新作者:");
String author = scanner.nextLine();
System.out.print("请输入新出版社:");
String publisher = scanner.nextLine();
System.out.print("请输入新价格:");
double price = scanner.nextDouble();
book.setName(name);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPrice(price);
flag = true;
break;
}
}
if (flag) {
System.out.println("修改成功!");
} else {
System.out.println("没有找到该图书!");
}
}
// 查询图书
public void queryBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的图书ID:");
int id = scanner.nextInt();
boolean flag = false;
for (Book book : bookList) {
if (book.getId() == id) {
System.out.println(book);
flag = true;
break;
}
}
if (!flag) {
System.out.println("没有找到该图书!");
}
}
// 显示所有图书
public void showAll() {
for (Book book : bookList) {
System.out.println(book);
}
}
}
```
最后,编写一个测试类来测试图书员管理系统:
```java
public class Test {
public static void main(String[] args) {
BookManager manager = new BookManager();
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();
switch (choice) {
case 1:
manager.addBook();
break;
case 2:
manager.deleteBook();
break;
case 3:
manager.updateBook();
break;
case 4:
manager.queryBook();
break;
case 5:
manager.showAll();
break;
case 0:
System.out.println("退出系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
这样,一个简单的图书员管理系统就完成了!
阅读全文