w

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.JOptionPane;
import java.io.ObjectOutputStream.PutField;
import java.util.ArrayList;
import java.util.HashMap;

public class TmallStore implements Store ,Runnable{
    HashMap<String, Long> priceMap=new HashMap<>();        //存放价格信息
    ConcurrentHashMap<String , ArrayList<Product>> cartMap=new ConcurrentHashMap<>();//存放多个购物车 
    ThreadLocal <String> customerId=new ThreadLocal<>();        //线程变量用来记录当前用户名
    
    
    @Override
    public long getCartTotal() {
        if(getCartItems()!=null) {
            List<LineItem> cart=getCartItems();
            int sum=0;
            for(LineItem i:cart) {
                sum+=i.getQuantity()*priceMap.get(i.getName());
            }
            return sum;
        }
        return 0;
    }

    
    
    @Override
    public void addItemToCart(String name, int quantity) {
        if(!cartMap.containsKey(customerId.get())) {
            cartMap.put(customerId.get(), new ArrayList<>());
        }
        ArrayList<Product> cart=cartMap.get(customerId.get());
        for(Product i:cart) {
            if(i.getName().equals(name)) {
                i.setQuantity(i.getQuantity()+quantity);
                return ;    //找到则直接返回
            }
        }
        //for循环之后还没有找到则说明购物车中没有该商品
        cart.add(new Product(name,quantity));
        
    }

    @Override
    public void addItemToStore(String name, long price) {
        
        priceMap.put(name,price);
    }

    @Override
    public List<LineItem> getCartItems() {
        List list=cartMap.get(customerId.get());
        return list;
    }


    
    public void setCustomerId() {
        customerId.set(Thread.currentThread().getName());
    }
    

    
    @Override
    public void run() {
        
        setCustomerId();
        System.out.println(customerId.get());
        Object[] options ={ "添加商品至购物车","购物车统计","退出" };  //自定义按钮上的文字
        int m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        while(m!=2) {
            if(m==0) {
                String name=JOptionPane.showInputDialog("请输入商品名:");
                if(!priceMap.containsKey(name)) {
                    JOptionPane.showMessageDialog(null,"该商品不存在");
                    m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                    continue;
                }
                String quantityString=JOptionPane.showInputDialog("请输入个数:");
                int quantity=Integer.valueOf(quantityString);
                addItemToCart(name, quantity);
                m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            }
            else if(m==1) {
                JOptionPane.showMessageDialog(null, customerId.get()+"您购物车内产品共计"+String.valueOf(getCartTotal()));
                m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                
            }
        }
        
    }

}


import javax.swing.JOptionPane;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TmallStore store=new TmallStore();
        Object[] options ={ "购物车相关","商品添加","退出" };  //自定义按钮上的文字
        int m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        System.out.println(m);
        while(m!=2) {
            if(m==0) {
                String s=JOptionPane.showInputDialog("请输入客户名:");
                Thread thread = new Thread(store,s);
                thread.start();
                m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            }
            else if(m==1) {
                String name=JOptionPane.showInputDialog("请输入商品名:");
                String priceString=JOptionPane.showInputDialog("请输入价格:");
                long price =Long.valueOf(priceString);
                store.addItemToStore(name, price);
                m = JOptionPane.showOptionDialog(null, "请选择功能?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            }
            if(m==2)
                System.exit(0);
            
        }
    }

}

public class Product implements LineItem{
    private String name;
    private int quantity;
    
    public  Product() {
        // TODO Auto-generated constructor stub
    }
    
    public  Product(String name,int quantity) {
        // TODO Auto-generated constructor stub
        this.name=name;
        this.quantity=quantity;
    }
    @Override
    public String getName() {

        return name;
    }

    @Override
    public int getQuantity() {
        return quantity;
    }
    
    public void setQuantity(int quantity) {
        if(quantity>=0)
            this.quantity=quantity;
    }
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值