Advertisement
Dimitrija

Stadium - NP

Jan 22nd, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class SeatTakenException extends Exception {
  5.     public SeatTakenException(String message) {
  6.         super(message);
  7.     }
  8. }
  9.  
  10. class SeatNotAllowedException extends Exception {
  11.     public SeatNotAllowedException(String message) {
  12.         super(message);
  13.     }
  14. }
  15.  
  16. class Seat {
  17.     boolean isTaken;
  18.     int type;
  19.  
  20.     public Seat(boolean isTaken) {
  21.         this.isTaken = isTaken;
  22.         this.type = 0;
  23.     }
  24.  
  25.     public int getType() {
  26.         return type;
  27.     }
  28.  
  29.     public boolean isTaken() {
  30.         return isTaken;
  31.     }
  32.  
  33.     public void buy(int type) {
  34.         this.isTaken = true;
  35.         this.type = type;
  36.     }
  37. }
  38.  
  39. class Sector {
  40.     String code;
  41.     int seats;
  42.     Map<Integer, Seat> seatsMap;
  43.     int typeSector;
  44.  
  45.     public Sector(String code, int seats) {
  46.         this.code = code;
  47.         this.seats = seats;
  48.         this.typeSector = 0;
  49.         seatsMap = new HashMap<>();
  50.         for (int i = 1; i <= seats; i++) {
  51.             Seat seat = new Seat(false);
  52.             seatsMap.put(i, seat);
  53.         }
  54.     }
  55.  
  56.     public void buySeat(int seat, int type) throws SeatTakenException, SeatNotAllowedException {
  57.  
  58.         if (seatsMap.containsKey(seat)) {
  59.             if (typeSector == 0&&type != 0)
  60.                 typeSector = type;
  61.             if (seatsMap.get(seat).isTaken()) {
  62.                 throw new SeatTakenException("Seat is Taken");
  63.             }
  64.             if ((type == 2 && typeSector == 1) ||
  65.                     (type == 1 && typeSector == 2)) {
  66.                 throw new SeatNotAllowedException("Seat is not allowed");
  67.             }
  68.             seatsMap.get(seat).buy(type);
  69.         } else {
  70.             throw new SeatNotAllowedException("Seat is not allowed");
  71.         }
  72.     }
  73.  
  74.     public String getCode() {
  75.         return code;
  76.     }
  77.  
  78.     public int takenSlotsCount() {
  79.         return (int) seatsMap.values().stream().filter(x -> x.isTaken()).count();
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.  
  85.         //E 1000/1000   0.0%
  86.         int freeSlots = seatsMap.size() - takenSlotsCount();
  87.         double percentTaken = takenSlotsCount() / (double) seatsMap.size() * 100;
  88.         return String.format("%s\t%d/%d\t%.1f%%", code, seatsMap.size() - takenSlotsCount(), seatsMap.size(), percentTaken);
  89.     }
  90. }
  91.  
  92. class Stadium {
  93.     String name;
  94.     Map<String, Sector> sectorMap;
  95.  
  96.     Stadium(String name) {
  97.         sectorMap = new TreeMap<>();
  98.         this.name = name;
  99.     }
  100.  
  101.     void createSectors(String[] sectorNames, int[] sizes) {
  102.         for (int i = 0; i < sizes.length; i++) {
  103.             Sector sector = new Sector(sectorNames[i], sizes[i]);
  104.             sectorMap.putIfAbsent(sectorNames[i], sector);
  105.         }
  106.     }
  107.  
  108.     void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
  109.         sectorMap.get(sectorName).buySeat(seat, type);
  110.     }
  111.  
  112.     void showSectors() {
  113.         List<Sector> secondList = sectorMap.values().stream().collect(Collectors.toList());
  114.         Comparator<Sector> comparator = Comparator.comparing(Sector::takenSlotsCount).thenComparing(Sector::getCode);
  115.  
  116.         secondList.stream().sorted(comparator).forEach(x -> System.out.println(x));
  117.  
  118.  
  119.     }
  120. }
  121.  
  122.  
  123. public class StaduimTest {
  124.     public static void main(String[] args) {
  125.         Scanner scanner = new Scanner(System.in);
  126.         int n = scanner.nextInt();
  127.         scanner.nextLine();
  128.         String[] sectorNames = new String[n];
  129.         int[] sectorSizes = new int[n];
  130.         String name = scanner.nextLine();
  131.         for (int i = 0; i < n; ++i) {
  132.             String line = scanner.nextLine();
  133.             String[] parts = line.split(";");
  134.             sectorNames[i] = parts[0];
  135.             sectorSizes[i] = Integer.parseInt(parts[1]);
  136.         }
  137.         Stadium stadium = new Stadium(name);
  138.         stadium.createSectors(sectorNames, sectorSizes);
  139.         n = scanner.nextInt();
  140.         scanner.nextLine();
  141.         for (int i = 0; i < n; ++i) {
  142.             String line = scanner.nextLine();
  143.             String[] parts = line.split(";");
  144.             try {
  145.                 stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
  146.                         Integer.parseInt(parts[2]));
  147.             } catch (SeatNotAllowedException e) {
  148.                 System.out.println("SeatNotAllowedException");
  149.             } catch (SeatTakenException e) {
  150.                 System.out.println("SeatTakenException");
  151.             }
  152.         }
  153.         stadium.showSectors();
  154.     }
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement