Java Reference
In-Depth Information
public int getSeatPrice(int seatId) throws
NoSuchSeatException {
return getSeat(seatId).getPrice();
public void buyTicket(int seatId) throws
SeatBookedException, NoSuchSeatException {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new SeatBookedException("Seat " + seatId
+ " already booked!");
private Seat getSeat(int seatId) throws
NoSuchSeatException {
final Seat seat = seats.get(seatId);
if (seat == null) {
throw new NoSuchSeatException("Seat " + seatId
+ " does not exist!");
return seat;
Let's see our application code in detail; the void method setupTheatre is invoked as
soon as the application is deployed and takes care of assembling the theatre seats, creating
a simple map of the Seat objects. Seat identifiers are key factors in this map. This hap-
pens right after deployment because our bean is annotated with @Singleton and
@Startup that force the container to initialize the bean during startup. Each Seat ob-
ject is constructed using a set of three field constructors, which includes the seat ID, its
Search WWH ::

Custom Search