Skip to content

Commit

Permalink
Composite 패턴 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
ZANGZANGS committed Dec 17, 2021
1 parent 07217d8 commit c0f1966
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/gof/dp/composite/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gof.dp.composite;

import java.util.ArrayList;

public class Category extends ProductCategory{

ArrayList<ProductCategory> list;

public Category(int id, String name, int price){
super(id, name, price);
list = new ArrayList<ProductCategory>();
}

@Override
public void addProduct(ProductCategory product) {
list.add(product);
}

@Override
public void removeProduct(ProductCategory product) {
list.remove(product);
}

@Override
public int getCount() {
return list.stream().mapToInt(ProductCategory::getCount).sum();
}

@Override
public String getName() {
return list.toString();
}

@Override
public int getPrice() {
return list.stream().mapToInt(ProductCategory::getPrice).sum();
}

@Override
public int getId() {
return 0;
}
}
61 changes: 61 additions & 0 deletions src/gof/dp/composite/CategoryClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gof.dp.composite;

public class CategoryClientTest {
public static void main(String[] args) {

//여성상품 카테고리
ProductCategory womanCategory = new Category(1234, "Woman", 0);

ProductCategory clothesCategoryW = new Category(2345, "Clothes", 0); //여성상품 - 상품 카테고리 생성
ProductCategory bagCategoryW = new Category(3456, "Bag", 0);
ProductCategory shoesCategoryW = new Category(9876, "Shoes", 0);
womanCategory.addProduct(clothesCategoryW);
womanCategory.addProduct(bagCategoryW);
womanCategory.addProduct(shoesCategoryW);

ProductCategory bag1 = new Product(121, "HERMES", 500000); //여성 가방 상품 카테고리 상품 추가
ProductCategory bag2 = new Product(122, "LOUISVUITTON", 500000);
ProductCategory bag3 = new Product(123, "GUCCI", 500000);
bagCategoryW.addProduct(bag1);
bagCategoryW.addProduct(bag2);
bagCategoryW.addProduct(bag3);

ProductCategory shoes1 = new Product(121, "Nike", 100000); //여성 신발 상품 카테고리 상품 추가
ProductCategory shoes2 = new Product(122, "ADIDAS", 200000);
ProductCategory shoes3 = new Product(123, "GUCCI", 300000);
shoesCategoryW.addProduct(shoes1);
shoesCategoryW.addProduct(shoes2);
shoesCategoryW.addProduct(shoes3);

System.out.println(womanCategory.getCount());
System.out.println(womanCategory.getPrice());

//남성상품 카테고리
ProductCategory manCategory = new Category(5678, "Man", 0);

ProductCategory clothesCategoryM = new Category(23450, "Clothes", 0); //남성상품 - 상품 카테고리 생성
ProductCategory bagCategoryM = new Category(34560, "Bag", 0);
ProductCategory shoesCategoryM = new Category(98760, "Shoes", 0);
manCategory.addProduct(clothesCategoryM);
manCategory.addProduct(bagCategoryM);
manCategory.addProduct(shoesCategoryM);

ProductCategory bag4 = new Product(124, "BALENCIA", 500000); //남성 가방 상품 카테고리 상품 추가
ProductCategory bag5 = new Product(125, "PRADA", 500000);
ProductCategory bag6 = new Product(126, "MULBERRY", 500000);
bagCategoryM.addProduct(bag4);
bagCategoryM.addProduct(bag5);
bagCategoryM.addProduct(bag6);

ProductCategory shoes4 = new Product(124, "BALENCIA", 400000); //남성 신발 상품 카테고리 상품 추가
ProductCategory shoes5 = new Product(125, "PRADA", 500000);
ProductCategory shoes6 = new Product(126, "BALLY", 600000);
shoesCategoryM.addProduct(shoes4);
shoesCategoryM.addProduct(shoes5);
shoesCategoryM.addProduct(shoes6);

System.out.println(manCategory.getCount());
System.out.println(manCategory.getPrice());

}
}
40 changes: 40 additions & 0 deletions src/gof/dp/composite/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gof.dp.composite;

public class Product extends ProductCategory{

//생성자
public Product(int id, String name, int price){
super(id,name,price);
}


@Override
public void addProduct(ProductCategory product) {

}

@Override
public void removeProduct(ProductCategory product) {

}

@Override
public int getCount() {
return 1;
}

@Override
public String getName() {
return name;
}

@Override
public int getPrice() {
return price;
}

@Override
public int getId() {
return id;
}
}
22 changes: 22 additions & 0 deletions src/gof/dp/composite/ProductCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gof.dp.composite;

public abstract class ProductCategory {
int id;
String name;
int price;

public ProductCategory(int id, String name, int price){
this.id = id;
this.name = name;
this.price = price;
}

public abstract void addProduct(ProductCategory product);
public abstract void removeProduct(ProductCategory product);
public abstract int getCount();
public abstract String getName();
public abstract int getPrice();
public abstract int getId();


}

0 comments on commit c0f1966

Please sign in to comment.