회원 가입 시스템 설계
package com.tenco.oop_sample.member;
public enum Grade {
BASIC, VIP
}
Member
package com.tenco.oop_sample.member;
import lombok.ToString;
@ToString
public class Member {
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
private Long id;
private String name;
private Grade grade;
public Member(Long id, String name, Grade grade){
this.id = id;
this.name = name;
this.grade = grade;
}
}
MemberRepository
package com.tenco.oop_sample.member.repository;
import com.tenco.oop_sample.member.Member;
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
MemberServiceImpl
package com.tenco.oop_sample.member.service;
import com.tenco.oop_sample.member.Member;
import com.tenco.oop_sample.member.repository.MemberRepository;
public class MemberServiceImpl implements MemberService {
private MemberRepository repository;
// 생성자 의존 주입
public MemberServiceImpl(MemberRepository memberRepository){
this.repository = memberRepository;
}
@Override
public void signUp(Member member) {
repository.save(member);
}
@Override
public Member findByIdMember(Long memberId) {
return repository.findById(memberId);
}
}
DBMemberRepositoryImpl
package com.tenco.oop_sample.member.repository;
import com.tenco.oop_sample.member.Member;
public class DBMemberRepositoryImpl implements MemberRepository{
@Override
public void save(Member member) {
}
@Override
public Member findById(Long memberId) {
return null;
}
}
MainTest1
package com.tenco.oop_sample;
import com.tenco.oop_sample.member.Grade;
import com.tenco.oop_sample.member.Member;
import com.tenco.oop_sample.member.repository.MemberRepository;
import com.tenco.oop_sample.member.repository.MemoryMemberRepositoryImpl;
import com.tenco.oop_sample.member.service.MemberService;
import com.tenco.oop_sample.member.service.MemberServiceImpl;
public class MainTest1 {
public static void main(String[] args) {
MemberRepository memberRepository = MemoryMemberRepositoryImpl.getInstance();
MemberService memberService = new MemberServiceImpl(memberRepository);
// 클라이언트
Member member = new Member(100L, "티모", Grade.VIP);
// 신규 회원 가입 기능 요청
memberService.signUp(member);
// 정상적으로 메모리에 들어갔는지 확인
System.out.println(memberService.findByIdMember(100L));
System.out.println(memberService.findByIdMember(1L));
System.out.println(memberService.findByIdMember(2L));
}
}
FixDiscountPolicyImpl
package com.tenco.oop_sample.order;
import com.tenco.oop_sample.member.Grade;
import com.tenco.oop_sample.member.Member;
public class FixDiscountPolicyImpl implements DiscountPolicy{
private int discountFixAmount = 1500;
@Override
public int discount(Member member ,int price) {
// VIP 회원만 할인 금액 적용
if(member.getGrade() == Grade.VIP){
return discountFixAmount;
}
return 0;
}
}
Order
package com.tenco.oop_sample.order;
public class Order {
private Long memberId;
private String itemName;
private int itemPrice;
private int discountPrice;
public Order(Long memberId, String itemName, int itemPrice, int discountPrice) {
this.memberId = memberId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.discountPrice = discountPrice;
}
// 상품 금액에 할인된 가격 금액을 출력하는 기능
public int calculateDiscount(){
return itemPrice - discountPrice;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
@Override
public String toString() {
return "Order [memberId=" + memberId + ", itemName=" + itemName + ", itemPrice=" + itemPrice
+ ", discountPrice=" + discountPrice + "]";
}
}
PercentDiscountPolicyImpl
package com.tenco.oop_sample.order;
import com.tenco.oop_sample.member.Grade;
import com.tenco.oop_sample.member.Member;
public class PercentDiscountPolicyImpl implements DiscountPolicy{
private double discountPercent = 10.0;
@Override
public int discount(Member member, int price) {
if(member.getGrade() == Grade.VIP){
return (int)(price * (discountPercent / 100));
}
return 0;
}
}
Order
package com.tenco.oop_sample.order;
public class Order {
private Long memberId;
private String itemName;
private int itemPrice;
private int discountPrice;
public Order(Long memberId, String itemName, int itemPrice, int discountPrice) {
this.memberId = memberId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.discountPrice = discountPrice;
}
// 상품 금액에 할인된 가격 금액을 출력하는 기능
public int calculateDiscount(){
return itemPrice - discountPrice;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
@Override
public String toString() {
return "Order [memberId=" + memberId + ", itemName=" + itemName + ", itemPrice=" + itemPrice
+ ", discountPrice=" + discountPrice + "]";
}
}
OrderService
package com.tenco.oop_sample.order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
OrderServiceImpl
package com.tenco.oop_sample.order;
import com.tenco.oop_sample.member.Member;
import com.tenco.oop_sample.member.repository.MemberRepository;
public class OrderServiceImpl implements OrderService{
private final MemberRepository memberRepository;
private final DiscountPolicy discountPolicy;
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy){
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberId);
int discountPrice = discountPolicy.discount(member, itemPrice);
return new Order(memberId, itemName, itemPrice, discountPrice);
}
}
OrderServiceTest
package com.tenco.oop_sample.order;
import com.tenco.oop_sample.member.Grade;
import com.tenco.oop_sample.member.Member;
import com.tenco.oop_sample.member.repository.MemberRepository;
import com.tenco.oop_sample.member.repository.MemoryMemberRepositoryImpl;
public class OrderServiceTest {
public static void main(String[] args) {
MemberRepository memberRepository = MemoryMemberRepositoryImpl.getInstance();
Member member = new Member(1L, "홍길동", Grade.VIP);
// 고정 할인 정책 테스트
DiscountPolicy fixDiscountPolicy = new FixDiscountPolicyImpl();
DiscountPolicy percentDiscountPolicy = new PercentDiscountPolicyImpl();
OrderService orderService1 = new OrderServiceImpl(memberRepository, percentDiscountPolicy);
Order order1 = orderService1.createOrder(1L, "맥북", 10000);
System.out.println("order1 : " + order1);
System.out.println(order1.calculateDiscount());
}
}
AppConfig
package com.tenco.oop_sample.config;
import com.tenco.oop_sample.member.repository.MemberRepository;
import com.tenco.oop_sample.member.repository.MemoryMemberRepositoryImpl;
import com.tenco.oop_sample.member.service.MemberService;
import com.tenco.oop_sample.member.service.MemberServiceImpl;
import com.tenco.oop_sample.order.DiscountPolicy;
import com.tenco.oop_sample.order.FixDiscountPolicyImpl;
import com.tenco.oop_sample.order.OrderService;
import com.tenco.oop_sample.order.OrderServiceImpl;
public class AppConfig {
public MemberRepository gMemberRepository(){
return MemoryMemberRepositoryImpl.getInstance();
}
public MemberService getMemberService(){
return new MemberServiceImpl(gMemberRepository());
}
public DiscountPolicy getDiscardPolicy(){
// 할인 정책이 -> 고정할인
// 변경 -> 정율 할인 빠구면 된다.
return new FixDiscountPolicyImpl(); // 이 부분만 변경하면 된다.
//return new PercentDiscountPolicyImpl();
}
public OrderService getOrderService(){
return new OrderServiceImpl(gMemberRepository(), getDiscardPolicy());
}
}
MainApp
package com.tenco.oop_sample.config;
import com.tenco.oop_sample.member.Grade;
import com.tenco.oop_sample.member.Member;
import com.tenco.oop_sample.member.service.MemberService;
import com.tenco.oop_sample.order.Order;
import com.tenco.oop_sample.order.OrderService;
public class MainApp {
public static void main(String[] args) {
AppConfig appConfig = new AppConfig();
// 클라이언트 입장에서 코딩
// 회원 가입 기능이 필요함
MemberService memberService = appConfig.getMemberService();
Member member = new Member(200L, "도시락", Grade.VIP);
memberService.signUp(member); // 회원가입
// 사용자 주문 요청
OrderService orderService1 = appConfig.getOrderService();
Order order1 = orderService1.createOrder(member.getId(), "아이폰16프로", 100000);
System.out.println(order1);
System.out.println(order1.getDiscountPrice());
}
}
728x90
'Spring boot' 카테고리의 다른 글
Bank App 만들기 ( deployment ) - OAuth 2.0 이란 ( Open Authorization ) (0) | 2024.08.16 |
---|---|
Server To Server (0) | 2024.08.16 |