十分基础的图书馆管理系统,适合初学Java的同学们。
实现了增删改查的功能,并且可以通过对象流对数据进行存取,让每次存入的数据在下次打开程序的时候也能够读取出来,以此实现数据持久化。
import java.io.*;
import java.util.List;
import java.util.Scanner;
import java.util.LinkedList;
public class Main{
private static List<Book> LIST; //Java的常用容器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");//创建一个名为”data“的文件
if(file.exists()){ //判断这个文件是否存在
try(ObjectInputStream stream =new ObjectInputStream(new FileInputStream(file))){ //
LIST =(List<Book>) stream.readObject(); // 把steam中的数据读取到LIST中
}catch (IOException | ClassNotFoundException e){ //如果出现异常则捕捉异常
e.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。
}
} else {
LIST = new LinkedList<>(); //如果这个文件不存在的话 则创建一个新的List双链表
}
}
private static void save(){
try(ObjectOutputStream stream =new ObjectOutputStream(new FileOutputStream("data"))){//
stream.writeObject(LIST); //将双链表中存储的数据写入文件中
}catch (IOException e){ //捕捉异常
e.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。
}
}
private static void insert(Scanner scanner){ //插入函数 将书籍的信息插入list中
scanner.nextLine(); //吸收回车
System.out.println("请输入书籍的标题: ");
String title = scanner.nextLine();
System.out.println("请输入书记的作者: ");
String author = scanner.nextLine();
System.out.println("请输入书籍的价格: ");
int price = scanner.nextInt();
scanner.nextLine(); //吸收回车
Book book =new Book(title,author,price); //使用构造器,将数据存储到Book类中
LIST.add( book); //将book的信息放进LIST中
System.out.println("书籍信息添加成功:"+book);
}
private static void modify(Scanner scanner){ //修改函数 修该list中已有数据的信息
scanner.nextLine(); //吸收回车
System.out.println("请输入你要修改的书籍的ID编号: ");
int index= scanner.nextInt();
scanner.nextLine(); //吸收回车
while(index>LIST.size()-1||index<0){ //判断用户输入的数字在list中是否存在
System.out.println("没有对应的书籍,请重新输入: ");
index = scanner.nextInt();
scanner.nextLine(); //吸收回车
}
Book book =LIST.get(index); //使用book读取LIST中指定位置的数据
System.out.println("请输入书籍的标题: ");
book.setTitle(scanner.nextLine());
System.out.println("请输入书记的作者: ");
book.setAuthor(scanner.nextLine());
System.out.println("请输入书籍的价格: ");
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.println("请输入你要删除的书籍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("删除信息成功! ");
}
}
Book类
import java.io.Serializable;
public class Book implements Serializable { //让这个类能够实现序列化
private String title;
private int price;
private String author;
public Book(String title,String author,int price) //构造器 快随初始化book
{
this.title=title;
this.author=author;
this.price=price;
}
public void setAuthor(String author) { //改变author的值
this.author = author;
}
public void setPrice(int price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public String toString(){ //改写toString函数
return "《"+title+"》作者:"+author+"("+price+"$)";
}
}