Java习题>|异常>|throw自定义异常小案例

throw自定义异常小案例

题目

模拟实现用户购买商品的功能,使用数组模拟商品列表,当购买的商品不存在或者商品库存为0时,抛出自定义异常。用户购买某一个商品时,对异常进行处理,并对库存进行改变。

  1. 首先创建商品实体
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Goods {
    private int id;//商品id
    private String name;//商品名
    private int store;//商品库存
    private int price;//商品价格
}

此处使用的lombok插件添加注解详情

  1. 创建一个类写购买商品的方法
public class Exception2 {
    public static Goods[] goods;
    //初始化商品数组
    public static Scanner input;
	//初始化输入Scanner
  	//初始化商品数据
    static {
        goods = new Goods[3];
        input = new Scanner(System.in);
        goods[0] = new Goods(1,"apple", 10, 8000);
        goods[1] = new Goods(2,"apple", 5, 3500);
        goods[2] = new Goods(3,"apple", 8, 4200);
    }
	//main方法调用购买商品的方法
    public static void main(String[] args) {
        try {
            buygood();
        } catch (BuyGoodsException e) {
            System.out.println(e.getMessage());
        }
    }

    private static void buygood() {
        showGoods();
        System.out.println("请输入要买的商品id");
        int goodId = input.nextInt();
        //初始化一个要购买的商品
        Goods buygood = null;
        //设置变量
        boolean flag = false;
        for (Goods good : goods) {
            if(good==null){
                break;
            }
            //循环遍历商品数组中的每一个数组与输入的商品Id对比查看是否存在该商品
            if(good.getId() == goodId){
                buygood = good;
                //如果找到该商变量变为true
                flag = true;
                //结束循环
                break;
            }
        }
        //如果!flag==true说明flag=falsh为初始化值没有进入循环内的判断没有找到该商品
        //抛出自定义异常
        if(!flag){
            throw new BuyGoodsException("商品不存在");
        }
        System.out.println("您购买的商品信息如下");
        System.out.println(buygood);
        System.out.println("请输入要购买"+buygood.getName()+"的数量");
        int buynum = input.nextInt();
        //当购买数量大于商品内存的时候抛出异常
        if(buynum>buygood.getStore()){
            throw new BuyGoodsException("该商品库存不足");
        }
        System.out.println("购买"+buygood.getName()+buynum+"个"+"共计"+buynum*buygood.getPrice());
        //重新设置商品的库存
        buygood.setStore(buygood.getStore()-buynum);
        //展示购买完商品的信息
        showGoods();
    }
    
    //展示所有商品信息方法
    private static void showGoods() {
        //循环商品数组
        for (Goods good : goods) {
            //如果商品不等于空展示该商品
            if (good != null) {
                System.out.println(good);
            }
        }
    }

}
  1. 写自定义异常类

    public class BuyGoodsException extends RuntimeException {
        //自定义的异常类必须继承Exception的子类RuntimeException
        //当然也可以继承Exception那么这个自定义异常类就变成了编译时异常
        //必须处理
        private String msg;
        public BuyGoodsException(String msg) {
            super(msg);
        }
        //此处的构造方法必须重写父类的放方法。
    }
    
    

    初来乍到有写的不对的欢迎指教

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 购物车类 */ public class Cart { //创建一个map对象,用来保存商品,key为商品,value为商品的数量 private Map<Goods, Integer> map = new HashMap<Goods, Integer>(); /** * 添加商品到购物车方法 * @param id 商品编号 * @param quantity 商品数量 */ public void addGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ Integer oQuantity = map.get(goods);//获取商品在购物车中原本的数量 if(oQuantity!=null){ oQuantity += quantity; }else{ oQuantity = quantity; } map.put(goods, oQuantity);//添加商品到map中 System.out.println("添加商品"+goods.getName()+"成功!"); }else{ System.out.println("添加失败!商品编号不存在!"); } } /** * 按指定的编号删除商品 * @param id 商品编号 */ public void delGoods(int id){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.remove(goods);//从map删除商品 System.out.println("删除商品"+goods.getName()+"成功!"); }else{ System.out.println("删除失败!商品编号不存在!"); } } /** * 修改商品数量方法 * @param id 商品编号 * @param quantity 要修改的商品数量 */ public void updateGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.put(goods, quantity);//从map删除商品 }else{ System.out.println("修改失败!商品编号不存在!"); } } /** * 打印购物车商品列表 */ public void show(){ Set<Entry<Goods, Integer>> entrySet = map.entrySet(); System.out.println("编号\t单价\t数量\t名称\t总价"); for(Entry<Goods, Integer> entry:entrySet){ Goods goods = entry.getKey(); Integer quantity = entry.getValue(); System.out.println(goods.getId()+"\t"+goods.getPrice()+"\t"+quantity+"\t"+goods.getName()+"\t"+goods.getPrice()*quantity); } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵相机-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值