Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stadium
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Scanner;
- class SeatTakenException extends RuntimeException{}
- class SeatNotAllowedException extends RuntimeException{}
- class Sector
- {
- String code;
- int seats;
- ArrayList<Integer> seatsTaken;
- boolean type[];
- Sector(String code, int seats)
- {
- this.code = code;
- this.seats = seats;
- this.seatsTaken = new ArrayList<>();
- this.type = new boolean[3];
- }
- public String getCode() {
- return code;
- }
- public int getSeats() {
- return seats;
- }
- void buyTicket(int seat, int seatType)
- {
- if(seatsTaken.contains(seat-1))
- throw new SeatTakenException();
- if(seatType == 1)
- if(type[2])
- throw new SeatNotAllowedException();
- if(seatType == 2)
- if(type[1])
- throw new SeatNotAllowedException()
- ;
- seatsTaken.add(seat-1);
- type[seatType] = true;
- }
- int getFreeSeats() {
- return seats - seatsTaken.size();
- }
- double getPercent() {
- return (1 - (1.0 * getFreeSeats() / seats)) * 100;
- }
- @Override
- public String toString() {
- return String.format("%s\t%d/%d\t%.1f%%", code, getFreeSeats(), seats, getPercent());
- }
- }
- class Stadium
- {
- String name;
- ArrayList<Sector> sectors;
- Stadium(String name)
- {
- this.name = name;
- this.sectors = new ArrayList<>();
- }
- void createSectors(String[] sectorNames, int[] sizes)
- {
- for(int i=0; i<sizes.length; i++)
- {
- sectors.add(new Sector(sectorNames[i], sizes[i]));
- }
- }
- void buyTicket(String sectorName, int seat, int type)
- {
- this.sectors.stream()
- .filter(t -> t.code.equals(sectorName))
- .findFirst()
- .get()
- .buyTicket(seat, type);
- }
- void showSectors()
- {
- this.sectors.stream()
- .sorted(Comparator.comparing(Sector::getFreeSeats).reversed())
- .forEach(System.out :: println);
- }
- }
- 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