Skip to content

Instantly share code, notes, and snippets.

@whosoonhwang
Created July 4, 2019 07:06
Show Gist options
  • Save whosoonhwang/c9f5e82b032528ef144b521b7bcda432 to your computer and use it in GitHub Desktop.
Save whosoonhwang/c9f5e82b032528ef144b521b7bcda432 to your computer and use it in GitHub Desktop.
코드스피치 s83 1차 과제
package codespitz;
public class Audience {
private Ticket ticket = Ticket.EMPTY;
private Invitation invitation = Invitation.EMPTY;
private Long amount;
public Audience(Long amount) {
this.amount = amount;
}
public Ticket getTicket() {
return ticket;
}
public void buyTicket(final TicketSeller seller, final Theater theater, final Movie movie) {
ticket = seller.getTicket(this, theater, movie);
}
public boolean hasAmount(Long amount) {
return this.amount >= amount;
}
public boolean minusAmount(Long amount) {
if (amount > this.amount) return false;
this.amount -= amount;
return true;
}
public Invitation getInvitation() {
return invitation;
}
public void setInvitation(final Invitation invitation) {
this.invitation = invitation;
}
public void removeInvitation() {
invitation = Invitation.EMPTY;
}
}
package codespitz;
public class Invitation {
final static public Invitation EMPTY = new Invitation(null, null);
final private Theater theater;
final private Movie movie;
public Invitation(final Theater theater, final Movie movie) {
this.theater = theater;
this.movie = movie;
}
public boolean isValid(final Theater theater, final Movie movie) {
return this.theater == theater && this.movie == movie;
}
}
package codespitz;
public class Movie {
final private String title;
final private Long fee;
public Movie(String title, Long fee) {
this.title = title;
this.fee = fee;
}
public Long getFee() {
return fee;
}
}
package codespitz;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Theater {
final private Set<Movie> movieSet = new HashSet<>();
final private Set<Ticket> ticketSet = new HashSet<>();
final private Set<Invitation> invitationSet = new HashSet<>();
public Theater() {}
public void setMovies(final Movie... movies) {
this.movieSet.addAll(Arrays.asList(movies));
}
private boolean hasMovie(final Movie movie) {
return movieSet.contains(movie);
}
private boolean isValidTicket(final Ticket ticket) {
return ticketSet.contains(ticket);
}
public boolean isValidInvitation(final Invitation invitation) {
return invitationSet.contains(invitation);
}
public void setTicket(final TicketOffice ticketOffice, final Movie movie, Long num) {
if (ticketOffice.getTheater() != this) return;
if (!hasMovie(movie)) return;
while (num-- > 0) {
final Ticket ticket = new Ticket(this, movie);
ticketSet.add(ticket);
ticketOffice.addTicket(ticket);
}
}
public void setInvitation(final Audience audience, final Movie movie) {
if (!hasMovie(movie)) return;
final Invitation invitation = new Invitation(this, movie);
invitationSet.add(invitation);
audience.setInvitation(invitation);
}
public boolean enter(final Audience audience) {
final Ticket ticket = audience.getTicket();
if (!isValidTicket(ticket)) return false;
return ticket.isValid(this);
}
}
package codespitz;
public class Ticket {
final static public Ticket EMPTY = new Ticket(null, null);
final private Theater theater;
final private Movie movie;
private boolean isEntered = false;
public Ticket(final Theater theater, final Movie movie) {
this.theater = theater;
this.movie = movie;
}
public Movie getMovie() {
return movie;
}
public boolean isValid(final Theater theater) {
if (isEntered || theater != this.theater || this == EMPTY) {
return false;
}
isEntered = true;
return true;
}
}
package codespitz;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class TicketOffice {
final private Theater theater;
private Long amount;
private Map<Movie, List<Ticket>> movieToTicketMap = new HashMap<>();
public TicketOffice(final Theater theater, Long amount) {
this.theater = theater;
this.amount = amount;
}
public Theater getTheater() {
return theater;
}
public void addTicket(final Ticket ticket) {
final Movie movie = ticket.getMovie();
if (!movieToTicketMap.containsKey(movie)) {
movieToTicketMap.put(movie, new LinkedList<>());
}
movieToTicketMap.get(movie).add(ticket);
}
public Ticket getTicketWIthFee(final Movie movie) {
final Ticket ticket = getTicketWithNoFee(movie);
if (ticket != Ticket.EMPTY) {
amount += movie.getFee();
}
return ticket;
}
public Ticket getTicketWithNoFee(final Movie movie) {
if (!movieToTicketMap.containsKey(movie)) return Ticket.EMPTY;
final List<Ticket> ticketList = movieToTicketMap.get(movie);
int size = ticketList.size();
if (size == 0) return Ticket.EMPTY;
else {
if (size == 1) movieToTicketMap.remove(movie);
return ticketList.remove(0);
}
}
}
package codespitz;
public class TicketSeller {
private TicketOffice ticketOffice;
public void setTicketOffice(final TicketOffice ticketOffice) {
this.ticketOffice = ticketOffice;
}
public Ticket getTicket(final Audience audience, final Theater theater, final Movie movie) {
Ticket ticket = Ticket.EMPTY;
final Invitation invitation = audience.getInvitation();
if (theater.isValidInvitation(invitation) && invitation.isValid(theater, movie)) {
ticket = ticketOffice.getTicketWithNoFee(movie);
if (ticket != Ticket.EMPTY) audience.removeInvitation();
} else {
Long price = movie.getFee();
if (price > 0 && audience.hasAmount(price)) {
ticket = ticketOffice.getTicketWIthFee(movie);
if (ticket != Ticket.EMPTY) audience.minusAmount(price);
}
}
return ticket;
}
}
import codespitz.*;
public class Main {
public static void main(String[] args) {
final Theater theater = new Theater();
final TicketOffice ticketOffice = new TicketOffice(theater, 0L);
final Movie codespitzMovie = new Movie("codespitz", 50000L);
final Movie aiMovie = new Movie("ai", 10000L);
final Audience audience0 = new Audience(0L);
final Audience audience1 = new Audience(0L);
final Audience audience2 = new Audience(100000L);
final Audience audience3 = new Audience(100L);
final Audience audience4 = new Audience(100000L);
final Audience audience5 = new Audience(0L);
final TicketSeller seller = new TicketSeller();
theater.setMovies(codespitzMovie, aiMovie);
theater.setTicket(ticketOffice, codespitzMovie, 1L);
theater.setTicket(ticketOffice, aiMovie, 1L);
theater.setInvitation(audience1, codespitzMovie);
audience0.setInvitation(new Invitation(theater, codespitzMovie));
seller.setTicketOffice(ticketOffice);
audience0.buyTicket(seller, theater, codespitzMovie);
audience1.buyTicket(seller, theater, codespitzMovie);
audience2.buyTicket(seller, theater, codespitzMovie);
audience3.buyTicket(seller, theater, aiMovie);
audience4.buyTicket(seller, theater, aiMovie);
audience5.buyTicket(seller, theater, aiMovie);
boolean isOk0 = theater.enter(audience0);
boolean isOk1 = theater.enter(audience1);
boolean isOk2 = theater.enter(audience2);
boolean isOk3 = theater.enter(audience3);
boolean isOk4 = theater.enter(audience4);
boolean isOk5 = theater.enter(audience5);
boolean isOk1Retry = theater.enter(audience1);
System.out.println(isOk0); // false - 돈 없는게 초대권 사기까지
System.out.println(isOk1); // true - 초대권을 티켓 교환
System.out.println(isOk2); // false - 부자인데 티켓이 다 팔림
System.out.println(isOk3); // false - 돈 없음
System.out.println(isOk4); // true - 부자라 티겟 교환
System.out.println(isOk5); // false - 아무것도 없음
System.out.println(isOk1Retry); // false - 재입장 불가
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment