Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.stream.Collectors;
- 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 Sector{
- String code;
- int size;
- Map<Integer,Integer> map;
- int type;
- public Sector(String code, int size) {
- super();
- this.code = code;
- this.size = size;
- map=new HashMap<>();
- }
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public int getSize() {
- return size;
- }
- public int freeSeats() {
- return size-map.size();
- }
- @Override
- public String toString() {
- return String.format("%s\t%d/%d\t%.1f%%", code,size-map.size(),size,(map.size()*100.0)/size);
- }
- public void takeSeat(int seat, int t) throws SeatNotAllowedException, SeatTakenException{
- if(map.containsKey(seat)) {
- throw new SeatTakenException();
- }
- if(t==1) {
- if(map.containsValue(2)) {
- throw new SeatNotAllowedException();
- }
- }
- if(t==2) {
- if(map.containsValue(1)) {
- throw new SeatNotAllowedException();
- }
- }
- map.put(seat, t);
- }
- }
- class Stadium{
- String name;
- Map<String,Sector> set;
- public Stadium(String name) {
- this.name=name;
- set=new HashMap<>();
- }
- public void createSectors(String[] sectorNames, int[] sizes) {
- for(int i=0;i<sizes.length;i++) {
- set.put(sectorNames[i], new Sector(sectorNames[i],sizes[i]));
- }
- }
- public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException{
- set.get(sectorName).takeSeat(seat,type);
- }
- public void showSectors() {
- set.values().stream()
- .sorted(Comparator.comparing(Sector::freeSeats).reversed().thenComparing(Sector::getCode))
- .forEach(System.out::println);
- }
- }
- class SeatTakenException extends Exception{
- public SeatTakenException() {
- //super("SeatTakenException");
- }
- }
- class SeatNotAllowedException extends Exception{
- public SeatNotAllowedException() {
- //super("SeatNotAllowedException");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment