Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;

import java.time.Duration;

public class AmountDiscountMovie extends Movie {
private Money discountAmount;

public AmountDiscountMovie(
String title, Duration runningTime, Money fee,
Money discountAmount, DiscountCondition... discountConditions) {
super(title, runningTime, fee, discountConditions);
this.discountAmount = discountAmount;
}

@Override
protected Money calculateDiscountAmount() {
return discountAmount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.johngrib.objects._05_movie;

public interface DiscountCondition {
boolean isSatisfiedBy(Screening screening);
}
43 changes: 43 additions & 0 deletions src/main/java/com/johngrib/objects/_05_movie/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;
import com.johngrib.objects._04_movie_data_system.MovieType;
import lombok.Getter;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

public abstract class Movie {
private String title;
private Duration runningTime;
@Getter
private Money fee;

private List<DiscountCondition> discountConditions;

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

public Movie(String title, Duration runningTime, Money fee, DiscountCondition... discountConditions) {
this.title = title;
this.runningTime = runningTime;
this.fee = fee;
this.discountConditions = Arrays.asList(discountConditions);
}

public Money calculateMovieFee(Screening screening) {
if (isDiscountable(screening)) {
return fee.minus(calculateDiscountAmount());
}
return fee;
}

private boolean isDiscountable(Screening screening) {
return discountConditions.stream()
.anyMatch(condition -> condition.isSatisfiedBy(screening));
}

abstract protected Money calculateDiscountAmount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;

import java.time.Duration;

public class NoneDiscountMovie extends Movie {
public NoneDiscountMovie(String title, Duration runningTime, Money fee) {
super(title, runningTime, fee);
}

@Override
protected Money calculateDiscountAmount() {
return Money.ZERO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;

import java.time.Duration;

public class PercentDiscountMovie extends Movie {
private double percent;

public PercentDiscountMovie(
String title, Duration runningTime, Money fee, double percent, DiscountCondition... discountConditions) {
super(title, runningTime, fee, discountConditions);
this.percent = percent;
}

@Override
protected Money calculateDiscountAmount() {
return getFee().times(percent);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/johngrib/objects/_05_movie/PeriodCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.johngrib.objects._05_movie;

import java.time.DayOfWeek;
import java.time.LocalTime;

public class PeriodCondition implements DiscountCondition {
private DayOfWeek dayOfWeek;
private LocalTime startTime;
private LocalTime endTime;

public PeriodCondition(DayOfWeek dayOfWeek, LocalTime startTime, LocalTime endTime) {
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.endTime = endTime;
}

@Override
public boolean isSatisfiedBy(Screening screening) {
return dayOfWeek.equals(screening.getWhenScreened().getDayOfWeek())
&& startTime.isBefore(screening.getWhenScreened().toLocalTime())
&& endTime.isAfter(screening.getWhenScreened().toLocalTime());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/johngrib/objects/_05_movie/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;
import com.johngrib.objects._04_movie_data_system.Customer;

public class Reservation {
private Customer customer;
private Screening screening;
private Money fee;
private int audienceCount;

public Reservation(Customer customer, Screening screening, Money fee, int audienceCount) {
this.customer = customer;
this.screening = screening;
this.fee = fee;
this.audienceCount = audienceCount;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/johngrib/objects/_05_movie/Screening.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.johngrib.objects._05_movie;

import com.johngrib.objects._02_movie.Money;
import com.johngrib.objects._04_movie_data_system.Customer;
import lombok.Getter;

import java.time.LocalDateTime;

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

public Reservation reserve(Customer customer, int audienceCount) {
return new Reservation(customer, this, calculateFee(audienceCount), audienceCount);
}

private Money calculateFee(int audienceCount) {
return movie.calculateMovieFee(this).times(audienceCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.johngrib.objects._05_movie;

public class SequenceCondition implements DiscountCondition {
private int sequence;

public SequenceCondition(int sequence) {
this.sequence = sequence;
}

@Override
public boolean isSatisfiedBy(Screening screening) {
return sequence == screening.getSequence();
}
}