Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. class Theater {
  2. constructor() {
  3. this._movies = new Set();
  4. this._movieFees = new Map();
  5. this._ticketOffices = new Set();
  6. }
  7.  
  8. getFee(movie) {
  9. if (!this.isScreening(movie)) throw new Error('movie is not screening');
  10.  
  11. return this._movieFees.get(movie);
  12. }
  13.  
  14. setFee(movie, fee) {
  15. if (!this._movies.has(movie)) return;
  16.  
  17. this._movieFees.set(movie, fee);
  18. }
  19.  
  20. setMovies(movies) {
  21. for (const movie of movies) {
  22. this._movies.add(movie);
  23. }
  24. }
  25.  
  26. isScreening(movie) {
  27. return this._movies.has(movie);
  28. }
  29.  
  30. setTicketOffices(ticketOffices) {
  31. for (const ticketOffice of ticketOffices) {
  32. if (!ticketOffice.isContractWith(this)) throw new Error('notContractWithThisTheater');
  33. this._ticketOffices.add(ticketOffice);
  34. }
  35. }
  36.  
  37. setTicket(ticketOffice, movie, num) {
  38. if (!this._ticketOffices.has(ticketOffice)) return;
  39.  
  40. while (num-- > 0) {
  41. ticketOffice.addTicket(movie, new Ticket(this, movie));
  42. }
  43. }
  44.  
  45. setInvitation(audience) {
  46. audience.setInvitation(new Invitation(this, 7000));
  47. }
  48.  
  49. enter(audience) {
  50. const ticket = audience.getTicket();
  51. if (!ticket) return false;
  52.  
  53. return ticket.isValid(this);
  54. }
  55. }
  56.  
  57. class Movie {
  58. constructor(name) {
  59. this._name = name;
  60. }
  61. }
  62.  
  63. class Ticket {
  64. constructor(theater, movie) {
  65. this._theater = theater;
  66. this._movie = movie;
  67. this._isEntered = false;
  68. }
  69.  
  70. getFee() {
  71. return this._theater.getFee(this._movie);
  72. }
  73.  
  74. isValid(theater) {
  75. if (this._isEntered || theater !== this._theater || !theater.isScreening(this._movie)) return false;
  76.  
  77. this._isEntered = true;
  78. return true;
  79. }
  80. }
  81.  
  82. class Invitation {
  83. constructor(theater, amount) {
  84. this._theater = theater;
  85. this._amount = amount;
  86. }
  87.  
  88. getTheater() {
  89. return this._theater;
  90. }
  91.  
  92. getAmount() {
  93. return this._amount;
  94. }
  95. }
  96.  
  97. class TicketOffice {
  98. constructor(theater, amount) {
  99. this._theater = theater;
  100. this._amount = amount;
  101. this._tickets = new Map();
  102. }
  103.  
  104. isValidInvitation(invitation) {
  105. return this._theater === invitation.getTheater();
  106. }
  107.  
  108. isContractWith(theater) {
  109. return this._theater === theater;
  110. }
  111.  
  112. addTicket(movie, ticket) {
  113. if (this._tickets.has(movie)) {
  114. const tickets = this._tickets.get(movie);
  115. this._tickets.set(movie, [...tickets, ticket]);
  116. } else {
  117. this._tickets.set(movie, [ticket]);
  118. }
  119. }
  120.  
  121. getTicketWithFee(movie) {
  122. if (!this._tickets.has(movie)) return null;
  123.  
  124. const ticket = this._getTicket(movie);
  125. if (!ticket) return null;
  126.  
  127. this._amount += ticket.getFee();
  128. return ticket;
  129. }
  130.  
  131. getTicketWithNoFee(movie) {
  132. if (!this._tickets.has(movie)) return null;
  133.  
  134. return this._getTicket(movie);
  135. }
  136.  
  137. _getTicket(movie) {
  138. const [ticket] = this._tickets.get(movie).splice(0, 1);
  139. return ticket ? ticket : null;
  140. }
  141.  
  142. getTicketPrice(movie) {
  143. if (!this._tickets.has(movie)) return null;
  144.  
  145. const tickets = this._tickets.get(movie);
  146. if (!tickets.length) return null;
  147.  
  148. const [ticket] = tickets;
  149. return ticket.getFee();
  150. }
  151. }
  152.  
  153. class TicketSeller {
  154. constructor() {
  155. this._ticketOffice = null;
  156. }
  157.  
  158. setTicketOffice(ticketOffice) {
  159. this._ticketOffice = ticketOffice;
  160. }
  161.  
  162. getTicket(audience, movie) {
  163. let ticket = null;
  164.  
  165. const price = this._ticketOffice.getTicketPrice(movie);
  166. const invitation = audience.getInvitation();
  167. if (invitation !== null && this._ticketOffice.isValidInvitation(invitation)) {
  168. const invitationAmount = invitation.getAmount();
  169. const canGetTicketByInvitation = price > 0 && invitationAmount >= price;
  170. const canGetTicketByInvitationWithAudienceMoney =
  171. (price > 0 && invitationAmount < price) && (audience.hasAmount(price - invitationAmount));
  172.  
  173. if (canGetTicketByInvitation) {
  174. ticket = this._ticketOffice.getTicketWithNoFee(movie);
  175. if (ticket !== null) audience.removeInvitation();
  176. } else if (canGetTicketByInvitationWithAudienceMoney) {
  177. ticket = this._ticketOffice.getTicketWithFee(movie);
  178. if (ticket !== null) audience.minusAmount(price - invitationAmount);
  179. }
  180. } else {
  181. if (price > 0 && audience.hasAmount(price)) {
  182. ticket = this._ticketOffice.getTicketWithFee(movie);
  183. if (ticket !== null) audience.minusAmount(price);
  184. }
  185. }
  186.  
  187. return ticket;
  188. }
  189. }
  190.  
  191. class Audience {
  192. constructor(amount) {
  193. this._amount = amount;
  194. this._ticket = null;
  195. this._invitation = null;
  196. }
  197.  
  198. buyTicket(seller, movie) {
  199. this._ticket = seller.getTicket(this, movie);
  200. }
  201.  
  202. hasAmount(amount) {
  203. return this._amount >= amount;
  204. }
  205.  
  206. minusAmount(amount) {
  207. if (amount > this._amount) return false;
  208.  
  209. this._amount -= amount;
  210. return true;
  211. }
  212.  
  213. getInvitation() {
  214. return this._invitation;
  215. }
  216.  
  217. removeInvitation() {
  218. this._invitation = null;
  219. }
  220.  
  221. getTicket() {
  222. return this._ticket;
  223. }
  224.  
  225. setInvitation(invitation) {
  226. this._invitation = invitation;
  227. }
  228. }
  229.  
  230.  
  231. function main() {
  232. const hongDaeCgv = new Theater();
  233. const endGame = new Movie('EndGame');
  234. const toyStory = new Movie('ToyStory');
  235.  
  236. hongDaeCgv.setMovies([endGame, toyStory]);
  237. hongDaeCgv.setFee(endGame, 10000);
  238. hongDaeCgv.setFee(toyStory, 12000);
  239.  
  240. const audience1 = new Audience(10000);
  241. const audience2 = new Audience(20000);
  242. const audience3 = new Audience(5000);
  243. const audience4 = new Audience(6000);
  244.  
  245. const hongDaeCgvTicketOffice = new TicketOffice(hongDaeCgv, 0);
  246. const seller = new TicketSeller();
  247.  
  248. hongDaeCgv.setTicketOffices([hongDaeCgvTicketOffice]);
  249. hongDaeCgv.setTicket(hongDaeCgvTicketOffice, endGame, 10);
  250. hongDaeCgv.setTicket(hongDaeCgvTicketOffice, toyStory, 3);
  251. hongDaeCgv.setInvitation(audience3);
  252. hongDaeCgv.setInvitation(audience4);
  253.  
  254. seller.setTicketOffice(hongDaeCgvTicketOffice);
  255.  
  256. audience1.buyTicket(seller, toyStory);
  257. audience2.buyTicket(seller, toyStory);
  258. audience3.buyTicket(seller, toyStory);
  259. audience4.buyTicket(seller, toyStory);
  260.  
  261. const isOk1 = hongDaeCgv.enter(audience1);
  262. const isOk2 = hongDaeCgv.enter(audience2);
  263. const isOk3 = hongDaeCgv.enter(audience3);
  264. const isOk4 = hongDaeCgv.enter(audience4);
  265.  
  266. console.log(isOk1); // false
  267. console.log(isOk2); // true
  268. console.log(isOk3); // true
  269. console.log(isOk4); // true
  270. }
  271.  
  272. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement