验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
语言: Java
分类: Web开发技术
最后更新于 2018-12-09 10:09
gistfile1.txt
原始数据 复制代码
package src;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
ShopCar hh=new ShopCar();
hh.addlz();
hh.addtz();
hh.addxz();
hh.print();
hh.move("桃子");
hh.print();
hh.Settleaccounts();
}
}
class ShopCar{
List<Goods> kk=new ArrayList<Goods>();
/**
* 添加
*
*/
public void addtz(){
kk.add(new Goods(80,"桃子"));
}
public void addxz(){
kk.add(new Goods(60,"杏子"));
}
public void addlz(){
kk.add(new Goods(40,"李子"));
}
/**
* 清空
*/
public void clear(){
kk.clear();
System.out.println("你的购物车已清空");
}
/**
* 移除商品
*/
public int move(String name){
for(Goods h:kk){
if(h.name.equals(name)){
kk.remove(h);
System.out.println("已移除成功"+h.name);
break;
}
}
return -1;
}
/**
* 结算
*/
public int Settleaccounts(){
int g=0;
for(Goods h:kk){
g+=h.price;
}
System.out.println(g);
return g;
}
/**
* 购物车数量
*/
public int number(){
System.out.println("购物车商品数量为");
return kk.size();
}
/**
* 输出购物车内容
*/
public void print(){
for(Goods h:kk){
System.out.println(h.name);
}
}
}
class Goods{
int price;
String name;
public Goods(int price, String name) {
this.price = price;
this.name = name;
}
}
/*
abstract class Goods{
int price;
public Goods() {
}
}
class Ww extends Goods{
public Ww(int price) {
this.price=80;
}
}
class Ee extends Goods{
public Ee(int price) {
this.price=60;
}
}
class Rr extends Goods{
public Rr(int price) {
this.price=90;
}
}*/

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助