图书管理系统
实现一个图书管理系统(控制台),支持以下功能:保存书籍信息(要求持久化),查询、添加、删除、修改书籍信息。
private static List<Book> LIST ;
public static void main(String[] args) {
System.out.println("正在初始化图书管理系统...");
load();
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("================================");
switch (scanner.nextInt()){
case 1:
insert(scanner);
break;
case 2:
list();
break;
case 3:
delete(scanner);
break;
case 4:
modify(scanner);
break;
case 5:
System.out.println("正在保存数据...");
save();
System.out.println("感谢使用,再见!");
return;
}
}
}
//导入
@SuppressWarnings("unchecked")
private static void load(){
File file = new File("data");
if(file.exists()){
try (ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file))){
LIST=(List<Book>) stream.readObject();
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}else{
LIST = new LinkedList<>();
}
}
//保存
private static void save(){
try (ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("data"))){
stream.writeObject(LIST);
stream.flush();
}catch (IOException e){
e.printStackTrace();
}
}
//插入
private static void insert(Scanner scanner){
scanner.nextLine();
System.out.print("请输入书籍标题");
String title = scanner.nextLine();
System.out.print("请输入书籍作者");
String author = scanner.nextLine();
System.out.print("请输入书籍价格");
int price = scanner.nextInt();
scanner.nextLine();
Book book = new Book(title,author,price);
LIST.add(book);
System.out.println("书籍信息添加成功:"+book);
}
//修改
private static void modify(Scanner scanner){
scanner.nextLine();
System.out.println("请输入你要修改的书籍ID编号:");
int index = scanner.nextInt();
scanner.nextLine();
while (index>LIST.size() -1||index<0) {
System.out.println("没有对应书籍,请重新输入:");
index = scanner.nextInt();
scanner.nextLine();
}
Book book = LIST.get(index);
System.out.print("请输入书籍的标题:");
book.setTitle(scanner.nextLine());
System.out.print("请输入书籍的作者:");
book.setAuthor(scanner.nextLine());
System.out.print("请输入书籍的价格:");
book.setPrice(scanner.nextInt());
scanner.nextLine();
System.out.println("书籍信息修改成功");
}
private static void list(){
for (int i = 0; i < LIST.size(); i++) {
System.out.println(i+"."+LIST.get(i));
}
}
//删除
private static void delete(Scanner scanner){
scanner.nextLine();
System.out.print("请输入你要删除的书籍ID编号:");
int index = scanner.nextInt();
scanner.nextLine();
while (index>LIST.size() -1||index<0) {
System.out.println("没有对应书籍,请重新输入:");
index = scanner.nextInt();
scanner.nextLine();
}
LIST.remove(index);
System.out.println("删除书籍信息成功");
}
}
public class Book implements Serializable {
private String title;
private String author;
private int price;
Book(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}
public void setAuthor(String author){
this.author=author;
}
public void setPrice(int price){
this.price=price;
}
public void setTitle(String title){
this.title=title;
}
@Override
public String toString() {
return "《"+title+"》作者:"+author+" ("+price+"$)";
}
}