Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Seat {
  4.     private Integer typeOfSeat;
  5.     private Integer seatNumber;
  6.  
  7.     public Seat(int seatNumber, int typeOfSeat) {
  8.         this.typeOfSeat = typeOfSeat;
  9.         this.seatNumber = seatNumber;
  10.     }
  11.  
  12.     public int getTypeOfSeat() {
  13.         return typeOfSeat;
  14.     }
  15.  
  16.     public int getSeatNumber() {
  17.         return seatNumber;
  18.     }
  19.  
  20.     public boolean equals(Object o) {
  21.         if (o == null) return false;
  22.         if (getClass() != o.getClass()) return false;
  23.         Seat that = (Seat) o;
  24.         return this.seatNumber == that.seatNumber;
  25.     }
  26.  
  27.     public int hashCode() {
  28.         return seatNumber.hashCode();
  29.     }
  30. }
  31.  
  32. class Sector implements  Comparable<Sector> {
  33.     private String sectorName;
  34.     private Integer noOfSeats;
  35.     Set<Seat> seats;
  36.  
  37.     public Sector(String sectorName, Integer noOfSeats) {
  38.         this.sectorName = sectorName;
  39.         this.noOfSeats = noOfSeats;
  40.         seats = new HashSet<Seat>();
  41.     }
  42.  
  43.     public String getSectorName() {
  44.         return sectorName;
  45.     }
  46.  
  47.     public Integer getNoOfSeats() {
  48.         return noOfSeats;
  49.     }
  50.  
  51.     public void buyTicket(Integer seatNumber, Integer type) throws SeatTakenException, SeatNotAllowedException {
  52.         Seat seat = new Seat(seatNumber, type);
  53.         if (seats.contains(seat)) throw new SeatTakenException();
  54.         if (type != 0)
  55.             for (Seat s : seats)
  56.                 if (s.getTypeOfSeat() != type && s.getTypeOfSeat() != 0)
  57.                     throw new SeatNotAllowedException();
  58.         seats.add(seat);
  59.     }
  60.  
  61.     public int compareTo(Sector that) {
  62.         Integer thisDifference = this.getNoOfSeats() - this.seats.size();
  63.         Integer thatDifference = that.getNoOfSeats() - that.seats.size();
  64.         if (thisDifference.compareTo(thatDifference) == 0) {
  65.             return this.getSectorName().compareTo(that.getSectorName());
  66.         } else {
  67.             return thatDifference.compareTo(thisDifference);
  68.         }
  69.     }
  70.  
  71.     public String toString() {
  72.         float ratio = (float)(noOfSeats - seats.size())/ (noOfSeats);
  73.         ratio *= 100;
  74.         ratio = 100 - ratio;
  75.         return String.format("%s\t%d/%d\t%.1f%%", sectorName, getNoOfSeats() - seats.size(), getNoOfSeats(), ratio);
  76.     }
  77. }
  78.  
  79. class SeatTakenException extends Exception {
  80.  
  81. }
  82.  
  83. class SeatNotAllowedException extends Exception {
  84.  
  85. }
  86.  
  87. class Stadium {
  88.     private String stadiumName;
  89.     private Map<String, Sector> nameToSectorMap = new HashMap<String, Sector>();
  90.  
  91.     public Stadium(String stadiumName) {
  92.         this.stadiumName = stadiumName;
  93.     }
  94.  
  95.     public String getStadiumName() {
  96.         return stadiumName;
  97.     }
  98.  
  99.     public void createSectors(String[] sectorNames, int[] sizes) {
  100.         for (int i = 0; i < sectorNames.length; i++) {
  101.             Sector sector = new Sector(sectorNames[i], sizes[i]);
  102.             nameToSectorMap.put(sectorNames[i], sector);
  103.         }
  104.     }
  105.  
  106.     public void buyTicket(String sectorName, int seat, int type) throws SeatTakenException, SeatNotAllowedException {
  107.         Sector sector = nameToSectorMap.get(sectorName);
  108.         sector.buyTicket(seat, type);
  109.     }
  110.  
  111.     public void showSectors() {
  112.         ArrayList<Sector> sectorArrayList = new ArrayList<Sector>(nameToSectorMap.values());
  113.         Collections.sort(sectorArrayList);
  114.         for (Sector sector : sectorArrayList) System.out.println(sector);
  115.     }
  116. }
  117.  
  118. public class StaduimTest {
  119.     public static void main(String[] args) {
  120.         Scanner scanner = new Scanner(System.in);
  121.         int n = scanner.nextInt();
  122.         scanner.nextLine();
  123.         String[] sectorNames = new String[n];
  124.         int[] sectorSizes = new int[n];
  125.         String name = scanner.nextLine();
  126.         for (int i = 0; i < n; ++i) {
  127.             String line = scanner.nextLine();
  128.             String[] parts = line.split(";");
  129.             sectorNames[i] = parts[0];
  130.             sectorSizes[i] = Integer.parseInt(parts[1]);
  131.         }
  132.         Stadium stadium = new Stadium(name);
  133.         stadium.createSectors(sectorNames, sectorSizes);
  134.         n = scanner.nextInt();
  135.         scanner.nextLine();
  136.         for (int i = 0; i < n; ++i) {
  137.             String line = scanner.nextLine();
  138.             String[] parts = line.split(";");
  139.             try {
  140.                 stadium.buyTicket(parts[0], Integer.parseInt(parts[1]),
  141.                         Integer.parseInt(parts[2]));
  142.             } catch (SeatNotAllowedException e) {
  143.                 System.out.println("SeatNotAllowedException");
  144.             } catch (SeatTakenException e) {
  145.                 System.out.println("SeatTakenException");
  146.             }
  147.         }
  148.         stadium.showSectors();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement