/**
* 商品类
*/
public class Goods implements Serializable{
private Integer id;
private String name;
private Double price;
private int count;
public Goods (){
// TODO Auto-generated constructor stub
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* 使用IO完成商品的添加和查询操作,并且把信息存储到本地,下次运行自动检测本地信息
*/
public class Demo3 {
Map<Integer, Goods> mp = null;
public Demo3() throws Exception {
try {
//反序列化
mp = this.getGwc();
if (mp == null) {
mp = new HashMap<Integer, Goods>();
}
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
/**
* 添加商品
*
* @throws IOException
* @throws FileNotFoundException
*/
public void add(Scanner sc) throws FileNotFoundException, IOException {
System.out.println("请输入商品的编号");
int id = sc.nextInt();
if (mp.containsKey(id)) {
mp.get(id).setCount(mp.get(id).getCount() + 1);
} else {
Goods g = new Goods();
g.setId(id);
System.out.println("请输入商品的名称");
g.setName(sc.next());
System.out.println("请输入商品的价格");
g.setPrice(sc.nextDouble());
g.setCount(1);
mp.put(id, g);
}
this.saveGwc(mp);
}
/**
* 根据用户输入的编号删除商品
*
* @param id
* @throws IOException
* @throws FileNotFoundException
*/
public void deleteById(int id) throws FileNotFoundException, IOException {
mp.remove(id);
this.saveGwc(mp);
}
/**
* 根据用户输入的编号展示商品信息
*
* @param id
*/
public void showById(int id) {
System.out.println("编号\t商品名称\t商品价格\t商品数量\t总价");
Goods g = mp.get(id);
System.out.println(g.getId() + "\t" + g.getName() + "\t" + g.getPrice() + "\t" + g.getCount() + "\t"
+ g.getPrice() * g.getCount());
}
/**
* 展示所有商品信息
*/
public void showAll() {
System.out.println("编号\t商品名称\t商品价格\t商品数量\t总价");
Set<Integer> k = mp.keySet();
for (Integer i : k) {
Goods g = mp.get(i);
System.out.println(g.getId() + "\t" + g.getName() + "\t" + g.getPrice() + "\t" + g.getCount() + "\t"
+ g.getPrice() * g.getCount());
}
}
/**
* 把传进去的Map序列化保存到本地
* @param gs
*/
public void saveGwc(Map<Integer, Goods> gs) {
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("e:/gwc.c")));
os.writeObject(gs);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 把上次序列化的Map对象反序化出来
* @return
* @throws Exception
*/
public Map<Integer, Goods> getGwc() throws Exception {
File f = new File("e:/gwc.c");
if (f.exists()) {
return (Map<Integer, Goods>) (new ObjectInputStream(new FileInputStream(f)).readObject());
}
return null;
}
}
import java.util.Scanner;
/**
* 测试类
*
*/
public class ShopTest {
public static void main(String[] args) throws Exception {
Demo3 d = new Demo3();
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("1、添加商品2、根据id查看商品信息3、展示所有商品信息4、根据id删除商品5、退出");
int xx = sc.nextInt();
switch (xx) {
case 1:
d.add(sc);
break;
case 2:
System.out.println("请输入商品id");
int id = sc.nextInt();
d.showById(id);
break;
case 3:
d.showAll();
break;
case 4:
System.out.println("请输入商品id");
int id2 = sc.nextInt();
d.deleteById(id2);
break;
case 5:
sc.close();
System.exit(0);
default:
System.out.println("输入不合法");
}
}
}
}