Advertisement
NenadKocev

[НП] Stadium

Jan 22nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.IntStream;
  3.  
  4. class Mesto{
  5.     private int type;
  6.     private boolean isFree;
  7.  
  8.     public Mesto() {
  9.         this.isFree = true;
  10.     }
  11.  
  12.     public int getType() {
  13.         return type;
  14.     }
  15.  
  16.     public void setType(int type) {
  17.         this.type = type;
  18.     }
  19.  
  20.     public boolean isFree() {
  21.         return isFree;
  22.     }
  23.  
  24.     public void setZafatenost(boolean free) {
  25.         this.isFree = free;
  26.     }
  27.  
  28.     public String toString(){
  29.         return String.valueOf(type) + String.valueOf(isFree);
  30.     }
  31. }
  32.  
  33. class SeatTakenException extends Exception{
  34.     SeatTakenException(){
  35.         super("SeatTakenException");
  36.     }
  37.  
  38.     SeatTakenException(String s){
  39.         super(s);
  40.     }
  41. }
  42.  
  43. class SeatNotAllowedException extends Exception{
  44.     SeatNotAllowedException(){
  45.         super("SeatTakenException");
  46.     }
  47.  
  48.     SeatNotAllowedException(String s){
  49.         super(s);
  50.     }
  51. }
  52.  
  53. class Sector{
  54.     private String code;
  55.     private int brojMesta;
  56.     private List<Mesto> mesta;
  57.  
  58.     public Sector(String code, int brojMesta){
  59.         this.code = code;
  60.         this.brojMesta = brojMesta;
  61.         mesta = new ArrayList<>(brojMesta);
  62.         IntStream.range(0, brojMesta).forEach(i -> mesta.add(new Mesto()));
  63.     }
  64.  
  65.     public int freePlaces(){
  66.         return (int)mesta.stream()
  67.                 .filter(Mesto::isFree)
  68.                 .count();
  69.     }
  70.  
  71.     public String getCode() {
  72.         return code;
  73.     }
  74.  
  75.     public List<Mesto> getMesta() {
  76.         return mesta;
  77.     }
  78.  
  79.     public String toString(){
  80.         float zafatenost = (float)(mesta.size() - freePlaces()) / mesta.size() * 100;
  81.         String s = freePlaces() + "/" + mesta.size();
  82.         return String.format("%s\t%s\t%.1f%%", code, s, zafatenost);
  83.     }
  84. }
  85.  
  86. class Stadium{
  87.     private String name;
  88.     private List<Sector> sectors;
  89.  
  90.     public Stadium(String name) {
  91.         this.name = name;
  92.         sectors = new ArrayList<>();
  93.     }
  94.  
  95.     public void createSectors(String [] sectorNames, int[] sizes){
  96.         IntStream.range(0, sectorNames.length)
  97.                 .forEach(i -> sectors.add(new Sector(sectorNames[i], sizes[i])));
  98.     }
  99.  
  100.     public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException{
  101.         Sector sector = sectors.stream()
  102.                 .filter(s -> s.getCode().equals(sectorName))
  103.                 .findFirst()
  104.                 .get();
  105.  
  106.         Mesto m = sector.getMesta().get(seat-1);
  107.         if(!m.isFree())
  108.             throw new SeatTakenException();
  109.  
  110.         boolean wrongType = sector.getMesta().stream()
  111.                 .filter(mesto -> !mesto.isFree())
  112.                 .filter(mesto -> mesto.getType() != 0)
  113.                 .filter(mesto ->mesto.getType() != type)
  114.                 .anyMatch(mesto -> type != 0);
  115.        
  116.         if(wrongType)
  117.             throw new SeatNotAllowedException();
  118.  
  119.         m.setType(type);
  120.         m.setZafatenost(false);
  121.     }
  122.  
  123.     public void showSectors(){
  124.         sectors.stream()
  125.                 .sorted(Comparator.comparing(Sector::freePlaces).reversed().thenComparing(Sector::getCode))
  126.                 .forEach(System.out::println);
  127.     }
  128. }
  129.  
  130. class StaduimTest {
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement