java小项目商品录入查看系统

1.要求

  1. 有界面提示,录入商品 查看商品 删除商品 退出系统

  2. 录入商品 :输入商品基本信息 ,可以保存到文件中

    > 提示: 商品可以定义java类或用map, 多个商品存入List集合中 通过对象输入流存入文件

  3. 查看商品 遍历显示所有商品信息

通过对象输出流 ,读取文件,反序列化为List集合,遍历集合中的商品 ,商品显示 显示序号,供用户选择删除商品

  1. 删除商品 根据显示的商品序号 删除商品

  2. 退出 程序运行后,只有选择退出 才会结束 否则一次执行

2.代码

package com.by;
/**
 * 功能选择
 * @author 
 */
public enum Choose {
    QUERY(1, "查看商品"), SAVE(2, "添加商品"), DELETE(3, "删除商品"), EXIT(0, "退出系统");
    // 选择
    private int choose;
    // 描述
    private String desc;
    private Choose(int choose, String desc) {
        this.choose = choose;
        this.desc = desc;
    }
    public int getChoose() {
        return choose;
    }
    public String getDesc() {
        return desc;
    }
}
package com.by;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
​
/**
 * 数据存储
 * @author 
 */
@SuppressWarnings("all")
public class Database {
    // 存储数据的文件名
    private static String DBFILE = "shop.list";
    // 存储数据文件
    static File file;
    // 系统加载时创建文件
    static {
        file = new File(DBFILE);
        // 判断文件是否存在
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("数据存储文件创建失败");
            }
        }
    }
    /**
     * 读取数据
     * @return
     */
    public static List readDb() {
        List list = null;
        try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
            Object obj = ois.readObject();
            list = (List) obj;
        } catch (IOException e) {
            System.out.println("暂无数据");
        } catch (ClassNotFoundException e) {
            System.out.println("暂无数据");
        } finally {
            return list;
        }
    }
    /**
     * 将商品写入文件
     * @param goods
     * @return 0写入失败 1写入成功
     */
    public static int writeDb(Goods goods) {
        int rs = 0;// 默认写入失败
        // 先获取已有列表
        List list = readDb();
        if (null == list) {
            // 说明是第一次写入
            list = new ArrayList();
        }
        // 将新的商品加入列表
        list.add(goods);
        return writeDb(list);
    }
    /**
     * 将商品列表写入文件
     * @param goods
     * @return 0写入失败 1写入成功
     */
    public static int writeDb(List list) {
        int rs = 0;// 默认写入失败
        try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
            oos.writeObject(list);
            rs = 1;// 写入成功
        } catch (IOException e) {
            System.out.println("写入数据失败");
        }
        return rs;
    }
​
}
package com.by;
import java.io.Serializable;
/**
 * 商品类 存储数据
 * @author 
 */
public class Goods implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 7898648852282924243L;
    private String name;
    private double price;
    private int num;
    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;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Goods(String name, double price, int num) {
        super();
        this.name = name;
        this.price = price;
        this.num = num;
    }
    public Goods() {
        super();
    }
}
package com.by;
import java.io.Serializable;
/**
 * 商品类 存储数据 
 * @author 
 */
public class Goods implements Serializable {
    private static final long serialVersionUID = 7898648852282924243L;
    private String name;
    private double price;
    private int num;
    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;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Goods(String name, double price, int num) {
        super();
        this.name = name;
        this.price = price;
        this.num = num;
    }
    public Goods() {
        super();
    }
}
package com.by;
import java.util.Scanner;
/**
 * 程序主入口
 * @author 
 */
public class Main {
​
    public static void main(String[] args) {
        Main main = new Main();
        // 用户选择
        int choose = -1;
        // 商品业务类
        GoodsService service = new GoodsService();
        while ((choose = main.welcome()) != 0) {
            switch (choose) {
            case 1:
                // 查看商品
                service.query();
                break;
            case 2:
                // 添加商品
                service.save();
                break;
            case 3:
                // 删除商品
                service.delete();
                break;
            }
        }
    }
    /**
     * 欢迎界面
     */
    int welcome() {
        System.out.println();
        System.out.println("\t\t\t               商品管理系统                ");
        System.out.println("\t\t\t=====================================");
        System.out.println();
        Choose[] chooses = Choose.values();
        for (Choose c : chooses) {
            System.out.println("\t\t\t\t     " + c.getChoose() + " " + c.getDesc());
        }
        System.out.println("\t\t\t请选择:");
        Scanner scanner = new Scanner(System.in);
        int rs = scanner.nextInt();
        return rs;
    }
}

3.运行截图

1.运行之后,查看商品,这里没有录入商品

2.录入商品

3.查看商品

4.删除商品以后,自动查看剩余商品,这里没有剩余商品

5.退出系统

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值