Niksi

НП Stadium

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