java图书管理系统项目控制台
时间: 2023-12-17 11:03:49 浏览: 85
java控制台输出图书馆管理系统
以下是一个Java控制台输出图书馆管理系统的示例代码,该代码不使用数据库和GUI等,仅供参考:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LibrarySystem {
private static ArrayList<Book> bookList = new ArrayList<Book>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
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();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
displayAllBooks();
break;
case 5:
System.out.println("感谢使用图书馆管理系统,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private static void addBook() {
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);
bookList.add(book); System.out.println("添加成功!");
}
private static void deleteBook() {
System.out.println("请输入要删除的图书名称:");
String name = scanner.next();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getName().equals(name)) {
bookList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该图书!");
}
private static void searchBook() {
System.out.println("请输入要查找的图书名称:");
String name = scanner.next();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getName().equals(name)) {
System.out.println(bookList.get(i));
return;
}
}
System.out.println("未找到该图书!");
}
private static void displayAllBooks() {
for (int i = 0; i < bookList.size(); i++) {
System.out.println(bookList.get(i));
}
}
}
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;
}
}
```
阅读全文