/**
* 商品類別實(shí)現(xiàn)
*/
public class Category implements ICategory {
// 該分類下的商品
private final List<IGoods> clothingList = new ArrayList<>();
// 分類名稱
private final String categoryName;
public Category(String categoryName) {
this.categoryName = categoryName;
}
@Override
public void addGoods(IGoods goods) {
clothingList.add(goods);
}
@Override
public void removeGoods(IGoods goods) {
clothingList.remove(goods);
}
@Override
public IGoods getGoods(int index) {
return clothingList.get(index);
}
@Override
public int getPrice() {
int price = 0;
for (IGoods goods : clothingList) {
price = price + goods.getPrice();
}
return price;
}
@Override
public String getDes() {
StringBuilder des = new StringBuilder(categoryName + ":(");
for (IGoods goods : clothingList) {
des.append(goods.getDes()).append(",");
}
if (!clothingList.isEmpty()) {
des.deleteCharAt(des.length() - 1);
}
des.append(")");
return des.toString();
}
}
/**
* 具體商品
*/
public class Goods implements IGoods {
private final String name;
private final int price;
public Goods(String name, int price) {
this.name = name;
this.price = price;
}
@Override
public int getPrice() {
return price;
}
@Override
public String getDes() {
return name;
}
}