写一个图书管理系统java代码,要简单且有增加,删除,查询,修改四个功能;
时间: 2024-03-20 21:42:03 浏览: 105
好的,以下是一个简单的图书管理系统Java代码,包含增加、删除、查询和修改四个功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
private static ArrayList<Book> bookList = new ArrayList<Book>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// 初始化图书列表
bookList.add(new Book("001", "Java程序设计", "张三", "清华大学出版社", 59.9));
bookList.add(new Book("002", "数据结构与算法", "李四", "人民邮电出版社", 49.9));
bookList.add(new Book("003", "数据库系统原理", "王五", "机械工业出版社", 69.9));
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. 退出系统");
int choice = scanner.nextInt();
scanner.nextLine(); // 清空缓冲区
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
updateBook();
break;
case 5:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
// 添加图书
private static void addBook() {
System.out.println("请输入添加的图书信息:");
System.out.print("编号:");
String id = scanner.nextLine();
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();
scanner.nextLine(); // 清空缓冲区
Book book = new Book(id, name, author, publisher, price);
bookList.add(book);
System.out.println("添加成功!");
}
// 删除图书
private static void deleteBook() {
System.out.println("请输入要删除图书的编号:");
String id = scanner.nextLine();
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
if (book.getId().equals(id)) {
bookList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到对应的图书!");
}
// 查询图书
private static void searchBook() {
System.out.println("请输入要查询图书的编号:");
String id = scanner.nextLine();
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println(book);
return;
}
}
System.out.println("没有找到对应的图书!");
}
// 修改图书信息
private static void updateBook() {
System.out.println("请输入要修改图书的编号:");
String id = scanner.nextLine();
for (Book book : bookList) {
if (book.getId().equals(id)) {
System.out.println("请输入修改后的图书信息:");
System.out.print("书名(原书名:" + book.getName() + "):");
String name = scanner.nextLine();
System.out.print("作者(原作者:" + book.getAuthor() + "):");
String author = scanner.nextLine();
System.out.print("出版社(原出版社:" + book.getPublisher() + "):");
String publisher = scanner.nextLine();
System.out.print("价格(原价格:" + book.getPrice() + "):");
double price = scanner.nextDouble();
scanner.nextLine(); // 清空缓冲区
book.setName(name);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPrice(price);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到对应的图书!");
}
}
class Book {
private String id;
private String name;
private String author;
private String publisher;
private double price;
public Book(String id, String name, String author, String publisher, double price) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public String getId() {
return 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;
}
@Override
public String toString() {
return "编号:" + id + ",书名:" + name + ",作者:" + author + ",出版社:" + publisher + ",价格:" + price;
}
}
```
使用方法:
1. 将代码保存到名为 `BookManagementSystem.java` 的文件中
2. 在命令行中进入代码所在的目录,执行 `javac BookManagementSystem.java` 编译代码
3. 执行 `java BookManagementSystem` 运行程序,按照提示进行操作
阅读全文