手机进库
1.程序介绍
学习:jackson操作和io流
使用txt做数据库持久化数据,数据库存储json数据。实现查看,插入,增加。
程序在springboot项目运行,也可以在普通项目运行,请自行导入Jackson的jar包
1.1 txt文件
1.2 查看:
- 将文件的字符串使用Json反序列化为对象,循环将属性输出
1.3 插入
- scanner接收数据,序列化为字符串,再用字符流写入到文件
1.4 修改
- 将文件内容反序列化,用list存放实体类,循环中用equals判断是否与txt中的型号相等,相等就修改实体类属性。将文件清空,再序列化实体类集合到文件。
删除功能:删除全部和删除某个。实例化一个字符输出流对象就会清除文件内所有内容,除非参数加上true。删除某个可以清除文件内容然再重新写入,利用循环判断是否存入文件,删除可自行实现。
2.代码
-
实体类
package com.lvyp.sprintboottest01.bean; import com.fasterxml.jackson.annotation.JsonInclude; @JsonInclude(JsonInclude.Include.NON_NULL) // 不展示值为Null的属性 public class Phone { String phoneName; double size; double price; String config; int storeAmount; double sumPrice; public Phone() { } public String getPhoneName() { return phoneName; } public void setPhoneName(String phoneName) { this.phoneName = phoneName; } public double getSize() { return size; } public void setSize(double size) { this.size = size; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getConfig() { return config; } public void setConfig(String config) { this.config = config; } public int getStoreAmount() { return storeAmount; } public void setStoreAmount(int storeAmount) { this.storeAmount = storeAmount; } public double getSumPrice() { return sumPrice; } public void setSumPrice(double sumPrice) { this.sumPrice = sumPrice; } @Override public String toString() { return "Phone{" + "phoneName='" + phoneName + '\'' + ", size=" + size + ", price=" + price + ", config='" + config + '\'' + ", storeAmount=" + storeAmount + ", sumPrice=" + sumPrice + '}'; } }
-
实现类:
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/database.txt")); 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/database.txt",true)); BufferedReader reader = new BufferedReader(new FileReader("./SprintBootTest01/src/main/resources/static/database.txt")); 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/database.txt")); // 写入文件 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("输入错误,重新输入"); } } } }
有什么错误, 请指指点点。。。。