Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Seat {
- private Integer typeOfSeat;
- private Integer seatNumber;
- public Seat(int seatNumber, int typeOfSeat) {
- this.typeOfSeat = typeOfSeat;
- this.seatNumber = seatNumber;
- }
- public int getTypeOfSeat() {
- return typeOfSeat;
- }
- public int getSeatNumber() {
- return seatNumber;
- }
- public boolean equals(Object o) {
- if (o == null) return false;
- if (getClass() != o.getClass()) return false;
- Seat that = (Seat) o;
- return this.seatNumber == that.seatNumber;
- }
- public int hashCode() {
- return seatNumber.hashCode();
- }
- }
- class Sector implements Comparable<Sector> {
- private String sectorName;
- private Integer noOfSeats;
- Set<Seat> seats;
- public Sector(String sectorName, Integer noOfSeats) {
- this.sectorName = sectorName;
- this.noOfSeats = noOfSeats;
- seats = new HashSet<Seat>();
- }
- public String getSectorName() {
- return sectorName;
- }
- public Integer getNoOfSeats() {
- return noOfSeats;
- }
- public void buyTicket(Integer seatNumber, Integer type) throws SeatTakenException, SeatNotAllowedException {
- Seat seat = new Seat(seatNumber, type);
- if (seats.contains(seat)) throw new SeatTakenException();
- if (type != 0)
- for (Seat s : seats)
- if (s.getTypeOfSeat() != type && s.getTypeOfSeat() != 0)
- throw new SeatNotAllowedException();
- seats.add(seat);
- }
- public int compareTo(Sector that) {
- Integer thisDifference = this.getNoOfSeats() - this.seats.size();
- Integer thatDifference = that.getNoOfSeats() - that.seats.size();
- if (thisDifference.compareTo(thatDifference) == 0) {
- return this.getSectorName().compareTo(that.getSectorName());
- } else {
- return thatDifference.compareTo(thisDifference);
- }
- }
- public String toString() {
- float ratio = (float)(noOfSeats - seats.size())/ (noOfSeats);
- ratio *= 100;
- ratio = 100 - ratio;
- return String.format("%s\t%d/%d\t%.1f%%", sectorName, getNoOfSeats() - seats.size(), getNoOfSeats(), ratio);
- }
- }
- class SeatTakenException extends Exception {
- }
- class SeatNotAllowedException extends Exception {
- }
- class Stadium {
- private String stadiumName;
- private Map<String, Sector> nameToSectorMap = new HashMap<String, Sector>();
- public Stadium(String stadiumName) {
- this.stadiumName = stadiumName;
- }
- public String getStadiumName() {
- return stadiumName;
- }
- public void createSectors(String[] sectorNames, int[] sizes) {
- for (int i = 0; i < sectorNames.length; i++) {
- Sector sector = new Sector(sectorNames[i], sizes[i]);
- nameToSectorMap.put(sectorNames[i], sector);
- }
- }
- public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
- Sector sector = nameToSectorMap.get(sectorName);
- sector.buyTicket(seat, type);
- }
- public void showSectors() {
- ArrayList<Sector> sectorArrayList = new ArrayList<Sector>(nameToSectorMap.values());
- Collections.sort(sectorArrayList);
- for (Sector sector : sectorArrayList) System.out.println(sector);
- }
- }
- 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