[์˜ค๋ธŒ์ ํŠธ 4์žฅ] - ์„ค๊ณ„ ํ’ˆ์งˆ๊ณผ ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„

2024. 7. 7. 20:52ใ†๋„์„œ

[์˜ค๋ธŒ์ ํŠธ 4์žฅ] - ์„ค๊ณ„ ํ’ˆ์งˆ๊ณผ ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„

 

 

์ด๋ฒˆ ์žฅ์€ ์˜ํ™” ์˜ˆ๋งค ์‹œ์Šคํ…œ์„ ์ฑ…์ž„์ด ์•„๋‹Œ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์„ค๊ณ„๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์„ค๊ณ„ํ•œ ๊ตฌ์กฐ์™€ ์–ด๋–ค ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๋Š” ์ฑ•ํ„ฐ์ž…๋‹ˆ๋‹ค. 

 

 

 ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์˜ํ™” ์˜ˆ๋งค ์‹œ์Šคํ…œ

 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„๋Š” '๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€'๋ฅผ ๋ฌป๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๋จผ์ € Movie์— ์ €์žฅ๋  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜์ž. ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ ‘๊ทผ์ž์™€ ์ˆ˜์ •์ž๋Š” ๋กฌ๋ถ์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

 

@Getter
@Setter
public class Movie {
    private String title;
    private Duration runningTime;
    private Money fee;
    private List<DiscountCondition> discountConditions;

    private MovieType movieType;
    private Money discountAmount;
    private double discountPercent;

}

 

 

ํ• ์ธ ์ •์ฑ…์˜ ์ข…๋ฅ˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์—ด๊ฑฐํ˜• ํƒ€์ž…์„ MovieType์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. 

 

public enum MovieType {
    AMOUNT_DISCOUNT,   // ๊ธˆ์•ก ํ• ์ธ ์ •์ฑ…
    PERCENT_DISCOUNT,  // ๋น„์œจ ํ• ์ธ ์ •์ฑ…
    NONE_DISCOUNT      // ๋ฏธ์ ์šฉ
}

 

 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„์—์„œ๋Š” ๊ฐ์ฒด๊ฐ€ ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์ค‘ํ•œ๋‹ค. movieType์ด ๊ธˆ์•ก ํ• ์ธ ์ •์ฑ…์ด๋ผ๋ฉด discountAmount ๊ฐ’์ด ํ•„์š”ํ•˜๊ณ , ๋น„์œจ ํ• ์ธ ์ •์ฑ…์ด๋ผ๋ฉด discountPercent๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ์บก์Šํ™”๋ฅผ ์œ„ํ•ด ์ ‘๊ทผ์ž์™€ ์ˆ˜์ •์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

 

๋‹ค์Œ์œผ๋กœ ํ• ์ธ ์กฐ๊ฑด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๋จผ์ € ํ˜„์žฌ์˜ ํ• ์ธ ์กฐ๊ฑด์˜ ์ข…๋ฅ˜๋ฅผ ์ €์žฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 

 

public enum DiscountConditionType {
    SEQUENCE, // ์ˆœ๋ฒˆ์กฐ๊ฑด
    PERIOD    // ๊ธฐ๊ฐ„์กฐ๊ฑด
}

 

๋˜ํ•œ ๊ธฐ๊ฐ„์กฐ๊ฑด๊ณผ ์ˆœ๋ฒˆ ์กฐ๊ฑด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋“ค์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. 

 

@Getter
@Setter
public class DiscountCondition {
    private DiscountConditionType type;
    
    private int sequence;
    
    private DayOfWeek dayOfWeek;
    private LocalTime startTime;
    private LocalTime endTime;
}

 

 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Screening, Reservation, Customer ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

 

// Screening.java
@Getter
@Setter
public class Screening {
    private Movie movie;
    private int sequence;
    private LocalDateTime whenScreened;
}

// Reservation.java
@Getter
@Setter
public class Reservation {
    private Customer customer;
    private Screening screening;
    private Money fee;
    private int audienceCount; 
}

// Customer.java
@Getter
@Setter
public class Customer {
    private String name;
    private String id;
}

 

 

 

 

 

 ์˜ํ™”์˜ˆ๋งค ์‹œ๋ฎฌ๋ ˆ์ด์…˜

 

ReservationAgency๋Š” ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋“ค์„ ์กฐํ•ฉํ•ด์„œ ์˜ํ™” ์˜ˆ๋งค ์ ˆ์ฐจ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋‹ค.

 

public class ReservationAgency {
    
    public Reservation reserve(Screening screening, Customer customer, int audienceCount) {
        Movie movie = screening.getMovie();
        
        boolean discountable = false;
        for (DiscountCondition condition: movie.getDiscountConditions()) {
            if (condition.getType() == DiscountConditionType.PERIOD) {
                discountable = screening.getWhenScreened().getDayOfWeek().equals(condition.getDayOfWeek()) && 
                        condition.getStartTime().compareTo(screening.getWhenScreened().toLocalTime()) <= 0 && 
                        condition.getEndTime().compareTo(screening.getWhenScreened().toLocalTime()) >= 0;
            } else {
                discountable = condition.getSequence() == screening.getSequence();
            }
            
            if (discountable) {
                break;
            }
        }
        
        Money fee;
        if (discountable) {
            Money discountAmount = Money.ZERO;
            switch (movie.getMovieType()) {
                case AMOUNT_DISCOUNT:
                    discountAmount = movie.getDiscountAmount();
                    break;
                case PERCENT_DISCOUNT:
                    discountAmount = movie.getFee().times(movie.getDiscountPercent());
                    break;
                case NONE_DISCOUNT:
                    discountAmount = Money.ZERO;
                    break;
            }
            
            fee = movie.getFee().minus(discountAmount).times(audienceCount);
        } else {
            fee = movie.getFee().times(audienceCount);
        }
        
        return new Reservation(customer, screening, fee, audienceCount);
    }
}

 

 

 

 

 ์„ค๊ณ„ ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„

 

'์บก์Šํ™”', '์‘์ง‘๋„', '๊ฒฐํ•ฉ๋„'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฑ…์ž„ ์ฃผ๋„ ์„ค๊ณ„์™€ ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„๋ฅผ ๋น„๊ตํ•ด๋ณด์ž. 

 

 

 

 

 ์บก์Šํ™”

 

์ƒํƒœ์™€ ํ–‰๋™์„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์•ˆ์— ๋ชจ์œผ๋Š” ์ด์œ ๋Š” ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๊ฐ์ถฐ์„œ ๋ณ€๊ฒฝ ํŒŒ๊ธ‰ํšจ๊ณผ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ๋‹ค. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋ถ€๋ถ„์„ '๊ตฌํ˜„'์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ  ์ƒ๋Œ€์ ์œผ๋กœ ์•ˆ์ •์ ์ธ ๋ถ€๋ถ„์„ '์ธํ„ฐํŽ˜์ด์Šค'๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. 

 

์ด๋ฅผ ์œ„ํ•œ ์žฅ์น˜๋กœ '์บก์Šํ™”'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์›๋ฆฌ๋Š” ๋ถˆ์•ˆ์ •ํ•œ ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ์„ ์•ˆ์ •์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค ๋’ค๋กœ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

'์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋ชฉํ‘œ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด๋ž€ ๋‘๋ ค์›€ ์—†์ด, ์ฃผ์ €ํ•จ ์—†์ด, ์ €ํ•ญ๊ฐ ์—†์ด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์„ ๋งํ•œ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋™๋ฃŒ๋Š” ์บก์Šํ™”๋‹ค. ์บก์Šํ™”๋Š” ์–ด๋–ค ๊ฒƒ์„ ์ˆจ๊ธฐ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š”๋ฐ ์ด๋ฅผ ํ†ตํ•ด ๋œป๋ฐ–์˜ ํ”ผํ•ด๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์„ ์‚ฌ์ „์— ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‘์ง‘๋„, ๊ฒฐํ•ฉ๋„, ์ค‘๋ณต ์—ญ์‹œ ํ›Œ๋ฅญํ•œ ์ฝ”๋“œ๋ฅผ ๊ทœ์ •ํ•˜๋Š” ๋ฐ ํ•ต์‹ฌ์ ์ธ ํ’ˆ์งˆ์ธ ๊ฒƒ์ด ์‚ฌ์‹ค์ด์ง€๋งŒ ์บก์Šํ™”๋Š” ์šฐ๋ฆฌ๋ฅผ ์ข‹์€ ์ฝ”๋“œ๋กœ ์•ˆ๋‚ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ œ1์›๋ฆฌ๋‹ค.'

 

 

 

 

 ์‘์ง‘๋„์™€ ๊ฒฐํ•ฉ๋„

 

์‘์ง‘๋„๋Š” ๋ชจ๋“ˆ์— ํฌํ•จ๋œ ๋‚ด๋ถ€ ์š”์†Œ๋“ค์ด ์—ฐ๊ด€๋ผ ์žˆ๋Š” ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ฒฐํ•ฉ๋„๋Š” ์˜์กด์„ฑ์˜ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ง€์‹์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒ™๋„๋‹ค. 

 

๋ณ€๊ฒฝ์˜ ๊ด€์ ์—์„œ ์‘์ง‘๋„๋ž€ '๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•  ๋•Œ ๋ชจ๋“ˆ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ณ€๊ฒฝ์˜ ์ •๋„'๋กœ ์ธก์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด ํ•˜๋‚˜์˜ ๋ณ€๊ฒฝ์œผ๋กœ ๋ชจ๋“ˆ ์ „์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค๋ฉด ์‘์ง‘๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ์ด๋‹ค. ์‘์ง‘๋„๊ฐ€ ๋†’์€ ์„ค๊ณ„์—์„œ๋Š” ํ•˜๋‚˜์˜ ์š”๊ตฌ์‚ฌํ•ญ ๋ณ€๊ฒฝ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ์˜ค์ง ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค. 

 

๊ฒฐํ•ฉ๋„ ์—ญ์‹œ ๋ณ€๊ฒฝ์˜ ๊ด€์ ์—์„œ 'ํ•œ ๋ชจ๋“ˆ์ด ๋ณ€๊ฒฝ๋˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ๋ณ€๊ฒฝ์„ ์š”๊ตฌํ•˜๋Š” ์ •๋„'๋กœ ์ธก์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด๋–ค ๋ชจ๋“ˆ์„ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋“ค์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์ด๋Š” ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์€ ๊ฒƒ์ด๋‹ค.

๋ณดํ†ต ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์— ์˜ํ–ฅ์ด ๋ผ์น˜๋ฉด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์€ ๊ฒƒ์ด๊ณ  ํผ๋ธ”๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ๋งŒ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์— ์˜ํ–ฅ์ด ๋ผ์น  ๋•Œ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ๋‹ค๊ณ  ํ‘œํ•œํ•œ๋‹ค. 

 

 

 

 

 ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์˜ํ™” ์˜ˆ๋งค ์‹œ์Šคํ…œ์˜ ๋ฌธ์ œ์ 

 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์„ค๊ณ„๋Š” ์บก์Šํ™”๋ฅผ ์œ„๋ฐ˜ํ•˜๊ณ  ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์ธํ„ฐํŽ˜์ด์Šค์— ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ํ•œ๋‹ค. 

 

์ด ๋•Œ๋ฌธ์— ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ๋ฌธ์ œ์ ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

- ์บก์Šํ™” ์œ„๋ฐ˜

- ๋†’์€ ๊ฒฐํ•ฉ๋„

- ๋‚ฎ์€ ์‘์ง‘๋„

 

 

๋กฌ๋ถ์„ ํ†ตํ•ด ์ ‘๊ทผ์ž์™€ ์ˆ˜์ •์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์€ ์บก์Šํ™”์˜ ์›์น™์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. getFee์™€ setFee๋Š” ํผ๋ธ”๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค์— ๋…ธ๊ณจ์ ์œผ๋กœ fee๋ผ๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋“œ๋Ÿฌ๋‚ด๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๊ณ ์˜จ๋‹ค๋ฉด ์ด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋Š” fee๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์ „๋ถ€ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ๋†’์€ ๊ฒฐํ•ฉ๋„๋กœ ์ƒ๊ธฐ๋Š” ์ผ์ด๋‹ค.

 

์—˜๋นˆ ํ™€๋Ÿฝ์€ ์ด์ฒ˜๋Ÿผ ์ ‘๊ทผ์ž์™€ ์ˆ˜์ •์ž์— ๊ณผ๋„ํ•˜๊ฒŒ ์˜์กดํ•˜๋Š” ์„ค๊ณ„ ๋ฐฉ์‹์„ '์ถ”์ธก์— ์˜ํ•œ ์„ค๊ณ„ ์ „๋žต'์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.์ด ์ „๋žต์€ ๊ฐ์ฒด์˜ ํ˜‘๋ ฅ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋ผ๋Š” ๊ฐ€์ •ํ•˜์— ์„ค๊ณ„๋ฅผ ํ•œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚ด๋ถ€ ๊ตฌํ˜„์ด ํผ๋ธ”๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค์— ๊ทธ๋Œ€๋กœ ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋˜๋ฉด์„œ ์บก์Šํ™”์˜ ์›์น™์„ ์œ„๋ฐ˜ํ•˜๋Š” ๋ณ€๊ฒฝ์— ์ทจ์•ฝํ•œ ์„ค๊ณ„๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. 

 

์„œ๋กœ ๋‹ค๋ฅธ ์ด์œ ๋กœ ๋ณ€๊ฒฝ๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ ์•ˆ์— ๊ณต์กดํ•  ๋•Œ ๋ชจ๋“ˆ์˜ ์‘์ง‘๋„๊ฐ€ ๋‚ฎ๋‹ค๊ณ  ๋งํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ ๋ชจ๋“ˆ์˜ ์‘์ง‘๋„๋ฅผ ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์‚ดํŽด๋ด์•ผ ํ•œ๋‹ค. 

 

ReservationAgency๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆ˜์ •์‚ฌํ•ญ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์— ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

- ํ• ์ธ ์ •์ฑ…์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ

- ํ• ์ธ ์ •์ฑ…๋ณ„๋กœ ํ• ์ธ ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ

- ํ• ์ธ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ

- ํ• ์ธ ์กฐ๊ฑด๋ณ„๋กœ ํ• ์ธ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ

- ์˜ˆ๋งค ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ

 

ํ• ์ธ ์ •์ฑ…์„ ์„ ํƒํ•˜๋Š” ์ฝ”๋“œ์™€ ํ• ์ธ ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ•จ๊ป˜ ์กด์žฌํ•˜๊ธฐ์— ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ…์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…์ด ํ• ์ธ ์กฐ๊ฑด์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ๋‹ค. 

์ด์ฒ˜๋Ÿผ ์–ด๋–ค ์š”๊ตฌ์‚ฌํ•ญ ๋ณ€๊ฒฝ์„ ์ˆ˜์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜ ์ด์ƒ์˜ ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์„ค๊ณ„์˜ ์‘์ง‘๋„๊ฐ€ ๋‚ฎ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

 

 

 ์Šค์Šค๋กœ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฑ…์ž„์ง€๋Š” ๊ฐ์ฒด

 

 

์‘์ง‘๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด ๋‚ด๋ถ€์— ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ณด๋‹ค ๊ฐ์ฒด๊ฐ€ ํ˜‘๋ ฅ์— ์ฐธ์—ฌํ•˜๋ฉด์„œ ์ˆ˜ํ–‰ํ•  ์ฑ…์ž„์„ ์ •์˜ํ•˜๋Š” ์˜คํผ๋ ˆ์ด์…˜์— ์ดˆ์ ์„ ๋งž์ถฐ์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ "์ด ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•˜๋Š”๊ฐ€?"๋ผ๋Š” ์งˆ๋ฌธ์€ ๋‹ค์Œ์˜ ๋‘ ๊ฐœ๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

 

- ์ด ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•˜๋Š”๊ฐ€?

- ์ด ๊ฐ์ฒด๊ฐ€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์˜คํผ๋ ˆ์ด์…˜์€ ๋ฌด์—‡์ธ๊ฐ€? 

 

๋‘ ์งˆ๋ฌธ์„ ์กฐํ•ฉํ•˜๋ฉด ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์ €์žฅ๋œ ์ƒํƒœ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์˜คํผ๋ ˆ์ด์…˜์˜ ์ง‘ํ•ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

 

๋จผ์ € DiscountCondition ํƒ€์ž…์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์˜คํผ๋ ˆ์ด์…˜์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด์ž. ๋‘ ๊ฐ€์ง€ ํ• ์ธ ์กฐ๊ฑด์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋„๋ก isDiscountable ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

 

@Getter
@Setter
public class DiscountCondition {
    private DiscountConditionType type;

    private int sequence;

    private DayOfWeek dayOfWeek;
    private LocalTime startTime;
    private LocalTime endTime;
    
    public boolean isDiscountable(DayOfWeek dayOfWeek, LocalTime time) {
        if (type != DiscountConditionType.PERIOD) {
            throw new IllegalArgumentException();
        }
        
        return this.dayOfWeek.equals(dayOfWeek) && this.startTime.compareTo(time) <= 0 && this.endTime.compareTo(time) >= 0;
    }
    
    public boolean isDiscountable(int sequence) {
        if (type != DiscountConditionType.SEQUENCE) {
            throw  new IllegalArgumentException();
        }
        
        return this.sequence == sequence;
    }
}

 

 

์ด์ œ Movie๋ฅผ ๋ณ€๊ฒฝํ•ด๋ณด์ž. Movie์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์˜คํผ๋ ˆ์ด์…˜์€ ๋ฌด์—‡์ธ๊ฐ€? Movie๊ฐ€ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์˜ํ™” ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ์˜คํผ๋ ˆ์ด์…˜๊ณผ ํ• ์ธ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์˜คํผ๋ ˆ์ด์…˜์ด ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋‹ค. 

 

@Getter
@Setter
public class Movie {
    private String title;
    private Duration runningTime;
    private Money fee;
    private List<DiscountCondition> discountConditions;

    private MovieType movieType;
    private Money discountAmount;
    private double discountPercent;

    public Money calculateAmountDiscountedFee() {
        if (movieType != MovieType.AMOUNT_DISCOUNT) {
            throw new IllegalArgumentException();
        }
        
        return fee.minus(discountAmount);
    }

    public Money calculatePercentDiscountedFee() {
        if (movieType != MovieType.PERCENT_DISCOUNT) {
            throw new IllegalArgumentException();
        }

        return fee.times(discountPercent);
    }
    
    public Money calculateNoneDiscountFee() {
        if (movieType != MovieType.NONE_DISCOUNT) {
            throw new IllegalArgumentException();
        }
        
        return Money.ZERO;
    }
    
    public boolean isDiscountable(LocalDateTime whenScreened, int sequence) {
        for(DiscountCondition condition : discountConditions) {
            if (condition.getType() == DiscountConditionType.PERIOD) {
                if (condition.isDiscountable(whenScreened.getDayOfWeek(), whenScreened.toLocalTime())) {
                    return true;
                }
            } else {
                if (condition.isDiscountable(sequence)) {
                    return true;
                }
            }
        }
        
        return false;
    }

}

 

 

Screening ์—ญ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์˜คํผ๋ ˆ์ด์…˜์„ ๊ตฌํ˜„ํ•˜์—ฌ ์ž์œจ์„ฑ์„ ๋†’์ด์ž.

 

@Getter
@Setter
public class Screening {
    private Movie movie;
    private int sequence;
    private LocalDateTime whenScreened;

    public Money calculateFee(int audienceCount) {
        switch (movie.getMovieType()) {
            case AMOUNT_DISCOUNT:
                if (movie.isDiscountable(whenScreened, sequence)) {
                    return movie.calculateAmountDiscountedFee().times(audienceCount);
                }
                break;
            case PERCENT_DISCOUNT:
                if (movie.isDiscountable(whenScreened, sequence)) {
                    return movie.calculatePercentDiscountedFee().times(audienceCount);
                }
                break;
            case NONE_DISCOUNT:
                return movie.calculateNoneDiscountFee().times(audienceCount);
        }
        
        return movie.calculateNoneDiscountFee().times(audienceCount);
    }
}

 

 

์ด์ œ ์ด ์ž์œจ์ ์ธ ๊ฐ์ฒด๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ์˜ํ™”๋ฅผ ์˜ˆ๋งคํ•ด๋ณด์ž.

 

public class ReservationAgency {
    public Reservation reserve(Screening screening, Customer customer, int audienceCount) {
        Money fee = screening.calculateFee(audienceCount);
        return new Reservation(customer, screening, fee, audienceCount);
    }
}

 

๊ฒฐํ•ฉ๋„ ์ธก๋ฉด์—์„œ ReservationAgency์— ๋ชฐ๋ ค์žˆ๋˜ ์˜์กด์„ฑ์ด ์ถฉ๋ถ„ํžˆ ๊ฐœ์„ ๋˜์—ˆ๋‹ค. 

 

 

 

 ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๋ถ€์กฑํ•˜๋‹ค.

 

์บก์Šํ™” ๊ด€์ ์—์„œ ๋‘ ๋ฒˆ์งธ ์„ค๊ณ„๊ฐ€ ์ฒซ ๋ฒˆ์งธ๋ณด๋‹ค ํ–ฅ์ƒ๋œ ๊ฑด ์‚ฌ์‹ค์ด์ง€๋งŒ ์—ฌ์ „ํžˆ ์บก์Šํ™”๋ฅผ ์œ„๋ฐ˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•œ๋‹ค.

 

Screening์˜ isDiscountable(DayofWeek dayOfWeek, LocalTime time) ๋ฉ”์„œ๋“œ๋Š” ๋‚ด๋ถ€์— dayOfWeek, time ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ์„ ๋…ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค.  Movie์—ญ์‹œ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์ธํ„ฐํŽ˜์ด์Šค์— ๋…ธ์ถœ์‹œํ‚ค๊ณ  ์žˆ๋‹ค. calculateAmountDiscountedFee๋Š” ์ด๋ฆ„์œผ๋กœ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๋…ธ์ถœ์‹œํ‚ค๊ณ  ์žˆ๋‹ค. ํ•ด๋‹น ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•œ๋‹ค๋ฉด? ์ƒˆ๋กœ์šด ํ• ์ธ์ •์ฑ…์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด? ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์—ญ์‹œ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

๋‚ด๋ถ€ ์†์„ฑ์„ ์บก์Šํ™”ํ–ˆ๋”๋ผ๋„ ๋‚ด๋ถ€ ๊ตฌํ˜„์˜ ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด ์™ธ๋ถ€์˜ ๊ฐ์ฒด๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋ฉด ์บก์Šํ™”๋ฅผ ์œ„๋ฐ˜ํ•œ ๊ฒƒ์ด๋‹ค. ์„ค๊ณ„์—์„œ ๋ณ€ํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ๊ณ ๋ คํ•˜๊ณ  ๋ณ€ํ•˜๋Š” ๊ฐœ๋…์„ ์บก์Šํ™”ํ•ด์•ผ ํ•œ๋‹ค. ์œ„ ๋‘ ๊ฐœ์˜ ๋ฉ”์„œ๋“œ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ•œ ๋‹จ๊ณ„ ์ถ”์ƒํ™”ํ•ด์„œ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ์ˆจ๊ธฐ๋Š” ๊ฒƒ์ด ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ๋ณ€๊ฒฝ์˜ ํŒŒ๊ธ‰ํšจ๊ณผ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. 

 

 

 

 

 ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„์˜ ๋ฌธ์ œ์ 

 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„๊ฐ€ ๋ณ€๊ฒฝ์— ์ทจ์•ฝํ•œ ์ด์œ ๋Š” ๋‘ ๊ฐ€์ง€๋‹ค.

- ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์„ค๊ณ„๋Š” ๋ณธ์งˆ์ ์œผ๋กœ ๋„ˆ๋ฌด ์ด๋ฅธ ์‹œ๊ธฐ์— ๋ฐ์ดํ„ฐ์— ๊ด€ํ•ด ๊ฒฐ์ •ํ•˜๋„๋ก ๊ฐ•์š”ํ•œ๋‹ค.

- ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์˜ ์„ค๊ณ„์—์„œ๋Š” ํ˜‘๋ ฅ์ด๋ผ๋Š” ๋ฌธ๋งฅ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ๊ณ ๋ฆฝ์‹œํ‚จ ์ฑ„ ์˜คํผ๋ ˆ์ด์…˜์„ ๊ฒฐ์ •ํ•œ๋‹ค. 

 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„์˜ ์‹œ์ž‘์€ "์ด ๊ฐ์ฒด๊ฐ€ ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€?"์ด๋‹ค. ์‹œ์ž‘๋ถ€ํ„ฐ ๋‚ด๋ถ€ ๊ตฌํ˜„์— ์ดˆ์ ์„ ๋งž์ถฐ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„ ๋ฐฉ์‹์— ์ต์ˆ™ํ•œ ๊ฐœ๋ฐœ์ž๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜๋Š” ์ ˆ์ฐจ์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์„ ๋”ฐ๋ฅธ๋‹ค. ๊ฐ์ฒด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ง‘ํ•ฉ์ฒด์ผ ๋ฟ์ด๊ณ  ์ ‘๊ทผ์ž์™€ ์ˆ˜์ •์ž๋ฅผ ๊ณผ๋„ํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋กœ์ธํ•ด ์บก์Šํ™”๋Š” ๋ฌด๋„ˆ์ง„๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์„ค๊ณ„์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜คํผ๋ ˆ์ด์…˜์„ ๊ฐ์ฒด ๋‚ด๋ถ€์— ๋งŒ๋“ค๊ฒŒ ๋˜๋”๋ผ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์˜คํผ๋ ˆ์ด์…˜์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€ ๊ตฌํ˜„์ด ์ธํ„ฐํŽ˜์ด์Šค์— ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋œ๋‹ค. ์ด ๋˜ํ•œ ์บก์Šํ™”๋ฅผ ์œ„๋ฐ˜ํ•˜๊ฒŒ ๋˜๊ณ  ๋ณ€๊ฒฝ์— ์ทจ์•ฝํ•ด์ง„๋‹ค. 

 

์˜ฌ๋ฐ”๋ฅธ ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์˜ ๋ฌด๊ฒŒ ์ค‘์‹ฌ์€ ํ•ญ์ƒ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€๊ฐ€ ์•„๋‹Œ ์™ธ๋ถ€์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ์„ค๊ณ„๋Š” ๋‚ด๋ถ€๋กœ ํ–ฅํ•œ๋‹ค. ๊ฐ์ฒด์˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋‚ด๋ถ€ ๊ตฌํ˜„์— ์ข…์†๋ผ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€ ๊ตฌํ˜„์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด ๋ชจ๋‘๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ๋ฐ–์— ์—†๋˜ ๊ฒƒ์ด๋‹ค.