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;
}
}