Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.IntStream;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- class SeatNotAllowedException extends Exception{
- public SeatNotAllowedException(String message) {
- super(message);
- }
- }
- class SeatTakenException extends Exception{
- public SeatTakenException(String message) {
- super(message);
- }
- }
- class Stadium {
- String name;
- Map<String,Sector> sectors;
- public Stadium(String name) {
- this.name = name;
- this.sectors= new HashMap<>();
- }
- public void createSectors(String[] sectorNames, int[] sectorSizes) {
- sectors= IntStream.range(0,sectorSizes.length).boxed().map(i->new Sector(sectorNames[i],sectorSizes[i])).collect(Collectors.toMap(
- sector -> sector.getCode(),
- sector -> sector,
- (a,b)-> a,
- HashMap::new
- ));
- }
- public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
- sectors.get(sectorName).buySeat(seat,type);
- }
- public void showSectors() {
- sectors.values().stream().sorted().forEach(i-> System.out.println(i));
- }
- }
- class Seat {
- int number;
- boolean isTaken;
- public Seat(int number) {
- this.number = number;
- this.isTaken = false;
- }
- public int getNumber() {
- return number;
- }
- public void setTaken(boolean taken) {
- isTaken = taken;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- }
- class Sector implements Comparable<Sector> {
- String code;
- int totalSeats;
- List<Seat> seats;
- int type;
- int freeSeats;
- public Sector(String code, int totalSeats) {
- this.code = code;
- this.totalSeats = totalSeats;
- this.freeSeats = totalSeats;
- this.seats = new ArrayList<>();
- IntStream.range(1,totalSeats+2).boxed().forEach(i->seats.add(new Seat(i)));
- this.type=0;
- }
- public String getCode() {
- return code;
- }
- public int getTotalSeats() {
- return totalSeats;
- }
- public List<Seat> getSeats() {
- return seats;
- }
- public int getType() {
- return type;
- }
- public void buySeat(int number, int type) throws SeatTakenException, SeatNotAllowedException {
- if(seats.get(number).isTaken)
- throw new SeatTakenException("SeatTakenException");
- if(getType()!=0&&getType()!=type && type!=0)
- throw new SeatNotAllowedException("SeatNotAllowedException");
- seats.get(number).setTaken(true);
- if(type!=0)
- this.type=type;
- freeSeats--;
- }
- public int getFreeSeats() {
- return freeSeats;
- }
- public double PercentOfFreeSeats(){
- return 1-((double)freeSeats/totalSeats);
- }
- @Override
- public String toString() {
- return String.format("%s\t%d/%d\t%.1f%%",getCode(),getFreeSeats(),getTotalSeats(),100*PercentOfFreeSeats());
- }
- @Override
- public int compareTo(Sector o) {
- return Comparator.comparing(Sector::PercentOfFreeSeats).thenComparing(Sector::getCode).compare(this,o);
- }
- }
- public class StadiumTest {
- 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