图书管理系统
程序主入口
public class SystemEntry {
public static void main(String[] args) {
BookOperator operator = new BookOperator();
Scanner sc = 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("请输入您的选择:");
String choice = sc.next();
switch (choice) {
case "1":
operator.addBook();
break;
case "2":
operator.delBook();
break;
case "3":
operator.updeteBook();
break;
case "4":
operator.findAllBooks();
break;
case "5":
System.out.println("欢迎下次再来");
System.exit(0);//退出
default:
System.out.println("输入有误!!");
break;
}
}
}
}
封装图书数据的类
public class Book {
private String id;
private String name;
private double price;
private String description;
public Book() {
}
public Book(String id, String name, double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
}
操作图书的功能类
public class BookOperator {
private ArrayList<Book> books = new ArrayList<>();
private Scanner sc = new Scanner(System.in);
public void addBook() {
String id = null;
while (true) {
System.out.println("请输入图书id");
id = sc.next();
int index = findIndexById(id);
if (index < 0) {
break;
} else {
System.out.println("输入的图书id重复,请重新输入!!");
}
}
System.out.println("请输入书名");
String name = sc.next();
System.out.println("请输入价格");
double price = sc.nextDouble();
System.out.println("请输入图书描述");
String description = sc.next();
Book book = new Book(id, name, price, description);
books.add(book);
System.out.println("添加成功!!");
}
public void delBook() {
System.out.println("请输入图书id");
String id = sc.next();
int index = findIndexById(id);
if (index < 0) {
System.out.println("删除的图书不存在,请重新输入");
} else {
books.remove(index);
System.out.println("删除成功!!");
}
}
public void updeteBook() {
System.out.println("请输入图书id");
String id = sc.next();
int index = findIndexById(id);
if (index < 0) {
System.out.println("更新的图书不存在,请重新输入");
} else {
System.out.println("请输入书名");
String name = sc.next();
System.out.println("请输入价格");
double price = sc.nextDouble();
System.out.println("请输入图书描述");
String description = sc.next();
Book book = new Book(id, name, price, description);
books.set(index, book);
System.out.println("更新成功!!");
}
}
private int findIndexById(String id) {
for (int i = 0; i < books.size(); i++) {
if (id.equals(books.get(i).getId())) {
return i;
}
}
return -1;
}
public void findAllBooks() {
if (books == null || books.size() == 0) {
System.out.println("图书为空,请先添加");
return;
}
System.out.println("图书id\t书名\t价格\t描述");
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
System.out.println(book.getId() + "\t" + book.getName() + "\t" +
book.getPrice() + "元\t" + book.getDescription());
}
}
}