public interface IMoudle {
String getName();
void add(IMoudle moudle);
void remove(IMoudle moudle);
int totalPrice();
}
public class Moudle implements IMoudle {
private String name;
private int price;
private List<IMoudle> childrenMoudleList;
public Moudle(int price, String name) {
this.name = name;
this.price = price;
childrenMoudleList = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(IMoudle moudle) {
childrenMoudleList.add(moudle);
}
@Override
public void remove(IMoudle moudle) {
childrenMoudleList.remove(moudle);
}
public int totalPrice() {
if (CollectionUtils.isEmpty(childrenMoudleList)) {
return price;
}
return price+childrenMoudleList.stream().collect(Collectors.summingInt(IMoudle::totalPrice));
}
}
public class Battery extends Moudle{
public Battery(int price, String name) {
super(price,name);
}
}
public class CPU extends Moudle {
public CPU(int price, String name) {
super(price,name);
}
}
public class EarPhone extends Moudle{
public EarPhone(int price, String name) {
super(price,name);
}
}
public class Memory extends Moudle{
public Memory(int price, String name) {
super(price,name);
}
}
public class Phone extends Moudle{
public Phone(int price, String name) {
super(price,name);
}
}
public class App {
public static void main(String[] args) {
IMoudle phone=new Phone(100,"Iphone");
IMoudle earPhone=new EarPhone(200,"Earphone");
IMoudle cpu=new CPU(300,"cpu A8");
IMoudle memory=new Memory(400,"cpu A8");
IMoudle battery=new Battery(500,"battery");
cpu.add(memory);
phone.add(cpu);
phone.add(earPhone);
phone.add(battery);
phone.add(phone);
phone.remove(phone);
System.out.println(phone.totalPrice());
}
}