package com.immo.car;
public class Goods {
private String goodId;
private String goodName;
private double price;
private String goodDesp;
Goods(String goodId,String goodName,double price,String goodDesp){
this.goodId=goodId;
this.goodName=goodName;
this.price=price;
this.goodDesp=goodDesp;
}
Goods(){
}
public String getGoodId() {
return goodId;
}
public void setGoodId(String goodId) {
this.goodId = goodId;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getGoodDesp() {
return goodDesp;
}
public void setGoodDesp(String goodDesp) {
this.goodDesp = goodDesp;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((goodDesp == null) ? 0 : goodDesp.hashCode());
result = prime * result + ((goodId == null) ? 0 : goodId.hashCode());
result = prime * result + ((goodName == null) ? 0 : goodName.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (goodDesp == null) {
if (other.goodDesp != null)
return false;
} else if (!goodDesp.equals(other.goodDesp))
return false;
if (goodId == null) {
if (other.goodId != null)
return false;
} else if (!goodId.equals(other.goodId))
return false;
if (goodName == null) {
if (other.goodName != null)
return false;
} else if (!goodName.equals(other.goodName))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
@Override
public String toString() {
return "[商品编号:" + goodId + ", 商品名称:" + goodName + ", 商品价钱:"
+ price + ", 商品描述:" + goodDesp + "]";
}
}
package com.immo.car;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage {
private Set goodset;
public Set getGoodset() {
return goodset;
}
public GoodsManage() {
// TODO Auto-generated constructor stub
}
public void setGoodset(Set goodset) {
this.goodset = goodset;
}
Set s=new HashSet();
public void improtGoods(){
Goods goods1=new Goods("s1001","矿泉水",90,"娃哈哈私人订制矿泉水");
Goods goods2=new Goods("s1002","旺仔牛奶",100,"再看再看就把你喝掉");
Goods goods3=new Goods("s1003","薯片",40,"咔吱脆");
Goods goods4=new Goods("s1004","苹果",30,"糖心苹果");
s.add(goods1);
s.add(goods4);
s.add(goods3);
s.add(goods2);
setGoodset(s);
}
public void displayAllGoods(){
/*Iterator it= s.iterator();
while (it.hasNext()) {
System.out.println("目前:"+it.next());
}
*/ Set set= this.getGoodset();
for (Goods goods : set) {
System.out.println(goods);
}
}
}
package com.immo.car;
public class GoodsInCart {
private Goods goods;
private int num;
GoodsInCart(Goods goods,int num){
this.goods=goods;
this.num=num;
}
GoodsInCart(){
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "GoodsInCart [goods=" + goods + ", num=" + num + "]";
}
}
package com.immo.car;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class ShopingCart {
public Map map;
Scanner sc=new Scanner(System.in);
Boolean flag=true;
GoodsInCart inCa;
int num=0;
public void addGoodsCart(GoodsManage gm){
if (gm.s.isEmpty()) {
System.out.println("请先导入数据"+gm.s.isEmpty());
}else {
System.out.println("友情提示:");
gm.displayAllGoods();
System.out.println("请输入商品编号");
String id=sc.next();
Set set= gm.getGoodset();
for (Goods g : set) {
if (g.getGoodId().equals(id)) {
System.out.println("请输入数量");
num=sc.nextInt();
inCa=new GoodsInCart(g, num);
System.out.println("数量"+inCa.toString());
System.out.println(g);
map.put(id, inCa);
// System.out.println(map.isEmpty());
}
}
//System.out.println(it.next());
}
}
public void Anset(){
System.out.println("请输入商品编号");
String Goodsid=sc.next();
Set set=map.keySet();
for (String st:set) {
if (st.contains(Goodsid)) {
System.out.println("请输入数量");
num=sc.nextInt();
if (num==0) {
map.remove(st);
}
}
}
}
public void balanceGoods(){
Collection c= map.values();
for (GoodsInCart goodsInCart : c) {
double price= goodsInCart.getNum()*goodsInCart.getGoods().getPrice();
System.out.println("共计:"+price);
}
}
}
import java.util.Iterator;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Goods sGoods=new Goods();
ShopingCart s=new ShopingCart();
//GoodsInCart inCart=new GoodsInCart();
GoodsManage manage=new GoodsManage();
manage.improtGoods();
Set set= manage.getGoodset();
for (Goods goods : set) {
System.out.println(goods);
}
//manage.displayAllGoods();
s.addGoodsCart(manage);
}
} |