package com.sf._02_list.demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        ArrayList<Goods> goods = new ArrayList<>();
        goods.add(new Goods(1L,"手机",1000,10));
        goods.add(new Goods(2L,"电脑",2000,10));
        goods.add(new Goods(3L,"耳机",500,10));
        printGoods(goods);
        // 在控制台当中录入购买商品名称和商品数量
        Scanner scanner = new Scanner(System.in);
        System.out.println("请录入要购买商品名称");
        String buyName = scanner.next();
        System.out.println("请输入要购买商品数量");
        int buyNum = scanner.nextInt();
        // 判断商品是否存在 , 如果存在扣减库存
        Goods good= contains(buyName, goods);
        if(good != null){
            System.out.println("购买商品存在");
            // 扣减库存
            good.reduceStock(buyNum);
        }else{
            System.out.println("购买商品不存在");
        }
        //计算总价
        double stockTotalPrice = getStockTotalPrice(goods);
        System.out.println("总价为:"+ stockTotalPrice);
    }

    /**
     * 计算库存总价
     */
    public static double getStockTotalPrice(List<Goods> list){
        double totalPrice=  0;
        for (Goods goods : list) {
            totalPrice+= goods.getPrice() * goods.getStock();
        }
        return totalPrice;
    }

    /**
     * 判断商品是否存在
     */
    public static Goods contains(String name,List<Goods> goods){
        for (Goods good : goods) {
            if(good.getName().equals(name)){
                return good;
            }
        }
        return null;
    }

    /**
     * 查询所有商品信息
     */
    public static void  printGoods(List<Goods> list){
        for (Goods goods : list) {
            System.out.println(goods);
        }
    }
}
