Advertisement
dzocesrce

[NP] Stadiums

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