Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.IntStream;
- class Mesto{
- private int type;
- private boolean isFree;
- public Mesto() {
- this.isFree = true;
- }
- public int getType() {
- return type;
- }
- public void setType(int type) {
- this.type = type;
- }
- public boolean isFree() {
- return isFree;
- }
- public void setZafatenost(boolean free) {
- this.isFree = free;
- }
- public String toString(){
- return String.valueOf(type) + String.valueOf(isFree);
- }
- }
- class SeatTakenException extends Exception{
- SeatTakenException(){
- super("SeatTakenException");
- }
- SeatTakenException(String s){
- super(s);
- }
- }
- class SeatNotAllowedException extends Exception{
- SeatNotAllowedException(){
- super("SeatTakenException");
- }
- SeatNotAllowedException(String s){
- super(s);
- }
- }
- class Sector{
- private String code;
- private int brojMesta;
- private List<Mesto> mesta;
- public Sector(String code, int brojMesta){
- this.code = code;
- this.brojMesta = brojMesta;
- mesta = new ArrayList<>(brojMesta);
- IntStream.range(0, brojMesta).forEach(i -> mesta.add(new Mesto()));
- }
- public int freePlaces(){
- return (int)mesta.stream()
- .filter(Mesto::isFree)
- .count();
- }
- public String getCode() {
- return code;
- }
- public List<Mesto> getMesta() {
- return mesta;
- }
- public String toString(){
- float zafatenost = (float)(mesta.size() - freePlaces()) / mesta.size() * 100;
- String s = freePlaces() + "/" + mesta.size();
- return String.format("%s\t%s\t%.1f%%", code, s, zafatenost);
- }
- }
- class Stadium{
- private String name;
- private List<Sector> sectors;
- public Stadium(String name) {
- this.name = name;
- sectors = new ArrayList<>();
- }
- public void createSectors(String [] sectorNames, int[] sizes){
- IntStream.range(0, sectorNames.length)
- .forEach(i -> sectors.add(new Sector(sectorNames[i], sizes[i])));
- }
- public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException{
- Sector sector = sectors.stream()
- .filter(s -> s.getCode().equals(sectorName))
- .findFirst()
- .get();
- Mesto m = sector.getMesta().get(seat-1);
- if(!m.isFree())
- throw new SeatTakenException();
- boolean wrongType = sector.getMesta().stream()
- .filter(mesto -> !mesto.isFree())
- .filter(mesto -> mesto.getType() != 0)
- .filter(mesto ->mesto.getType() != type)
- .anyMatch(mesto -> type != 0);
- if(wrongType)
- throw new SeatNotAllowedException();
- m.setType(type);
- m.setZafatenost(false);
- }
- public void showSectors(){
- sectors.stream()
- .sorted(Comparator.comparing(Sector::freePlaces).reversed().thenComparing(Sector::getCode))
- .forEach(System.out::println);
- }
- }
- 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