
public StringBuilder data
public float total
public Cart(){
data=new StringBuilder()
}
public void buy(Goods g){
g.gtotal=g.gnum*g.gprice
total=total+g.gtotal
data.append("[")
data.append(g.gname+"|")
data.append(g.gprice+"|")
data.append(g.gnum+"|") //还是竖线看着方便
data.append(g.gtotal)
data.append("]")
}
public void delete(Goods g){
int s=data.indexOf(g.gname)
int e=data.indexOf("]", s)
data.delete(s-1, e+1)
total=total-g.gtotal //删除商品 ,需要修改总额
}
public void update(Goods g){
data.replace(3, 10, "["+g.gname+"|"+g.gprice+"|"+g.gnum+"|"+g.gtotal)
}
public void show(){
System.out.print("总计金额:" + total + " ")
System.out.println(data)
}
}
//Excute类里有点小错误,
//总觉得update方法 不对头,你想怎么做?
对于私有的属性可以在类中增加get和set方法例如:
public class Product
{
private long PID = -1
public long getPID(){return PID}
public void setPID(long value){this.PID = value}
}
//通过调整get和set方法就可以实现对私有属性的访问控制
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)