Advertisement
Martina312

[НП] - Stadium

Aug 21st, 2020
1,494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class SeatNotAllowedException extends Exception{
  4.     public SeatNotAllowedException(String message) {
  5.         super(message);
  6.     }
  7. }
  8.  
  9. class SeatTakenException extends Exception{
  10.     public SeatTakenException(String message) {
  11.         super(message);
  12.     }
  13. }
  14. class Sector{
  15.     private String code; //kod na sektorot
  16.     private int numSeats; //kolku mesta za sedenje ima
  17.     private boolean [] taken; //dali mestata se zafateni
  18.     private int type;
  19.     private int count;
  20.  
  21.     public Sector(String code, int numSeats) {
  22.         this.code = code;
  23.         this.numSeats = numSeats;
  24.         this.taken = new boolean[numSeats];
  25.         for (int i=0; i<numSeats; i++){
  26.             taken[i] = false;
  27.         }
  28.         this.type = 0;
  29.         this.count = 0;
  30.     }
  31.  
  32.     public String getCode() {
  33.         return code;
  34.     }
  35.  
  36.     public int getNumSeats() {
  37.         return numSeats;
  38.     }
  39.  
  40.     public int getType() {
  41.         return type;
  42.     }
  43.  
  44.     public void setType(int type) {
  45.         this.type = type;
  46.     }
  47.  
  48.     public boolean isTaken(int index){
  49.         return taken[index];
  50.     }
  51.  
  52.     public void takeSeat(int index){
  53.         taken[index] = true;
  54.         count++;
  55.     }
  56.  
  57.     public int freeSeats(){
  58.         return numSeats - count;
  59.     }
  60.    
  61.  
  62.     @Override
  63.     public String toString() {
  64.         return String.format("%s\t%d/%d\t%.1f%%", code, numSeats-count, numSeats,((double)count/numSeats)*100);
  65.     }
  66. }
  67.  
  68. class Stadium{
  69.     private String name; //ime na stadion
  70.     Map<String, Sector> map; //mapa na iminja na sektorite so samiot sektor
  71.  
  72.     public Stadium(String name) {
  73.         this.name = name;
  74.         this.map = new HashMap<>();
  75.     }
  76.  
  77.     public void createSectors(String [] sectorNames, int [] sizes){
  78.         for (int i=0; i<sizes.length; i++){
  79.             map.put(sectorNames[i], new Sector(sectorNames[i],sizes[i]));
  80.         }
  81.     }
  82.  
  83.     public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
  84.         Sector sector = map.get(sectorName);
  85.         if (sector.getType()==0){
  86.             sector.setType(type);
  87.         }
  88.  
  89.         if (sector.isTaken(seat-1))
  90.             throw new SeatTakenException("The seat "+seat+" is taken");
  91.         if ((sector.getType()==1 && type==2) || sector.getType()==2 && type==1)
  92.             throw new SeatNotAllowedException("");
  93.  
  94.         sector.takeSeat(seat-1);
  95.     }
  96.  
  97.     public void showSectors(){
  98.         map.values().stream()
  99.                 .sorted(Comparator.comparing(Sector::freeSeats).reversed().thenComparing(Sector::getCode))
  100.                 .forEach(System.out::println);
  101.     }
  102. }
  103. public class StaduimTest {
  104.     public static void main(String[] args) {
  105.         Scanner scanner = new Scanner(System.in);
  106.         int n = scanner.nextInt();
  107.         scanner.nextLine();
  108.         String[] sectorNames = new String[n];
  109.         int[] sectorSizes = new int[n];
  110.         String name = scanner.nextLine();
  111.         for (int i = 0; i < n; ++i) {
  112.             String line = scanner.nextLine();
  113.             String[] parts = line.split(";");
  114.             sectorNames[i] = parts[0];
  115.             sectorSizes[i] = Integer.parseInt(parts[1]);
  116.         }
  117.         Stadium stadium = new Stadium(name);
  118.         stadium.createSectors(sectorNames, sectorSizes);
  119.         n = scanner.nextInt();
  120.         scanner.nextLine();
  121.         for (int i = 0; i < n; ++i) {
  122.             String line = scanner.nextLine();
  123.             String[] parts = line.split(";");
  124.             try {
  125.                 stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
  126.                         Integer.parseInt(parts[2]));
  127.             } catch (SeatNotAllowedException e) {
  128.                 System.out.println("SeatNotAllowedException");
  129.             } catch (SeatTakenException e) {
  130.                 System.out.println("SeatTakenException");
  131.             }
  132.         }
  133.         stadium.showSectors();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement