Advertisement
lameski

Untitled

Jun 11th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. Stadium
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.Scanner;
  6.  
  7. class SeatTakenException extends RuntimeException{}
  8. class SeatNotAllowedException extends RuntimeException{}
  9.  
  10.  
  11. class Sector
  12. {
  13. String code;
  14. int seats;
  15. ArrayList<Integer> seatsTaken;
  16. boolean type[];
  17. Sector(String code, int seats)
  18. {
  19. this.code = code;
  20. this.seats = seats;
  21. this.seatsTaken = new ArrayList<>();
  22. this.type = new boolean[3];
  23. }
  24.  
  25. public String getCode() {
  26. return code;
  27. }
  28.  
  29. public int getSeats() {
  30. return seats;
  31. }
  32.  
  33.  
  34. void buyTicket(int seat, int seatType)
  35. {
  36. if(seatsTaken.contains(seat-1))
  37. throw new SeatTakenException();
  38.  
  39. if(seatType == 1)
  40. if(type[2])
  41. throw new SeatNotAllowedException();
  42. if(seatType == 2)
  43. if(type[1])
  44. throw new SeatNotAllowedException()
  45. ;
  46.  
  47. seatsTaken.add(seat-1);
  48. type[seatType] = true;
  49. }
  50. int getFreeSeats() {
  51. return seats - seatsTaken.size();
  52. }
  53.  
  54. double getPercent() {
  55. return (1 - (1.0 * getFreeSeats() / seats)) * 100;
  56. }
  57.  
  58. @Override
  59. public String toString() {
  60. return String.format("%s\t%d/%d\t%.1f%%", code, getFreeSeats(), seats, getPercent());
  61. }
  62.  
  63. }
  64.  
  65. class Stadium
  66. {
  67. String name;
  68. ArrayList<Sector> sectors;
  69.  
  70. Stadium(String name)
  71. {
  72. this.name = name;
  73. this.sectors = new ArrayList<>();
  74. }
  75.  
  76. void createSectors(String[] sectorNames, int[] sizes)
  77. {
  78. for(int i=0; i<sizes.length; i++)
  79. {
  80. sectors.add(new Sector(sectorNames[i], sizes[i]));
  81. }
  82. }
  83.  
  84. void buyTicket(String sectorName, int seat, int type)
  85. {
  86. this.sectors.stream()
  87. .filter(t -> t.code.equals(sectorName))
  88. .findFirst()
  89. .get()
  90. .buyTicket(seat, type);
  91. }
  92.  
  93. void showSectors()
  94. {
  95. this.sectors.stream()
  96. .sorted(Comparator.comparing(Sector::getFreeSeats).reversed())
  97. .forEach(System.out :: println);
  98. }
  99. }
  100. public class StaduimTest {
  101. public static void main(String[] args) {
  102. Scanner scanner = new Scanner(System.in);
  103. int n = scanner.nextInt();
  104. scanner.nextLine();
  105. String[] sectorNames = new String[n];
  106. int[] sectorSizes = new int[n];
  107. String name = scanner.nextLine();
  108. for (int i = 0; i < n; ++i) {
  109. String line = scanner.nextLine();
  110. String[] parts = line.split(";");
  111. sectorNames[i] = parts[0];
  112. sectorSizes[i] = Integer.parseInt(parts[1]);
  113. }
  114. Stadium stadium = new Stadium(name);
  115. stadium.createSectors(sectorNames, sectorSizes);
  116. n = scanner.nextInt();
  117. scanner.nextLine();
  118. for (int i = 0; i < n; ++i) {
  119. String line = scanner.nextLine();
  120. String[] parts = line.split(";");
  121. try {
  122. stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
  123. Integer.parseInt(parts[2]));
  124. } catch (SeatNotAllowedException e) {
  125. System.out.println("SeatNotAllowedException");
  126. } catch (SeatTakenException e) {
  127. System.out.println("SeatTakenException");
  128. }
  129. }
  130. stadium.showSectors();
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement