1 package org.myorg.jpatickets.bl; 2 3 import java.math.BigDecimal; 4 import java.util.List; 5 6 import org.myorg.jpatickets.bo.Event; 7 import org.myorg.jpatickets.bo.Seat; 8 import org.myorg.jpatickets.bo.Ticket; 9 import org.myorg.jpatickets.bo.Venue; 10 import org.myorg.jpatickets.dao.EventMgmtDAO; 11 import org.myorg.jpatickets.dao.VenueDAO; 12 13 public class EventMgmtImpl implements EventMgmt { 14 private EventMgmtDAO edao; 15 private VenueDAO vdao; 16 17 public void setEventDAO(EventMgmtDAO edao) { 18 this.edao = edao; 19 } 20 public void setVenueDAO(VenueDAO vdao) { 21 this.vdao = vdao; 22 } 23 24 @Override 25 public Event createEvent(Event event, Venue venue) throws UnavailableException { 26 BigDecimal maxPrice = new BigDecimal(1000); 27 List<Seat> seats = vdao.getSeatsForVenue(venue, 0, 0); 28 for (Seat seat: seats) { 29 Ticket ticket = new Ticket(event, seat); 30 BigDecimal discount = new BigDecimal(seat.getRow()*.1); 31 ticket.setPrice(maxPrice.subtract(maxPrice.multiply(discount))); 32 event.withTicket(ticket); 33 } 34 return edao.createEvent(event); 35 } 36 37 @Override 38 public Event getEvent(int id) { 39 return edao.getEvent(id); 40 } 41 @Override 42 public Event fetchEventTickets(int id) { 43 return edao.fetchEventTickets(id); 44 } 45 @Override 46 public Event fetchEventTicketsSeats(int id) { 47 return edao.fetchEventTicketsSeats(id); 48 } 49 50 @Override 51 public List<Seat> findSeats(Event event, String section, Integer row, Integer position, int offset, int limit) { 52 return edao.findSeats(event, section, row, position, offset, limit); 53 } 54 55 @Override 56 public List<Ticket> getTickets(Event event, List<Seat> seats) { 57 return edao.findTickets(event, seats); 58 } 59 60 @Override 61 public List<Ticket> reserveSeats(Event event, List<Seat> seats) throws UnavailableException { 62 if (event==null) { throw new IllegalArgumentException("no event provided"); } 63 if (seats==null) { throw new IllegalArgumentException("no seat selected"); } 64 65 List<Ticket> tickets = getTickets(event, seats); 66 for (Ticket ticket: tickets) { 67 if (ticket.isSold()) { 68 throw new UnavailableException(String.format("seat %s already sold", ticket.getSeat())); 69 } else { 70 ticket.setSold(true); 71 } 72 } 73 return tickets; 74 } 75 }