A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Aaron9527 中级黑马   /  2016-6-8 01:16  /  717 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

基础班第六天的案例,现在加了一些IO流的知识在里面,当然还有很多问题的地方,希望大家多多指点

3 个回复

倒序浏览
  1. package cn.itcast.水果超市IO流版本;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Scanner;
  14. import java.util.Set;

  15. /*
  16. 文件操作符和序列化能不能一起使用?
  17. 这里采用序列化方式初始化水果

  18. * */
  19. public class MainApp {
  20.         public static void main(String[] args) throws IOException,
  21.                         ClassNotFoundException {
  22.                 Map<String, Fruit> e = new HashMap<String, Fruit>();
  23.                 // 水果初始化

  24.                 // 初始化做了两件事,1:将水果序列化 2:给水果配置了文件操作符
  25.                 init(e);
  26.                
  27.                 System.out.println("欢迎来到熊王水果超级市场,熊王水果,越吃越开心!");
  28.         do{
  29.                 System.out.println("请根据一下提示完成对应操作");
  30.                 System.out.println("1:查询水果\t2:增加水果\t3:删除水果\t4:修改水果\t5:退出系统");
  31.                 Scanner sc = new Scanner(System.in);
  32.                 int chose = sc.nextInt();
  33.                 switch (chose) {
  34.                 case 1:
  35.                         printAll(e);
  36.                         break;
  37.                 case 2:
  38.                         ftuitFormat(e);
  39.                         break;
  40.                 case 3:
  41.                         fruitRemove(e);
  42.                         break;
  43.                 case 4:
  44.                         updateFruit(e);
  45.                         break;
  46.                 case 5:
  47.                         System.exit(0);
  48.                         break;
  49.                 default:
  50.                         System.out.println("脑残请勿玩坏系统,该干嘛干嘛去");
  51.                         break;
  52.                 }
  53.                 }while(true);
  54.         }

  55.         private static void updateFruit(Map<String, Fruit> e) {
  56.                 System.out.println("请输入要修改的水果编号!");
  57.                 Scanner sc = new Scanner(System.in);
  58.                 String idd = sc.next();
  59.                 Set<Map.Entry<String, Fruit>> ff = e.entrySet();
  60.                 for(Object g : ff){
  61.                         Map.Entry<String,Fruit> ee=(Map.Entry<String,Fruit>)g;
  62.                         String color=ee.getKey();
  63.                         Fruit gg = ee.getValue();
  64.                         if(idd==gg.getID()){
  65.                                 gg.setID(idd);
  66.                                 System.out.println("请输入新的水果名称");
  67.                                 String newName = sc.next();
  68.                                 gg.setName(newName);
  69.                         // String iD, String name, double price, int number, double money
  70.                                 System.out.println("请输入新的水果价格");
  71.                                 double newPrice = sc.nextDouble();
  72.                                 gg.setPrice(newPrice);
  73.                                 System.out.println("请输入新的水果数量");
  74.                                 int newNumber = sc.nextInt();
  75.                                 gg.setNumber(newNumber);
  76.                                 System.out.println("请输入新的水果价格");
  77.                                 double newMoney = sc.nextDouble();
  78.                                 gg.setMoney(newMoney);
  79.                         }
  80.                 }
  81.                 System.out.println("恭喜!水果更新成功!");
  82.         }

  83.         private static void fruitRemove(Map<String, Fruit> e) {
  84. //删除水果,遍历然后删除水果对象即可,这里任然采用keySet遍历方式
  85.                 System.out.println("请输入要删除的水果编号");
  86.                 Scanner sc = new Scanner(System.in);
  87.                 String idd = sc.next();
  88.                 Set<String> f = e.keySet();
  89.                 for(Object g:f){
  90.                         Fruit l=e.get(g);
  91.                         if(l.getID()==idd){
  92.                                 e.remove(l);
  93.                         }
  94.                 }
  95.                 System.out.println("水果删除成功!");
  96.         }

  97.         private static void ftuitFormat(Map<String, Fruit> e) {
  98.                 // 增加水果,需要遍历水果,然后再匹配ID,这时,颜色不需要考虑,只是水果ID不能一致便可添加!这里只用集合的方法来做

  99.                 Set f = e.keySet();
  100.         /*        a:while (true) {
  101.                         Scanner sc = new Scanner(System.in);
  102.                         String idd = sc.next();
  103.                         for (Object i : f) {
  104.                                 Fruit g = e.get(i);
  105.                                 if (g.getID() == idd) {
  106.                                         System.out.println("您输入的编号已经存在!");
  107.                                         continue a;
  108.                                 }
  109.                         }
  110.                         break;
  111.                 }*/                //这一个验证想法还行,但是感觉无法实施,因为我调用水果类必须先得遍历HashMap,然后再通过匹配不同的值来得到对象,
  112.                                 //这样就需要用户输入两次才能完成该操作!
  113.                
  114.                         System.out.println("请输入要增加的水果编号");
  115.                         Scanner sc = new Scanner(System.in);
  116.                         String idd = sc.next();
  117.                         for (Object i : f) {
  118.                                 Fruit g = e.get(i);
  119.                                 if (g.getID() != idd) {
  120.                                         g.setID(idd);
  121.                                         // String iD, String name, double price, int number, double money
  122.                                         System.out.println("请输入要新的水果名称");
  123.                                         String newname = sc.next();
  124.                                         g.setName(newname);
  125.                                         System.out.println("请输入新的水果价格");
  126.                                         double newprice = sc.nextDouble();
  127.                                         g.setPrice(newprice);
  128.                                         System.out.println("请输入新的水果数量");
  129.                                         int newnumber = sc.nextInt();
  130.                                         g.setNumber(newnumber);
  131.                                         System.out.println("请输入新的水果总额");
  132.                                         double newmoney = sc.nextDouble();
  133.                                         g.setMoney(newmoney);
  134.                                         return ;
  135.                                 }
  136.                         }
  137.                         System.out.println("水果添加成功!");
  138.         }

  139.         private static void printAll(Map<String, Fruit> e) throws IOException,
  140.                         ClassNotFoundException {
  141.                 // 遍历Map集合,打印水果 //遍历方式1
  142.                 Set<Map.Entry<String, Fruit>> f = e.entrySet();
  143.                 for (Object i : f) {
  144.                         Map.Entry<String, Fruit> g = (Map.Entry<String, Fruit>) i;
  145.                         String color = g.getKey();
  146.                         Fruit ff = g.getValue();
  147.                         System.out.println(color + "--------" + ff.getID() + "-------"
  148.                                         + ff.getName() + "-------" + ff.getNumber()  + "-------"+ ff.getPrice()
  149.                                          + "-------"+ ff.getMoney());
  150.                 }
  151.                 // 遍历文件操作符,打印水果 //遍历方式2
  152.                 ObjectInputStream input = new ObjectInputStream(
  153.                                 new BufferedInputStream(new FileInputStream("D:\\3.txt")));
  154.                 // 持久化对象没有对应的ObjectReader方法,所以这里不能用字符流来读取
  155.                 Object reading = null;
  156.                 while ((reading = input.readObject()) != null) {
  157.                         Fruit dd = (Fruit) reading;
  158.                         System.out.println(dd.getID() + "-----" + dd.getName() + "-----"
  159.                                         + dd.getNumber() + "-----" + dd.getPrice() + "-----"
  160.                                         + dd.getMoney());
  161.                
  162.                 }
  163.                
  164.                 //EOFException
  165.                 //这里得持久化一个集合,用这个readObject会产生一个标记问题,这样读取最后一个就会出现EOFException异常!
  166.         }

  167.         private static void init(Map<String, Fruit> e) throws IOException {
  168.                 ObjectOutputStream obj = new ObjectOutputStream(
  169.                                 new BufferedOutputStream(new FileOutputStream("D:\\3.txt")));

  170.                 // String iD, String name, double price, int number, double money
  171.                 /*
  172.                  * obj.writeObject(new Fruit("0x0001","榴莲",30.0,100,3000.0 ));
  173.                  * obj.writeObject(new Fruit("0x0002","葡萄",10.0,100,1000.0 ));
  174.                  * obj.writeObject(new Fruit("0x0003","杨梅",8.0,100,800.0 ));
  175.                  * obj.writeObject(new Fruit("0x0004","葫芦",3.0,100,300.0 ));
  176.                  */
  177.                 Fruit fruit1 = new Fruit("0x0001", "榴莲", 30.0, 100, 3000.0);
  178.                 obj.writeObject(fruit1);
  179.                 Fruit fruit2 = new Fruit("0x0002", "葡萄", 10.0, 100, 1000.0);
  180.                 obj.writeObject(fruit2);
  181.                 Fruit fruit3 = new Fruit("0x0003", "杨梅", 8.0, 100, 800.0);
  182.                 obj.writeObject(fruit3);
  183.                 Fruit fruit4 = new Fruit("0x0004", "葫芦", 3.0, 100, 300.0);
  184.                 obj.writeObject(fruit4);

  185.                 obj.close();
  186.                 // 与此同时,我还需要将其写入集合中,以便完成增删改查的功能,此时Fruit类中已经存了4个对象,因为下面要依次存储,故不用匿名对象
  187.                 Fruit i = new Fruit();

  188.                 e.put("黄色", fruit1);
  189.                 e.put("紫色", fruit2);
  190.                 e.put("红色", fruit3);
  191.                 e.put("绿色", fruit4);

  192.         }
  193. }
复制代码
回复 使用道具 举报
  1. package cn.itcast.水果超市IO流版本;

  2. import java.io.Serializable;

  3. class Fruit implements Serializable {
  4.         private String ID;
  5.         private String name;
  6.         private double price;
  7.         private int number;
  8.         private double money;
  9.         public Fruit(String iD, String name, double price, int number, double money) {
  10.                 super();
  11.                 ID = iD;
  12.                 this.name = name;
  13.                 this.price = price;
  14.                 this.number = number;
  15.                 this.money = money;
  16.         }
  17.         /**
  18.          * @return the iD
  19.          */
  20.         public String getID() {
  21.                 return ID;
  22.         }
  23.         /**
  24.          * @param iD the iD to set
  25.          */
  26.         public void setID(String iD) {
  27.                 ID = iD;
  28.         }
  29.         /**
  30.          * @return the name
  31.          */
  32.         public String getName() {
  33.                 return name;
  34.         }
  35.         /**
  36.          * @param name the name to set
  37.          */
  38.         public void setName(String name) {
  39.                 this.name = name;
  40.         }
  41.         /**
  42.          * @return the price
  43.          */
  44.         public double getPrice() {
  45.                 return price;
  46.         }
  47.         /**
  48.          * @param price the price to set
  49.          */
  50.         public void setPrice(double price) {
  51.                 this.price = price;
  52.         }
  53.         /**
  54.          * @return the number
  55.          */
  56.         public int getNumber() {
  57.                 return number;
  58.         }
  59.         /**
  60.          * @param number the number to set
  61.          */
  62.         public void setNumber(int number) {
  63.                 this.number = number;
  64.         }
  65.         /**
  66.          * @return the money
  67.          */
  68.         public double getMoney() {
  69.                 return money;
  70.         }
  71.         /**
  72.          * @param money the money to set
  73.          */
  74.         public void setMoney(double money) {
  75.                 this.money = money;
  76.         }
  77.         public Fruit() {
  78.                 super();
  79.                 // TODO Auto-generated constructor stub
  80.         }
  81.         /* (non-Javadoc)
  82.          * @see java.lang.Object#hashCode()
  83.          */
  84.         @Override
  85.         public int hashCode() {
  86.                 final int prime = 31;
  87.                 int result = 1;
  88.                 result = prime * result + ((ID == null) ? 0 : ID.hashCode());
  89.                 long temp;
  90.                 temp = Double.doubleToLongBits(money);
  91.                 result = prime * result + (int) (temp ^ (temp >>> 32));
  92.                 result = prime * result + ((name == null) ? 0 : name.hashCode());
  93.                 result = prime * result + number;
  94.                 temp = Double.doubleToLongBits(price);
  95.                 result = prime * result + (int) (temp ^ (temp >>> 32));
  96.                 return result;
  97.         }
  98.         /* (non-Javadoc)
  99.          * @see java.lang.Object#equals(java.lang.Object)
  100.          */
  101.         @Override
  102.         public boolean equals(Object obj) {
  103.                 if (this == obj)
  104.                         return true;
  105.                 if (obj == null)
  106.                         return false;
  107.                 if (getClass() != obj.getClass())
  108.                         return false;
  109.                 Fruit other = (Fruit) obj;
  110.                 if (ID == null) {
  111.                         if (other.ID != null)
  112.                                 return false;
  113.                 } else if (!ID.equals(other.ID))
  114.                         return false;
  115.                 if (Double.doubleToLongBits(money) != Double
  116.                                 .doubleToLongBits(other.money))
  117.                         return false;
  118.                 if (name == null) {
  119.                         if (other.name != null)
  120.                                 return false;
  121.                 } else if (!name.equals(other.name))
  122.                         return false;
  123.                 if (number != other.number)
  124.                         return false;
  125.                 if (Double.doubleToLongBits(price) != Double
  126.                                 .doubleToLongBits(other.price))
  127.                         return false;
  128.                 return true;
  129.         }
  130.         /* (non-Javadoc)
  131.          * @see java.lang.Object#toString()
  132.          */
  133.         @Override
  134.         public String toString() {
  135.                 return "Fruit [ID=" + ID + ", name=" + name + ", price=" + price
  136.                                 + ", number=" + number + ", money=" + money + "]";
  137.         }
  138.        
  139. }
复制代码
回复 使用道具 举报
查询水果的方式有些问题。用readObject()方法一次只能读取一次,有一个指针移动的问题,就是当我读取到最后一个的时候,循环还会往下走一次,这是指针指到最底端,那时候已经没有对应的水果了,所以抛出了EOFException异常,如果用for循环改进的话,那必须得知道循环的次数,这样我每次添加水果都得改一次,显得很麻烦,老师提示我可以再持久化一个集合,然后再遍历就能够得到解决,但是这样得大改啊有没有{:2_44:},~~~~(>_<)~~~~就没有什么其他的好一点的方法吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马