⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcar.java

📁 购物车的核心代码演示了HASHMAP的用法
💻 JAVA
字号:
import java.util.HashMap;
class product{
 	 private  String txm;
	 private String name;
	 private float price;
	 private String chandi;
	 public  product(String txm, String name,float price,String chandi){
	 	this.txm=txm;
	 	this.name=name;
	 	this.price=price;
	 	this.chandi=chandi;
	 }
	 public String getTxm(){
	 	return this.txm;
	 }
	 public String getName(){
	 	return this.name;
	 }
	 public float getPrice(){
	 	return this.price;
	 }
	 public String getChandi(){
	 	return this.chandi;
	 }
	 
	 
	
}
//购物车的实现
class item{
private product aa;
private int num;
item(product aa,int num){
	this.aa=aa;
	this.num=num;
}
public int increase(int n){
	return num+=n;
	
}


	
}

//
abstract class shoppingcart{
	abstract void add(product aa,int num);
	abstract void remove(product aa,int num);
	abstract float sum();
}

class cart extends shoppingcart{
	HashMap map;
	cart(){
		map=new HashMap();
	}
	
	 void add(product aa,int num){
	 	//
	 	if(map.containsKey(aa.getTxm())){
	 	item kk=cart.get(aa.getTxm());
	 	kk.increase(num);	
	 		
	 	}
	 	else{
	 		item dd=new item(aa,num);
	 		map.put(aa.getTxm(),dd);
	 	}
	 	
	 	
	 }
	 void desplay(){
	 	//map.keySet()
	 //	map.values()
	 //	map.entrySet()
	 	
	 }
	 void remove(product aa,int num){
	 	
	 	
	 	
	 }
	float sum(){
		
		ruturn 0.0f;
	}
}









class test{
	public static void main(String args[]){
		//product aa=new product();
		product coca =new product("001","cocola",3.0f,"wuxi");
		product fbm=new product("002","kangshifu",1.9f,"tianjin");
		cart cc=new cart();
		cc.add(coca,10);
	    cc.add(coca,10);
	    cc.add(coca,10);
	    cc.add(fbm,10);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -