Skip to content

Instantly share code, notes, and snippets.

@georgicodes
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save georgicodes/9386848 to your computer and use it in GitHub Desktop.

Select an option

Save georgicodes/9386848 to your computer and use it in GitHub Desktop.
The beginnings of a RESTful API for an event booking system.
public class Booking {
private final Event event;
private final int bookingId;
private final String bookingName;
private final int numTickets;
public Booking(Event event, int bookingId, String bookingName, int numTickets) {
this.bookingName = bookingName;
this.bookingId = bookingId;
this.numTickets = numTickets;
this.event = event;
}
public int getBookingId() {
return bookingId;
}
public String getBookingName() {
return bookingName;
}
public int getNumTickets() {
return numTickets;
}
public Event getEvent() {
return event;
}
}
/**
* A Simple RESTful API that makes bookings for events.
* In a real situation this API would use JSON and need a framework like Jersey.
* Not all RESTful scenarios are listed here for simplicity
*/
public class BookingsAPI {
// POST eg: /bookings
public void createBooking(Booking booking) {
String eventName = booking.getEvent().getEventName();
// if event doesn't exist in database, throw error
if (!Database.events.containsKey(booking.getEvent())) {
System.out.println("Booking attempt failed as event was not found");
throw new UnsupportedOperationException();
}
Database.addBooking(booking);
System.out.println("Booking complete for event: " + eventName + " under bookingName: " + booking.getBookingName()
+ ", with numTickets: " + booking.getNumTickets());
}
// GET eg: /bookings/:bookingId
public Booking getBooking(int bookingId) {
throw new UnsupportedOperationException();
}
// PUT eg: /bookings/:bookingId
public void updateBooking(Booking booking) {
throw new UnsupportedOperationException();
}
// DELETE eg:/bookings/:bookingId
public void deleteBooking(Booking booking) {
throw new UnsupportedOperationException();
}
private static BookingsAPI instance;
private BookingsAPI() {
}
public static BookingsAPI getInstance() {
if (instance == null)
instance = new BookingsAPI();
return instance;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Database {
public static final Map<Event, List<Booking>> events = new HashMap<Event, List<Booking>>();
public static void addBooking(Booking booking) {
Event event = booking.getEvent();
if (booking == null || event == null)
return;
List<Booking> bookings = events.get(event);
bookings.add(booking);
}
public static int getNumTicketsSoldForEvent(Event event) {
List<Booking> bookings = events.get(event);
int numTicketsSold = 0;
for (Booking booking : bookings) {
numTicketsSold += booking.getNumTickets();
}
return numTicketsSold;
}
}
import java.util.ArrayList;
import java.util.List;
public class Event {
private String eventName;
private int maxAttendence;
public Event(String eventName, int maxAttendence) {
this.eventName = eventName;
this.maxAttendence = maxAttendence;
}
public String getEventName() {
return eventName;
}
public int getMaxAttendence() {
return maxAttendence;
}
}
import java.util.ArrayList;
public class TestRunner {
/*
* A simple app which takes booking reservations for events
*/
public static void main(String[] args) {
BookingsAPI bookingAPI = BookingsAPI.getInstance();
Event eventJSConference = new Event("JS Conf", 10);
// Scenario 1 - Make booking when event exists in our database
Database.events.put(eventJSConference, new ArrayList<Booking>()); // update database with event
Booking booking = new Booking(eventJSConference, 1, "Ada Lovelace", 2);
bookingAPI.createBooking(booking);
// Scenario 2 - Make booking when event doesn't exist in our database
Event eventRubyConference = new Event("Ruby Conf", 10);
bookingAPI.createBooking(new Booking(eventRubyConference, 2, "Grace Hopper", 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment