Controller
@RestController
@RequestMapping("/theater")
@RequiredArgsConstructor
public class TheaterController {
private final TheaterService theaterService;
@GetMapping("hello")
public String welcomeMessage(){
return "Welcome to The Wanted Theater";
}
@GetMapping("enter")
public String enter(){
return theaterService.enter();
}
}
Service
@Service
@RequiredArgsConstructor
public class TheaterService {
private final Theater theater;
public String enter(){
theater.enter(new Audience(new Bag(1000L)),
new TicketSeller(new TicketOffice(20000L, new Ticket(100L))));
return "Have a good time.";
}
}
Handler
public class Audience {
private final Bag bag;
public Audience(Bag bag){
this.bag = bag;
}
public Bag getBag(){ return bag;}
}
public class Bag {
private Long amount;
private final Invitation invitation;
private Ticket ticket;
public Bag(long amount){
this(null, amount);
}
public Bag(Invitation invitation, long amount){
this.invitation = invitation;
this.amount = amount;
}
public boolean hasInvitation() {
return invitation != null;
}
public boolean hasTicket() {
return ticket != null;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
public void minusAmount(long amount) {
this.amount -= amount;
}
public void plusAmount(long amount) {
this.amount += amount;
}
}
public class Invitation {
private LocalDateTime when;
}
@Component
@RequiredArgsConstructor
public class Theater {
public void enter(Audience audience, TicketSeller ticketSeller){
if(audience.getBag().hasInvitation()){
Ticket ticket = ticketSeller.getTicketOffice().getTicket();
audience.getBag().setTicket(ticket);
}else {
Ticket ticket = ticketSeller.getTicketOffice().getTicket();
audience.getBag().minusAmount(ticket.getFee());
ticketSeller.getTicketOffice().plusAmount(ticket.getFee());
audience.getBag().setTicket(ticket);
}
}
}
public class Ticket {
private Long fee;
public Ticket(Long fee) {
this.fee = fee;
}
public Long getFee() {
return fee;
}
}
public class TicketOffice {
private long amount;
private final List<Ticket> tickets;
public TicketOffice(Long amount, Ticket ... tickets) {
this.amount = amount;
this.tickets = Arrays.asList(tickets);
}
public Ticket getTicket(){
return tickets.get(0);
}
public void minusAmount(long amount) {
this.amount -= amount;
}
public void plusAmount(long amount) {
this.amount += amount;
}
}
public class TicketSeller {
private final TicketOffice ticketOffice;
public TicketSeller(TicketOffice ticketOffice){
this.ticketOffice = ticketOffice;
}
public TicketOffice getTicketOffice(){
return ticketOffice;
}
}