本帖最后由 doevents 于 2013-9-17 00:03 编辑
简单的实现购物车功能,其中有3个文件Product.java,ShoppingCart.java和ShoppingTest.java,代码如下
Product.java
//超市中的商品
public class Product
{
//Field
private int no;
private String name;
private double price;
//Constructor
public Product(){}
public Product(int no,String name,double price){
this.no = no;
this.name = name;
this.price = price;
}
//setter and getter
public void setNo(int no){
this.no = no;
}
public int getNo(){
return no;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return price;
}
//toString
public String toString(){
return "[编号:"+no+",名称:"+name+",单价:"+price+"元/个]";
}
//hashCode
//超市商品编号的格式:[1000-9999]
public int hashCode(){
return no/1000;
}
//equals
//需求:如果商品编号和商品名字都一样表示同一个商品
public boolean equals(Object o){
if(this==o) return true;
if(o instanceof Product){
Product p = (Product)o;
if(p.no==this.no && p.name.equals(this.name)){
return true;
}
}
return false;
}
}
ShoppingCart.java
import java.util.*;
//购物车
public class ShoppingCart
{
//使用Map存储商品条目.
Map<Product,Integer> productMaps;
//总价
double totalPrice;
//Constructor
ShoppingCart(){
productMaps = new HashMap<Product,Integer>();
}
//添加1个商品
public void add(Product p){
add(p,1);
}
//添加N个商品
public void add(Product p,int n){
//判断该购物车中是否含有该商品
if(!productMaps.containsKey(p)){
productMaps.put(p,n); //自动装箱
}else{
//车中有该商品
int before = productMaps.get(p).intValue(); //添加商品之前的该商品数量
int after = before + n;
productMaps.put(p,after); //key重复value覆盖。
}
//修改总价
totalPrice += p.getPrice()*n;
}
//删除1个商品
public void remove(Product p){
remove(p,1);
}
//删除N个商品
public void remove(Product p,int n){
int before = productMaps.get(p).intValue();
//如果购物车中的某商品数量和n是相等的,必须删除整个条目
if(before==n){
productMaps.remove(p);
}else{
int after = before - n;
productMaps.put(p,after);
}
//修改总价
totalPrice -= p.getPrice()*n;
}
//清空购物车
public void clear(){
productMaps.clear();
totalPrice = 0.0;
}
//输出购物车详单.
/*
格式:
购物详单:
10 苹果 10元
3 西瓜 30元
................
总价:40...
*/
public void print(){
StringBuffer sb = new StringBuffer();
sb.append("购物详单:\n");
//遍历Map
Set<Product> keys = productMaps.keySet();
Iterator<Product> it = keys.iterator();
while(it.hasNext()){
Product k = it.next();
Integer v = productMaps.get(k);
sb.append("\t" + v.intValue() + "\t" + k + "\t" + (v.intValue()*k.getPrice())+"元\n");
}
sb.append("\t\t\t\t\t\t\t\t\t\t\t总价:" + totalPrice+"元");
System.out.println(sb);
}
}
ShoppingTest.java
public class ShoppingTest
{
public static void main(String[] args){
//创建购物车
ShoppingCart cart = new ShoppingCart();
//货物
Product p1 = new Product(1000,"iphone4s",10.0);
Product p2 = new Product(2000,"samsung",3.0);
Product p3 = new Product(3000,"HTC",6.0);
Product p4 = new Product(4000,"小米",2.0);
//开始购物
cart.add(p1);
cart.add(p1,3);
cart.add(p2,1);
cart.add(p3,2);
cart.add(p4,3);
cart.remove(p1);
cart.remove(p1,2);
//打印详单
cart.print();
//清空
cart.clear();
cart.print();
}
}
希望大家多多学习,{:soso_e100:}
|
|