- package cn.itcast.水果超市IO流版本;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- /*
- 文件操作符和序列化能不能一起使用?
- 这里采用序列化方式初始化水果
- * */
- public class MainApp {
- public static void main(String[] args) throws IOException,
- ClassNotFoundException {
- Map<String, Fruit> e = new HashMap<String, Fruit>();
- // 水果初始化
- // 初始化做了两件事,1:将水果序列化 2:给水果配置了文件操作符
- init(e);
-
- System.out.println("欢迎来到熊王水果超级市场,熊王水果,越吃越开心!");
- do{
- System.out.println("请根据一下提示完成对应操作");
- System.out.println("1:查询水果\t2:增加水果\t3:删除水果\t4:修改水果\t5:退出系统");
- Scanner sc = new Scanner(System.in);
- int chose = sc.nextInt();
- switch (chose) {
- case 1:
- printAll(e);
- break;
- case 2:
- ftuitFormat(e);
- break;
- case 3:
- fruitRemove(e);
- break;
- case 4:
- updateFruit(e);
- break;
- case 5:
- System.exit(0);
- break;
- default:
- System.out.println("脑残请勿玩坏系统,该干嘛干嘛去");
- break;
- }
- }while(true);
- }
- private static void updateFruit(Map<String, Fruit> e) {
- System.out.println("请输入要修改的水果编号!");
- Scanner sc = new Scanner(System.in);
- String idd = sc.next();
- Set<Map.Entry<String, Fruit>> ff = e.entrySet();
- for(Object g : ff){
- Map.Entry<String,Fruit> ee=(Map.Entry<String,Fruit>)g;
- String color=ee.getKey();
- Fruit gg = ee.getValue();
- if(idd==gg.getID()){
- gg.setID(idd);
- System.out.println("请输入新的水果名称");
- String newName = sc.next();
- gg.setName(newName);
- // String iD, String name, double price, int number, double money
- System.out.println("请输入新的水果价格");
- double newPrice = sc.nextDouble();
- gg.setPrice(newPrice);
- System.out.println("请输入新的水果数量");
- int newNumber = sc.nextInt();
- gg.setNumber(newNumber);
- System.out.println("请输入新的水果价格");
- double newMoney = sc.nextDouble();
- gg.setMoney(newMoney);
- }
- }
- System.out.println("恭喜!水果更新成功!");
- }
- private static void fruitRemove(Map<String, Fruit> e) {
- //删除水果,遍历然后删除水果对象即可,这里任然采用keySet遍历方式
- System.out.println("请输入要删除的水果编号");
- Scanner sc = new Scanner(System.in);
- String idd = sc.next();
- Set<String> f = e.keySet();
- for(Object g:f){
- Fruit l=e.get(g);
- if(l.getID()==idd){
- e.remove(l);
- }
- }
- System.out.println("水果删除成功!");
- }
- private static void ftuitFormat(Map<String, Fruit> e) {
- // 增加水果,需要遍历水果,然后再匹配ID,这时,颜色不需要考虑,只是水果ID不能一致便可添加!这里只用集合的方法来做
- Set f = e.keySet();
- /* a:while (true) {
- Scanner sc = new Scanner(System.in);
- String idd = sc.next();
- for (Object i : f) {
- Fruit g = e.get(i);
- if (g.getID() == idd) {
- System.out.println("您输入的编号已经存在!");
- continue a;
- }
- }
- break;
- }*/ //这一个验证想法还行,但是感觉无法实施,因为我调用水果类必须先得遍历HashMap,然后再通过匹配不同的值来得到对象,
- //这样就需要用户输入两次才能完成该操作!
-
- System.out.println("请输入要增加的水果编号");
- Scanner sc = new Scanner(System.in);
- String idd = sc.next();
- for (Object i : f) {
- Fruit g = e.get(i);
- if (g.getID() != idd) {
- g.setID(idd);
- // String iD, String name, double price, int number, double money
- System.out.println("请输入要新的水果名称");
- String newname = sc.next();
- g.setName(newname);
- System.out.println("请输入新的水果价格");
- double newprice = sc.nextDouble();
- g.setPrice(newprice);
- System.out.println("请输入新的水果数量");
- int newnumber = sc.nextInt();
- g.setNumber(newnumber);
- System.out.println("请输入新的水果总额");
- double newmoney = sc.nextDouble();
- g.setMoney(newmoney);
- return ;
- }
- }
- System.out.println("水果添加成功!");
- }
- private static void printAll(Map<String, Fruit> e) throws IOException,
- ClassNotFoundException {
- // 遍历Map集合,打印水果 //遍历方式1
- Set<Map.Entry<String, Fruit>> f = e.entrySet();
- for (Object i : f) {
- Map.Entry<String, Fruit> g = (Map.Entry<String, Fruit>) i;
- String color = g.getKey();
- Fruit ff = g.getValue();
- System.out.println(color + "--------" + ff.getID() + "-------"
- + ff.getName() + "-------" + ff.getNumber() + "-------"+ ff.getPrice()
- + "-------"+ ff.getMoney());
- }
- // 遍历文件操作符,打印水果 //遍历方式2
- ObjectInputStream input = new ObjectInputStream(
- new BufferedInputStream(new FileInputStream("D:\\3.txt")));
- // 持久化对象没有对应的ObjectReader方法,所以这里不能用字符流来读取
- Object reading = null;
- while ((reading = input.readObject()) != null) {
- Fruit dd = (Fruit) reading;
- System.out.println(dd.getID() + "-----" + dd.getName() + "-----"
- + dd.getNumber() + "-----" + dd.getPrice() + "-----"
- + dd.getMoney());
-
- }
-
- //EOFException
- //这里得持久化一个集合,用这个readObject会产生一个标记问题,这样读取最后一个就会出现EOFException异常!
- }
- private static void init(Map<String, Fruit> e) throws IOException {
- ObjectOutputStream obj = new ObjectOutputStream(
- new BufferedOutputStream(new FileOutputStream("D:\\3.txt")));
- // String iD, String name, double price, int number, double money
- /*
- * obj.writeObject(new Fruit("0x0001","榴莲",30.0,100,3000.0 ));
- * obj.writeObject(new Fruit("0x0002","葡萄",10.0,100,1000.0 ));
- * obj.writeObject(new Fruit("0x0003","杨梅",8.0,100,800.0 ));
- * obj.writeObject(new Fruit("0x0004","葫芦",3.0,100,300.0 ));
- */
- Fruit fruit1 = new Fruit("0x0001", "榴莲", 30.0, 100, 3000.0);
- obj.writeObject(fruit1);
- Fruit fruit2 = new Fruit("0x0002", "葡萄", 10.0, 100, 1000.0);
- obj.writeObject(fruit2);
- Fruit fruit3 = new Fruit("0x0003", "杨梅", 8.0, 100, 800.0);
- obj.writeObject(fruit3);
- Fruit fruit4 = new Fruit("0x0004", "葫芦", 3.0, 100, 300.0);
- obj.writeObject(fruit4);
- obj.close();
- // 与此同时,我还需要将其写入集合中,以便完成增删改查的功能,此时Fruit类中已经存了4个对象,因为下面要依次存储,故不用匿名对象
- Fruit i = new Fruit();
- e.put("黄色", fruit1);
- e.put("紫色", fruit2);
- e.put("红色", fruit3);
- e.put("绿色", fruit4);
- }
- }
复制代码 |