java手机进库程序:txt文件当做小型数据库实现增删改查,存储json数据到txt实体类的序列化和反序列化,修改txt中内容,Jackson操作,io流写入和输出

时间:2025-03-28 19:33:34
package com.lvyp.sprintboottest01.jacksonTest; import com.fasterxml.jackson.databind.ObjectMapper; import com.lvyp.sprintboottest01.bean.Phone; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PhoneTest { private static final ObjectMapper mapper = new ObjectMapper(); // jackson主要操作类 // 将json字符串反序列化为实体类存入list集合并返回 public static List<Phone> read() throws IOException { BufferedReader reader = new BufferedReader(new FileReader("./SprintBootTest01/src/main/resources/static/")); String info = null; ArrayList<Phone> list = new ArrayList<>(); for (int i = 0; (info = reader.readLine())!=null; i++) { Phone phone = mapper.readValue(info, Phone.class); list.add(phone); } return list; } // scanner接收数据返回实体类 public static Phone scanValue(){ Scanner scan = new Scanner(System.in); Phone phone = new Phone(); System.out.print("品牌型号:"); phone.setPhoneName(scan.next()); System.out.print("尺寸:"); phone.setSize(scan.nextDouble()); System.out.print("价格:"); phone.setPrice(scan.nextDouble()); scan.nextLine();// 先将double的回车读取了 System.out.print("配置:"); phone.setConfig(scan.nextLine()); System.out.print("库存:"); phone.setStoreAmount(scan.nextInt()); System.out.println(""); return phone; } // 插入方法 public static void write() throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter("./SprintBootTest01/src/main/resources/static/",true)); BufferedReader reader = new BufferedReader(new FileReader("./SprintBootTest01/src/main/resources/static/")); Phone phone = scanValue(); phone.setSumPrice(phone.getPrice()*phone.getStoreAmount()); String info = null; Phone readValue; int flag = 0; while ((info = reader.readLine())!=null) { readValue = mapper.readValue(info, Phone.class); if ((phone.getPhoneName().equals(readValue.getPhoneName()))){ flag = 1; break; } } if(flag == 0){ String s = mapper.writeValueAsString(phone); writer.write(s+"\r\n"); writer.close(); System.out.println("插入成功"); return; } System.out.println("插入失败"); } // 更新方法 public static void update() throws IOException { BufferedWriter writer = null; Scanner scanner = new Scanner(System.in); List<Phone> phoneList = read(); System.out.println("请输入要修改的手机名字"); String phoneName = scanner.next(); int i; int flag = 0; for (i = 0; i < phoneList.size(); i++) { if ((phoneList.get(i).getPhoneName().equals(phoneName))) { flag = 1; break; } } if (flag == 0) { System.out.println("没有此手机"); return; } System.out.println("请输入要修改的内容:输入1修改名字,输入2修改尺寸,输入3修改价格,输入4修改配置,输入5修改库存"); int num = scanner.nextInt(); // 要修改的项 switch (num){ case 1: phoneList.get(i).setPhoneName(scanner.next()); break; case 2: phoneList.get(i).setSize(scanner.nextDouble()); break; case 3: phoneList.get(i).setPrice(scanner.nextDouble()); break; case 4: phoneList.get(i).setConfig(scanner.next()); break; case 5: phoneList.get(i).setStoreAmount(scanner.nextInt()); break; } // 先清空一次,只要实例化字符输出流就会清空文件的内容 writer = new BufferedWriter(new FileWriter("./SprintBootTest01/src/main/resources/static/")); // 写入文件 for (int j = 0; j < phoneList.size(); j++) { String str = mapper.writeValueAsString(phoneList.get(j)); writer.write(str+"\r\n"); } writer.close(); } // 展示清单功能1 public static void show () throws IOException { List<Phone> phones; phones = read(); double sum =0; int sumAmount = 0; if (phones.size()==0){ System.out.println("清单为空"); return; } System.out.println("---------------------------库存清单-------------------"); System.out.println("型号 尺寸 价格 配置 库存数量 总价格"); for (int j = 0; j < phones.size(); j++) { System.out.println(phones.get(j).getPhoneName()+" "+ phones.get(j).getSize()+" "+ phones.get(j).getPrice()+" "+ phones.get(j).getConfig()+" "+ phones.get(j).getStoreAmount()+" "+ phones.get(j).getSumPrice()); sum = sum + phones.get(j).getSumPrice(); sumAmount = sumAmount + phones.get(j).getStoreAmount(); } System.out.println("-------------------------------------------------"); System.out.println("总库存:"+sum); System.out.println("库存总价:"+sumAmount); } public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); while(true){ System.out.println("输入1查看清单,输入2插入,输入3修改,输入4退出"); int i = scanner.nextInt(); if (i == 1){ show (); } else if (i == 2){ write(); } else if (i==3){ update(); } else if (i==4){ return; }else { System.out.println("输入错误,重新输入"); } } } }