Advertisement
popovstefan

[NP 2 kol.] Stadium

Dec 2nd, 2018
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.43 KB | None | 0 0
  1. Stadium Problem 7 (2 / 15)
  2. Да се имплементира систем за билети за стадион. За таа цел треба да се имплементираат класите:
  3.  
  4. Sector во која се чуват информации за:
  5. кодот на секторот String
  6. бројот на места за седење int
  7. информации за зафатеност на местата за седење ?
  8. Stadium во која се чуваат информации за:
  9. името на стадионот String
  10. и сите сектори во стадионот ?
  11. Во класата Stadium треба да се имплементираат следните методи:
  12.  
  13. Stadium(String name) конструктор со аргумент име на стадионот
  14. void createSectors(String[] sectorNames, int[] sizes) креирање на сектори со имиња String[] sectorNames и број на места int[] sizes (двете низи се со иста големина)
  15. void buyTicket(String sectorName, int seat, int type) за купување билет од проследениот тип (type, 0 - неутрален, 1 - домашен, 2 - гостински), во секторот sectorName со број на место seat (местото секогаш е со вредност во опсег 1 - size). Ако местото е зафатено (претходно е купен билет на ова место) се фрла исклучок од вид SeatTakenException. Исто така ако се обидеме да купиме билет од тип 1, во сектор во кој веќе има купено билет од тип 2 (и обратно) се фрла исклучок од вид SeatNotAllowedException.
  16. void showSectors() ги печати сите сектори сортирани според бројот на слободни места во опаѓачки редослед (ако повеќе сектори имаат ист број на слободни места, се подредуваат според името).
  17.  
  18. ===================================================================================================================
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Comparator;
  22. import java.util.Hashtable;
  23. import java.util.Scanner;
  24.  
  25. @SuppressWarnings("serial")
  26. class SeatNotAllowedException extends RuntimeException {}
  27. @SuppressWarnings("serial")
  28. class SeatTakenException extends RuntimeException {}
  29.  
  30. class Sector {
  31.     final String code;
  32.     int seats;
  33.     ArrayList<Integer> seatsTaken;
  34.     boolean type [];
  35.     Sector(String code, int seats) {
  36.         this.code = code;
  37.         this.seats = seats;
  38.         type = new boolean[3];
  39.         this.seatsTaken = new ArrayList<>();
  40.     }
  41.  
  42.     int getFreeSeats() {
  43.         return seats - seatsTaken.size();
  44.     }
  45.  
  46.     double getPercent() {
  47.        return (1 - (1.0 * getFreeSeats() / seats)) * 100;
  48.     }
  49.    
  50.     void buyTicket(int seat, int seatType) {
  51.         if (seatsTaken.contains(seat - 1))
  52.             throw new SeatTakenException();
  53.         if (seatType == 1) {
  54.             if (type[2])
  55.                 throw new SeatNotAllowedException();
  56.         }
  57.         if (seatType == 2) {
  58.             if (type[1])
  59.                 throw new SeatNotAllowedException();
  60.         }
  61.         seatsTaken.add(seat - 1);
  62.         type[seatType] = true;
  63.     }
  64.    
  65.     @Override
  66.     public String toString() {
  67.         return String.format("%s\t%d/%d\t%.1f%%", code, getFreeSeats(), seats, getPercent());
  68.     }
  69. }
  70.  
  71. class Stadium {
  72.     final String name;
  73.     Hashtable<String, Sector> sectors;
  74.     final Comparator<Sector> compareByFreeSeats = (s1, s2) -> {
  75.         if (Integer.compare(s1.getFreeSeats(), s2.getFreeSeats()) != 0)
  76.             return Integer.compare(s2.getFreeSeats(), s1.getFreeSeats());
  77.         else
  78.             return s1.code.compareTo(s2.code);
  79.     };
  80.  
  81.     Stadium(String name) {
  82.         this.name = name;
  83.         sectors = new Hashtable<>();
  84.     }
  85.  
  86.     void createSectors(String [] sectorNames, int [] sizes) {
  87.         for (int i = 0; i < sectorNames.length; i++)
  88.             sectors.put(sectorNames[i], new Sector(sectorNames[i] , sizes[i]));
  89.     }
  90.  
  91.     void buyTicket(String sectorName, int seat, int type) {
  92.         this.sectors
  93.         .values()
  94.         .stream()
  95.         .filter(t -> t.code.equals(sectorName))
  96.         .findFirst()
  97.         .get()
  98.         .buyTicket(seat, type);
  99.     }
  100.  
  101.     void showSectors() {
  102.         this.sectors
  103.         .values()
  104.         .stream()
  105.         .sorted(compareByFreeSeats)
  106.         .forEach(System.out :: println);
  107.     }
  108. }
  109. public class StaduimTest {
  110.     public static void main(String[] args) {
  111.         Scanner scanner = new Scanner(System.in);
  112.         int n = scanner.nextInt();
  113.         scanner.nextLine();
  114.         String[] sectorNames = new String[n];
  115.         int[] sectorSizes = new int[n];
  116.         String name = scanner.nextLine();
  117.         for (int i = 0; i < n; ++i) {
  118.             String line = scanner.nextLine();
  119.             String[] parts = line.split(";");
  120.             sectorNames[i] = parts[0];
  121.             sectorSizes[i] = Integer.parseInt(parts[1]);
  122.         }
  123.         Stadium stadium = new Stadium(name);
  124.         stadium.createSectors(sectorNames, sectorSizes);
  125.         n = scanner.nextInt();
  126.         scanner.nextLine();
  127.         for (int i = 0; i < n; ++i) {
  128.             String line = scanner.nextLine();
  129.             String[] parts = line.split(";");
  130.             try {
  131.                 stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
  132.                                   Integer.parseInt(parts[2]));
  133.             } catch (SeatNotAllowedException e) {
  134.                 System.out.println("SeatNotAllowedException");
  135.             } catch (SeatTakenException e) {
  136.                 System.out.println("SeatTakenException");
  137.             }
  138.         }
  139.         stadium.showSectors();
  140.     }
  141. }
  142.  
  143.  
  144. ===============================================================================================================
  145. ample input
  146. 5
  147. Camp Nou
  148. A;1000
  149. B;1000
  150. C;1000
  151. D;1000
  152. E;1000
  153. 10
  154. A;1;1
  155. A;2;1
  156. A;1;1
  157. A;3;0
  158. A;4;2
  159. B;1;1
  160. C;2;2
  161. D;1;1
  162. D;2;0
  163. D;3;0
  164.  
  165. ===================================================================================================================
  166. Sample output
  167. SeatTakenException
  168. SeatNotAllowedException
  169. E   1000/1000   0.0%
  170. B   999/1000    0.1%
  171. C   999/1000    0.1%
  172. A   997/1000    0.3%
  173. D   997/1000    0.3%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement