글과 관련해
선요약 및 정리
객체 지향에 있어 실무와 이론
기본 원리
초대장 구현
public class Invitation { // 초대 일자를 인스턴수 변수로 포함
private LocalDateTime when;
}
티켓 구현
public class Ticket {
private Long fee; // 클래스 내부에서만 접근
public Long getFee() { // 클래스 외부에서 값 이용 가능
return fee;
}
}
가방 구현
public class Bag {
private Long amount;
private Invitation invitation;
private Ticket ticket;
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;
}
}
관람객이 소지품을 넣어두는 Bag 클래스는 현금(amount), 초대장(invitation), 티켓(ticket)을 인스턴스 변수로 포함합니다.
이벤트 당첨자는 티켓으로 교환할 초대장을 가지고 있고, 미당첨자는 현금을 보유합니다. 티켓을 구매하거나 초대장과 교환하기 전, 초기 상태에는 두 가지 경우가 있습니다.
Bag 클래스에 생성자 추가
public class Bag {
public Bag(long amount) { // 초대장 없음
this(null, amount);
}
public Bag(Invitation invitation, long amount) { // 초대장 있음
this.invitation = invitation;
this.amount = amount;
}
}
관람객 구현
public class Audience { // 관람객은 가방을 소지
private Bag bag;
public Audience(Bag bag) {
this.bag = bag;
}
public Bag getBag() { // 외부에서 bag에 접근
return bag;
}
}
매표소 구현
public class TicketOffice {
private Long amount;
private List<Ticket> tickets = new ArrayList<>();
public TicketOffice(Long amount, Ticket ... tickets) {
this.amount = amount;
this.tickets.addAll(Arrays.asList(tickets));
}
public Ticket getTicket() { // 티켓을 주면서, 하나 제거
return tickets.remove(0);
}
public void minusAmount(Long amount) { // 현금 감소
this.amount -= amount;
}
public void plusAmount(Long amount) { // 현금 증가
this.amount += amount;
}
}
매표원 구현
public class TicketSeller {
private TicketOffice ticketOffice;
public TicketSeller(TicketOffice ticketOffice) {
this.ticketOffice = ticketOffice;
}
public TicketOffice getTicketOffice() {
return ticektOffice;
}
}
소극장 구현
public class Theater {
private TicketSeller ticketSeller;
public Theater(TicketSeller ticketSeller) {
this.ticketSeller = ticketSeller;
}
public void enter(Audience audience) {
if (audience.getBag().hasInvitation()) { // 초대장 O, 교환
Ticket ticket = ticketSeller.getTicketOffice().getTIcket();
audience.getBag().setTicket(ticket);
} else { // 초대장 X, 현금 받고 티켓 주기
Ticket ticket = ticketSeller.getTicektOffice().getTicket();
audience.getBag().minusAmount(ticekt.getFee());
ticketSeller.getTicketOffice.plusAmount(ticket.getFee());
audience.getBag().setTicket(ticket);
}
}
}
그러나
질문들