java小型图书馆管理系统
时间: 2023-12-18 08:29:29 浏览: 51
以下是一个Java小型图书馆管理系统的例子:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("请选择操作:");
System.out.println("1.添加书籍");
System.out.println("2.删除书籍");
System.out.println("3.查看书籍");
System.out.println("4.退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
Book book = new Book(name, author, price);
books.add(book);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的书籍的编号:");
int index = scanner.nextInt();
if (index >= 0 && index < books.size()) {
books.remove(index);
System.out.println("删除成功!");
} else {
System.out.println("输入有误!");
}
break;
case 3:
System.out.println("书籍列表:");
for (int i = 0; i < books.size(); i++) {
System.out.println(i + ". " + books.get(i));
}
break;
case 4:
System.out.println("退出系统!");
break;
default:
System.out.println("输入有误!");
break;
}
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "书名:" + name + ",作者:" + author + ",价格:" + price;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)