Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- 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();
- }
- }
- class SeatNotAllowedException extends Exception {
- }
- class SeatTakenException extends Exception {
- }
- class Sector {
- String name;
- int size;
- /* Seat -> Type of Fan */
- HashMap<Integer, Integer> takenSeats;
- HashSet<Integer> typeOfTakenSeat;
- public Sector(String name, int size) {
- this.name = name;
- this.size = size;
- takenSeats = new HashMap<>();
- typeOfTakenSeat = new HashSet<>();
- }
- public String getName() {
- return name;
- }
- int emptySeats() {
- return size - takenSeats.size();
- }
- public boolean isTaken(int seat) {
- return takenSeats.containsKey(seat);
- }
- public final int TYPE_HOME = 1;
- public final int TYPE_GUEST = 2;
- public void takeSeat(int seat, int type) throws SeatNotAllowedException {
- if (type == TYPE_HOME) {
- if (typeOfTakenSeat.contains(TYPE_GUEST))
- throw new SeatNotAllowedException();
- } else if (type == TYPE_GUEST) {
- if (typeOfTakenSeat.contains(TYPE_HOME))
- throw new SeatNotAllowedException();
- }
- typeOfTakenSeat.add(type);
- takenSeats.put(seat, type);
- }
- private double occupancy() {
- return takenSeats.size() * 100.0 / size;
- }
- @Override
- public String toString() {
- return String.format("%s\t%d/%d\t%.1f%%", name, emptySeats(), size, occupancy());
- }
- }
- class Stadium {
- String name;
- /* Name of Sector -> Sector */
- HashMap<String, Sector> sectors;
- public Stadium(String name) {
- this.name = name;
- sectors = new HashMap<>();
- }
- void addSector(String name, int size) {
- Sector sector = new Sector(name, size);
- sectors.put(name, sector);
- }
- public void createSectors(String[] sectorNames, int[] sectorSizes) {
- IntStream.range(0, sectorNames.length)
- .forEach(i -> addSector(sectorNames[i], sectorSizes[i]));
- }
- public void buyTicket(String sectorName, int seat, int type)
- throws SeatNotAllowedException, SeatTakenException {
- Sector sector = sectors.get(sectorName);
- if (sector.isTaken(seat)) {
- throw new SeatTakenException();
- }
- sector.takeSeat(seat, type);
- }
- public void showSectors() {
- sectors.values().stream()
- .sorted(
- Comparator.comparing(Sector::emptySeats)
- .reversed()
- .thenComparing(Sector::getName)
- )
- .forEach(System.out::println);
- }
- }
Add Comment
Please, Sign In to add comment