Filip_Markoski

[NP] Stadium

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