Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- class SeatTakenException extends Exception {
- public SeatTakenException(String message) {
- super(message);
- }
- }
- class SeatNotAllowedException extends Exception {
- public SeatNotAllowedException(String message) {
- super(message);
- }
- }
- class Seat {
- boolean isTaken;
- int type;
- public Seat(boolean isTaken) {
- this.isTaken = isTaken;
- this.type = 0;
- }
- public int getType() {
- return type;
- }
- public boolean isTaken() {
- return isTaken;
- }
- public void buy(int type) {
- this.isTaken = true;
- this.type = type;
- }
- }
- class Sector {
- String code;
- int seats;
- Map<Integer, Seat> seatsMap;
- int typeSector;
- public Sector(String code, int seats) {
- this.code = code;
- this.seats = seats;
- this.typeSector = 0;
- seatsMap = new HashMap<>();
- for (int i = 1; i <= seats; i++) {
- Seat seat = new Seat(false);
- seatsMap.put(i, seat);
- }
- }
- public void buySeat(int seat, int type) throws SeatTakenException, SeatNotAllowedException {
- if (seatsMap.containsKey(seat)) {
- if (typeSector == 0&&type != 0)
- typeSector = type;
- if (seatsMap.get(seat).isTaken()) {
- throw new SeatTakenException("Seat is Taken");
- }
- if ((type == 2 && typeSector == 1) ||
- (type == 1 && typeSector == 2)) {
- throw new SeatNotAllowedException("Seat is not allowed");
- }
- seatsMap.get(seat).buy(type);
- } else {
- throw new SeatNotAllowedException("Seat is not allowed");
- }
- }
- public String getCode() {
- return code;
- }
- public int takenSlotsCount() {
- return (int) seatsMap.values().stream().filter(x -> x.isTaken()).count();
- }
- @Override
- public String toString() {
- //E 1000/1000 0.0%
- int freeSlots = seatsMap.size() - takenSlotsCount();
- double percentTaken = takenSlotsCount() / (double) seatsMap.size() * 100;
- return String.format("%s\t%d/%d\t%.1f%%", code, seatsMap.size() - takenSlotsCount(), seatsMap.size(), percentTaken);
- }
- }
- class Stadium {
- String name;
- Map<String, Sector> sectorMap;
- Stadium(String name) {
- sectorMap = new TreeMap<>();
- this.name = name;
- }
- void createSectors(String[] sectorNames, int[] sizes) {
- for (int i = 0; i < sizes.length; i++) {
- Sector sector = new Sector(sectorNames[i], sizes[i]);
- sectorMap.putIfAbsent(sectorNames[i], sector);
- }
- }
- void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
- sectorMap.get(sectorName).buySeat(seat, type);
- }
- void showSectors() {
- List<Sector> secondList = sectorMap.values().stream().collect(Collectors.toList());
- Comparator<Sector> comparator = Comparator.comparing(Sector::takenSlotsCount).thenComparing(Sector::getCode);
- secondList.stream().sorted(comparator).forEach(x -> System.out.println(x));
- }
- }
- public class StaduimTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- String[] sectorNames = new String[n];
- int[] sectorSizes = new int[n];
- String name = scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- sectorNames[i] = parts[0];
- sectorSizes[i] = Integer.parseInt(parts[1]);
- }
- Stadium stadium = new Stadium(name);
- stadium.createSectors(sectorNames, sectorSizes);
- n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- try {
- stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
- Integer.parseInt(parts[2]));
- } catch (SeatNotAllowedException e) {
- System.out.println("SeatNotAllowedException");
- } catch (SeatTakenException e) {
- System.out.println("SeatTakenException");
- }
- }
- stadium.showSectors();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement