在控制台购物车操作。使用IO完成商品的添加和查询操作,并且把信息存储到本地,下次运行自动检测本地信息

import java.io.Serializable;

/**
 * 商品类
 */
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("输入不合法");
            }

        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值