Main.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package J20250718.demo02_exception;
  2. import java.util.ArrayList;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title Main
  7. * @description
  8. * @create 2025/7/18
  9. */
  10. public class Main {
  11. private ArrayList<Product> list=new ArrayList<>();
  12. {
  13. // Product p1 = new Product("商品名称",商品价格,商品库存呢);
  14. // Product p2 = new Product("可乐",3.5,50);
  15. // Product p3 = new Product("雪碧",35,500);
  16. // Product p4 = new Product();
  17. // Product p5 = new Product();
  18. // list.add(p1);
  19. // list.add(p2);
  20. // list.add(p3);
  21. // list.add(p4);
  22. // list.add(p5);
  23. }
  24. public void shopping(Product product){
  25. for (int i = 0; i < list.size() ; i++) {
  26. Product p = list.get(i);
  27. if(p.getName()==product.getName()){
  28. int count=p.getCount()-product.getCount();
  29. if (count>=0){
  30. p.setCount(count);
  31. }else {
  32. // 抛出库存不足异常
  33. }
  34. }else {
  35. // 抛出商品不存在异常对象
  36. }
  37. }
  38. }
  39. public static void main(String[] args) {
  40. Main main=new Main();
  41. //Product p=new Product("可乐",1);
  42. //main.shopping(p);
  43. }
  44. }