✍版权声明:@CSDN遊光
版权声明:
@Kcx@遊光@胤殁@Karl@Karlcixio
有幸您能来看我的博客,本博客仅供大家学术交流,如您有需要将本博客用作它用,请与我联系!
我的联系方式:
微信搜索公众号:Karlcixio
也可以直接扫描以下二维码:
----------------------------------
个人 QQ:1005589159
?第零步:审题!!!
首先,我们先来看看题目要求,然后再罗列我们大体需要做些什么:
- 创建实体类Goods,并写入相关变量及方法;
- 创建测试类Shopping,并写入测试过程;
?第一步:实体类Goods
package 【PackageName】;
import java.util.Objects;
public class Goods {
private String gid;
private String gname;
private float gprice;
public Goods(){
}
public Goods(String gid, String gname, float gprice) {
this.gid = gid;
this.gname = gname;
this.gprice = gprice;
}
public String getGid() {
return gid;
}
public void setGid(String gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public float getGprice() {
return gprice;
}
public void setGprice(float gprice) {
this.gprice = gprice;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Goods goods = (Goods) o;
return Float.compare(goods.gprice, gprice) == 0 &&
Objects.equals(gid, goods.gid) &&
Objects.equals(gname, goods.gname);
}
@Override
public int hashCode() {
return Objects.hash(gid, gname, gprice);
}
@Override
public String toString() {
return "\n[" +
"* 编号:" + gid +
";名称:" + gname +
";价格:" + gprice +
"]\n\t\t\t\t\t\t\t\t\t -->数量";
}
}
?第二步:测试类Shopping
package 【PackageName】;
import java.util.*;
public class Shopping {
private Map<Goods, Integer> goodsMap;
private static