黑马程序员技术交流社区
标题:
水果超市IO流版本
[打印本页]
作者:
Aaron9527
时间:
2016-6-8 01:16
标题:
水果超市IO流版本
基础班第六天的案例,现在加了一些IO流的知识在里面,当然还有很多问题的地方,希望大家多多指点
作者:
Aaron9527
时间:
2016-6-8 01:18
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);
}
}
复制代码
作者:
Aaron9527
时间:
2016-6-8 01:19
package cn.itcast.水果超市IO流版本;
import java.io.Serializable;
class Fruit implements Serializable {
private String ID;
private String name;
private double price;
private int number;
private double money;
public Fruit(String iD, String name, double price, int number, double money) {
super();
ID = iD;
this.name = name;
this.price = price;
this.number = number;
this.money = money;
}
/**
* @return the iD
*/
public String getID() {
return ID;
}
/**
* @param iD the iD to set
*/
public void setID(String iD) {
ID = iD;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the number
*/
public int getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(int number) {
this.number = number;
}
/**
* @return the money
*/
public double getMoney() {
return money;
}
/**
* @param money the money to set
*/
public void setMoney(double money) {
this.money = money;
}
public Fruit() {
super();
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
long temp;
temp = Double.doubleToLongBits(money);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + number;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fruit other = (Fruit) obj;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (Double.doubleToLongBits(money) != Double
.doubleToLongBits(other.money))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (number != other.number)
return false;
if (Double.doubleToLongBits(price) != Double
.doubleToLongBits(other.price))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Fruit [ID=" + ID + ", name=" + name + ", price=" + price
+ ", number=" + number + ", money=" + money + "]";
}
}
复制代码
作者:
Aaron9527
时间:
2016-6-8 01:25
查询水果的方式有些问题。用readObject()方法一次只能读取一次,有一个指针移动的问题,就是当我读取到最后一个的时候,循环还会往下走一次,这是指针指到最底端,那时候已经没有对应的水果了,所以抛出了EOFException异常,如果用for循环改进的话,那必须得知道循环的次数,这样我每次添加水果都得改一次,显得很麻烦,老师提示我可以再持久化一个集合,然后再遍历就能够得到解决,但是这样得大改啊有没有{:2_44:},~~~~(>_<)~~~~就没有什么其他的好一点的方法吗
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2