0% found this document useful (0 votes)
8 views13 pages

BUS Management System

Uploaded by

manav.koirala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

BUS Management System

Uploaded by

manav.koirala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

BUS MANAGEMENT SYSTEM: -

pseudocode: -

1. START

2. DECLARE structure Bus:

- integer bus_id

- string driver_name

- integer total_seats

- integer booked_seats

3. DECLARE file pointer

4. DECLARE integer i

5. DECLARE function addBus()

6. DECLARE function viewBuses()

7. DECLARE function bookSeat()

8. MAIN:

a. PRINT "=== Bus Reservation System ==="

b. LOOP:

- PRINT menu options (Add Bus, View Buses, Book a Seat, Exit)

- GET user choice

c. IF choice is 1 THEN:

- CALL addBus()

d. ELSE IF choice is 2 THEN:

- CALL viewBuses()

e. ELSE IF choice is 3 THEN:

- CALL bookSeat()

f. ELSE IF choice is 4 THEN:


- PRINT "Exiting the program."

- EXIT the program

g. ELSE:

- PRINT "Invalid choice. Please try again."

9. FUNCTION addBus:

a. CREATE a Bus object

b. PRINT "Enter Bus ID:"

c. GET bus_id

d. PRINT "Enter Driver's Name:"

e. GET driver_name

f. PRINT "Enter Total Seats:"

g. GET total_seats

h. SET booked_seats to 0

i. OPEN "buses.txt" in append mode

j. IF file cannot be opened THEN:

- PRINT "Error opening file!"

- RETURN from function

k. WRITE bus object to file

l. CLOSE the file

m. PRINT "Bus added successfully!"

10. FUNCTION viewBuses:

a. CREATE a Bus object

b. OPEN "buses.txt" in read mode

c. IF file cannot be opened THEN:

- PRINT "No buses found!"


- RETURN from function

d. PRINT "List of Buses"

e. PRINT column headers (ID, Driver, Total Seats, Booked Seats)

f. WHILE there are buses in the file:

- READ bus data from file

- PRINT bus details (ID, Driver, Total Seats, Booked Seats)

g. CLOSE the file

11. FUNCTION bookSeat:

a. DECLARE integer bus_id, seats_to_book, found as 0

b. DECLARE temporary file pointer

c. PRINT "Enter Bus ID to book a seat:"

d. GET bus_id

e. OPEN "buses.txt" in read mode

f. IF file cannot be opened THEN:

- PRINT "No buses found!"

- RETURN from function

g. OPEN "temp.txt" in write mode

h. IF temporary file cannot be opened THEN:

- PRINT "Error opening temporary file!"

- CLOSE the original file

- RETURN from function

i. WHILE there are buses in the file:

- READ bus data from file

- IF bus_id matches:

- SET found to 1
- PRINT "Enter number of seats to book:"

- GET seats_to_book

- IF available seats are sufficient THEN:

- Increase booked_seats by seats_to_book

- PRINT success message

- ELSE:

- PRINT "Not enough available seats!"

- WRITE updated bus data to temporary file

j. CLOSE both files

k. IF found THEN:

- DELETE original "buses.txt" file

- RENAME "temp.txt" to "buses.txt"

l. ELSE:

- PRINT "Bus with ID not found!"

- DELETE "temp.txt"

12. END
Snippets: -

1. Login Function

Adding a Bus (addBus function)

Purpose: Allows the user to add a bus with its ID, driver’s name, and total seats to the system.
Key Operations:

Prompts the user to input bus details (ID, driver name, total seats).
Initializes the number of booked seats to 0 since the bus is newly added.
Opens the file (buses.txt) in append mode (ab) to add the bus details.
Uses fwrite() to save the bus data to the file.
Closes the file and notifies the user of the successful addition.
2. Viewing All Buses (viewBuses function)

2. Viewing All Buses (viewBuses function)

Purpose: Displays the details of all buses currently stored in the system.
Key Operations:

Opens the buses.txt file in binary read mode (rb).


Checks if the file exists. If not, notifies the user.
Uses fread() in a loop to read each bus record and prints the bus
details (ID, driver, total seats, booked seats).
Closes the file after reading all records.
Displays a formatted table with the bus details.
3. Booking a Seat (bookSeat function)

Key Operations:
Purpose: Allows a user to book seats on a specific bus.
Prompts the user for the bus ID and number of seats to book.
Searches for the bus with the provided ID in the file.
Checks if there are enough available seats on the bus.
If the bus exists and there are enough seats, updates the number of booked seats.
Creates a temporary file (temp.txt) to write the updated bus records,
including the newly updated bus.
After processing, deletes the original file and renames the temporary file to the original file name.
Handles errors (like no buses found or insufficient seats) and cleans up if needed.
4. Main Menu Loop (main function)

4. Main Menu Loop (main function)

Purpose: Displays the main menu and allows the user to choose an action.
Key Operations:
Displays a menu with four options: Add Bus, View Buses, Book a Seat, and Exit.
Takes input from the user to determine the next action.
Uses a switch statement to call the appropriate function based on the user’s choice.
Handles invalid input by asking the user to try again.
Exits the program if the user selects the "Exit" option.
CODE: -
INPUT AND OUTPUT: -

You might also like