Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. public class Audience {
  2. private Ticket ticket = Ticket.EMPTY;
  3. private Invitation invitation = Invitation.EMPTY;
  4. private Long amount;
  5. public Audience(Long amount){this.amount = amount;}
  6. public void buyTicket(TicketSeller seller){
  7. ticket = seller.getTicket(this);
  8. }
  9. public boolean hasAmount(Long amount){
  10. return this.amount >= amount;
  11. }
  12. public boolean minusAmount(Long amount){
  13. if(amount > this.amount) return false;
  14. this.amount -= amount;
  15. return true;
  16. }
  17. public Invitation getInvitation(){return invitation;}
  18. public void removeInvitation(){
  19. invitation = Invitation.EMPTY;
  20. }
  21. public Ticket getTicket(){return ticket;}
  22. public void setInvitation(Invitation invitation){
  23. this.invitation = invitation;
  24. }
  25. }
  26.  
  27. public class Invitation {
  28. final static public Invitation EMPTY = new Invitation(null);
  29. final private Theater theater;
  30. public Invitation(Theater theater){
  31. this.theater = theater;
  32. }
  33. }
  34.  
  35. public class Main {
  36. public static void main(String[] args) {
  37. Theater theater = new Theater(100L);
  38. Audience audience1 = new Audience(0L);
  39. Audience audience2 = new Audience(50L);
  40. TicketOffice ticketOffice = new TicketOffice(0L);
  41. TicketSeller seller = new TicketSeller();
  42.  
  43. theater.setTicketOffices(ticketOffice);
  44. theater.setTicket(10L);
  45. theater.setInvitation(audience1);
  46.  
  47. seller.setTicketOffice(ticketOffice);
  48.  
  49. audience1.buyTicket(seller);
  50. audience2.buyTicket(seller);
  51.  
  52. boolean isOk1 = theater.enter(audience1);
  53. boolean isOk2 = theater.enter(audience2);
  54.  
  55. System.out.println(isOk1);//true
  56. System.out.println(isOk2);//false
  57.  
  58. }
  59. }
  60.  
  61. public class Theater{
  62. private TicketOffice ticketOffice = TicketOffice.EMPTY ;
  63. final private Long fee;
  64. public Theater( Long fee){
  65. this.fee = fee;
  66. }
  67. Long getFee(){
  68. return this.fee;
  69. }
  70.  
  71. public void setTicketOffices(TicketOffice ticketOffice ) {
  72. this.ticketOffice = ticketOffice;
  73. }
  74. public void setTicket(Long num){
  75. if(this.ticketOffice == TicketOffice.EMPTY) return;
  76. while(num-- > 0) {
  77. this.ticketOffice.addTicket(new Ticket(this));
  78. }
  79. }
  80. public void setInvitation(Audience audience){
  81. audience.setInvitation(new Invitation(this));
  82. }
  83. public boolean enter(Audience audience){
  84. Ticket ticket = audience.getTicket();
  85. return ticket.isValid(this);
  86. }
  87. }
  88.  
  89. public class Ticket {
  90. final static public Ticket EMPTY = new Ticket(null);
  91. final private Theater theater;
  92. private boolean isEntered = false;
  93. public Ticket(Theater theater){
  94. this.theater = theater;
  95. }
  96. public boolean isValid(Theater theater){
  97. if(isEntered || theater != this.theater || this == EMPTY){
  98. return false;
  99. }else{
  100. isEntered = true;
  101. return true;
  102. }
  103. }
  104. public Long getFee(){
  105. return theater.getFee();
  106. }
  107. }
  108.  
  109. import java.util.ArrayList;
  110. import java.util.List;
  111.  
  112. public class TicketOffice {
  113. final static public TicketOffice EMPTY = new TicketOffice(null);
  114. private Long amount;
  115. private List<Ticket> tickets = new ArrayList<>();
  116. public TicketOffice(Long amount){this.amount = amount;}
  117. public void addTicket(Ticket ticket){
  118. this.tickets.add(ticket);
  119. }
  120. public Ticket getTicketWithFee(){
  121. if(tickets.size() == 0) return Ticket.EMPTY;
  122. else{
  123. Ticket ticket = tickets.remove(0);
  124. amount += ticket.getFee();
  125. return ticket;
  126. }
  127. }
  128. public Ticket getTicketWithNoFee(){
  129. if(tickets.size() == 0) return Ticket.EMPTY;
  130. else return tickets.remove(0);
  131. }
  132. public Long getTicketPrice(){
  133. if(tickets.size() == 0) return 0L;
  134. else return tickets.get(0).getFee();
  135. }
  136. }
  137.  
  138.  
  139. public class TicketSeller {
  140. private TicketOffice ticketOffice;
  141. public void setTicketOffice(TicketOffice ticketOffice){
  142. this.ticketOffice = ticketOffice;
  143. }
  144. public Ticket getTicket(Audience audience){
  145. Ticket ticket = Ticket.EMPTY;
  146. if(audience.getInvitation() != Invitation.EMPTY){
  147. ticket = ticketOffice.getTicketWithNoFee();
  148. if(ticket != Ticket.EMPTY) audience.removeInvitation();
  149. }else{
  150. Long price = ticketOffice.getTicketPrice();
  151. if(price > 0 && audience.hasAmount(price)){
  152. ticket = ticketOffice.getTicketWithFee();
  153. if(ticket != Ticket.EMPTY) audience.minusAmount(price);
  154. }
  155. }
  156. return ticket;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement