cart.java
来自「Java完全自学宝典配套源代码 全集 例子丰富」· Java 代码 · 共 65 行
JAVA
65 行
package wyf;
import java.util.*;
public class Cart
{
HashMap hm=new HashMap();
double total=0;
public void add(String sid)
{
if(hm.containsKey(sid))
{
int xc=((Integer)hm.get(sid)).intValue()+1;
hm.put(sid,new Integer(xc));;
}
else
{
hm.put(sid,new Integer(1));
}
total=total+DBBean.getPrice(sid);
}
public double getTotal()
{
return total;
}
public void setCount(int c,String sid)
{
int yc=((Integer)hm.get(sid)).intValue();
total=total+(c-yc)*DBBean.getPrice(sid);
hm.put(sid,new Integer(c));
}
public void deleteFromCart(String sid)
{
int yc=((Integer)hm.get(sid)).intValue();
total=total-yc*DBBean.getPrice(sid);
hm.remove(sid);
}
public boolean isEmpty()
{
return hm.isEmpty();
}
public String[][] getCart()
{
Set ks=hm.keySet();
Iterator ii=ks.iterator();
int size=hm.size();
String rs[][]=new String[size][];
for(int i=0;i<size;i++)
{
String sid=(String)ii.next();
String[] sa=new String[4];
sa[0]=sid;
String[] sat=DBBean.getDetail(sid);
sa[1]=sat[0];
sa[2]=sat[1];
sa[3]=((Integer)hm.get(sid)).toString();
rs[i]=sa;
}
return rs;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?