|
| 1 | +package com.collections; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by dev on 2/12/2015. |
| 7 | + */ |
| 8 | +public class Theatre { |
| 9 | + private final String theatreName; |
| 10 | + private List<Seat> seats = new ArrayList<>(); |
| 11 | + static final Comparator<Seat> PRICE_ORDER; |
| 12 | + |
| 13 | + static { |
| 14 | + PRICE_ORDER = new Comparator<Seat>() { |
| 15 | + @Override |
| 16 | + public int compare(Seat seat1, Seat seat2) { |
| 17 | + if (seat1.getPrice() < seat2.getPrice()) { |
| 18 | + return -1; |
| 19 | + } else if (seat1.getPrice() > seat2.getPrice()) { |
| 20 | + return 1; |
| 21 | + } else { |
| 22 | + return 0; |
| 23 | + } |
| 24 | + } |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + public Theatre(String theatreName, int numRows, int seatsPerRow) { |
| 29 | + this.theatreName = theatreName; |
| 30 | + |
| 31 | + int lastRow = 'A' + (numRows - 1); |
| 32 | + for (char row = 'A'; row <= lastRow; row++) { |
| 33 | + for (int seatNum = 1; seatNum <= seatsPerRow; seatNum++) { |
| 34 | + double price = 12.00; |
| 35 | + |
| 36 | + if((row < 'D') && (seatNum >=4 && seatNum <=9)) { |
| 37 | + price = 14.00; |
| 38 | + } else if((row > 'F') || (seatNum < 4 || seatNum > 9)) { |
| 39 | + price = 7.00; |
| 40 | + } |
| 41 | + |
| 42 | + Seat seat = new Seat(row + String.format("%02d", seatNum), price); |
| 43 | + seats.add(seat); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public String getTheatreName() { |
| 49 | + return theatreName; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean reserveSeat(String seatNumber) { |
| 53 | + Seat requestedSeat = new Seat(seatNumber, 0); |
| 54 | + int foundSeat = Collections.binarySearch(seats, requestedSeat, null); |
| 55 | + if (foundSeat >= 0) { |
| 56 | + return seats.get(foundSeat).reserve(); |
| 57 | + } else { |
| 58 | + System.out.println("There is no seat " + seatNumber); |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public List<Seat> getSeats() { |
| 64 | + return seats; |
| 65 | + } |
| 66 | + |
| 67 | + public class Seat implements Comparable<Seat> { |
| 68 | + private final String seatNumber; |
| 69 | + private double price; |
| 70 | + private boolean reserved = false; |
| 71 | + |
| 72 | + public Seat(String seatNumber, double price) { |
| 73 | + this.seatNumber = seatNumber; |
| 74 | + this.price = price; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int compareTo(Seat seat) { |
| 79 | + return this.seatNumber.compareToIgnoreCase(seat.getSeatNumber()); |
| 80 | + } |
| 81 | + |
| 82 | + public boolean reserve() { |
| 83 | + if (!this.reserved) { |
| 84 | + this.reserved = true; |
| 85 | + System.out.println("Seat " + seatNumber + " reserved"); |
| 86 | + return true; |
| 87 | + } else { |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public boolean cancel() { |
| 93 | + if (this.reserved) { |
| 94 | + this.reserved = false; |
| 95 | + System.out.println("Reservation of seat " + seatNumber + " cancelled"); |
| 96 | + return true; |
| 97 | + } else { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public String getSeatNumber() { |
| 103 | + return seatNumber; |
| 104 | + } |
| 105 | + |
| 106 | + public double getPrice() { |
| 107 | + return price; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments