123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package J20250718.demo02_exception;
- import java.util.ArrayList;
- /**
- * @author WanJl
- * @version 1.0
- * @title Main
- * @description
- * @create 2025/7/18
- */
- public class Main {
- private ArrayList<Product> list=new ArrayList<>();
- {
- // Product p1 = new Product("商品名称",商品价格,商品库存呢);
- // Product p2 = new Product("可乐",3.5,50);
- // Product p3 = new Product("雪碧",35,500);
- // Product p4 = new Product();
- // Product p5 = new Product();
- // list.add(p1);
- // list.add(p2);
- // list.add(p3);
- // list.add(p4);
- // list.add(p5);
- }
- public void shopping(Product product){
- for (int i = 0; i < list.size() ; i++) {
- Product p = list.get(i);
- if(p.getName()==product.getName()){
- int count=p.getCount()-product.getCount();
- if (count>=0){
- p.setCount(count);
- }else {
- // 抛出库存不足异常
- }
- }else {
- // 抛出商品不存在异常对象
- }
- }
- }
- public static void main(String[] args) {
- Main main=new Main();
- //Product p=new Product("可乐",1);
- //main.shopping(p);
- }
- }
|