Can any give me algorithm for online railway reservation system?

Dani AI

Generated

For : a compact, practical booking-flow you can adapt. As and suggested, study reservation-system concepts first, but the sketch below gives a concrete algorithm for search -> hold -> payment -> confirm + waitlist handling. It focuses on correctness under concurrency and on a simple, testable workflow you can implement in a relational DB plus a few background workers.

function bookTicket(userId, trainId, date, travelClass, passengers):
    seatsNeeded = len(passengers)
    if not trainRuns(trainId, date): return {status:"NO_SERVICE"}

    if getAvailableSeatCount(trainId,date,travelClass) < seatsNeeded:
        pos = addToWaitlist(userId, trainId, date, travelClass, passengers)
        return {status:"WAITLIST", position:pos}

    // Atomic allocation: lock inventory rows
    begin transaction
        seats = selectFreeSeatsForUpdate(trainId,date,travelClass, seatsNeeded)
        if len(seats) < seatsNeeded:
            rollback
            return {status:"CONFLICT"} // caller may retry or go to waitlist
        markSeatsHeld(seats, holdExpiry = now + HOLD_WINDOW)
        bookingId = createBooking(userId, trainId, date, travelClass, passengers, status="PENDING", holdExpiry)
        linkSeatsToBooking(bookingId, seats)
    commit

    // Payment (can be async): on success confirm, on failure cancel
    if processPayment(userId, bookingId, amount) == SUCCESS:
        begin transaction
            updateBookingStatus(bookingId, "CONFIRMED"); markSeatsBooked(seats)
            pnr = generatePNR(bookingId)
        commit
        return {status:"CONFIRMED", pnr:pnr}
    else:
        cancelBooking(bookingId) // releases seats
        return {status:"FAILED"}

Background worker (simplified): scan expired PENDING bookings, cancel and release seats; when seats free, pop next waitlist candidate, create a PENDING booking with a HOLD_WINDOW and notify them. Important cautions: use DB transactions or SELECT ... FOR UPDATE (or optimistic locking) to avoid race conditions; make payment callbacks idempotent; use a short configurable hold window (commonly 5–15 minutes); implement quotas/allocation policy separately; and put allocation logic in a single service or partition inventory by train+date to reduce contention. Test heavy concurrent booking, partial failures, and payment/timeouts before production.

Recommended Answers

All 2 Replies

Google is your friend. We don't give answers to school problems. If this isn't a school problem I'd still tell you to do a Google search. There are relevant articles on the Wikipedia, but in any case, you first need to understand reservation systems themselves: http://en.wikipedia.org/wiki/Computer_reservations_system

The above link is for airline reservation systems primarily, but the principles are the same.

Make google your friend, partner...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.