0% found this document useful (0 votes)
34 views8 pages

Java Bus Management System OOP

The document outlines an OOPS-II experiment to create a Bus Management System in Java, focusing on managing buses, passengers, bookings, and tickets using object-oriented programming concepts. It includes an algorithm for the program flow, class diagrams for various components like Passenger, Ticket, Booking, Bus, and BusCompany, and a sample source code implementation. The program allows users to book seats, search for passengers, and view bus status, demonstrating practical application of OOP principles.

Uploaded by

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

Java Bus Management System OOP

The document outlines an OOPS-II experiment to create a Bus Management System in Java, focusing on managing buses, passengers, bookings, and tickets using object-oriented programming concepts. It includes an algorithm for the program flow, class diagrams for various components like Passenger, Ticket, Booking, Bus, and BusCompany, and a sample source code implementation. The program allows users to book seats, search for passengers, and view bus status, demonstrating practical application of OOP principles.

Uploaded by

Chandran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OOPS-II EXPERIMENT

AIM:
To develop a Bus Management System in Java that manages buses,
passengers, bookings, and tickets using object-oriented concepts by passing
and returning objects through methods.

ALGORITHM:
1. Start the program.
2. Create a bus company named CityLine.
3. Ask the user to enter a bus number.
4. Ask the user to enter bus capacity.
5. Create a Bus object with the given bus number and capacity.
6. Add the bus to the company using addBus().
7. Repeat the following steps (capacity + 1) times:
7.1 Ask the user to enter passenger name.
7.2 Ask the user to enter passenger age.
7.3 Create a Passenger object.
7.4 Call the bookSeat(passenger) method.
- If seats are available, create a Ticket and Booking, add to bus, and display success
message.
- Otherwise, display a bus full message.
8. Call getBookedSeats() to find total number of booked seats.
9. Display the total number of booked seats.
10. Ask the user to enter a passenger name to search.
11. Call findPassenger(name) method.
12. If passenger is found, display passenger details.
13. If passenger is not found, display "Passenger not found".
14. Call showAllBuses() to display the status of all buses in the company.
15. End the program.

CLASS DIAGRAM:
SOURCE CODE:
// [Link]
import [Link].*;

// 1. Passenger class
class Passenger {
private String name;
private int age;
public Passenger(String name, int age) {
[Link] = name;
[Link] = age;
}
public String getName() { return name; }
public int getAge() { return age; }
@Override
public String toString() {
return name + " (age " + age + ")";
}
}
// 2. Ticket class
class Ticket {
private static int counter = 1000; // ticket number generator
private int ticketNumber;
private String busNumber;
private Passenger passenger;

public Ticket(String busNumber, Passenger passenger) {


[Link] = counter++;
[Link] = busNumber;
[Link] = passenger;
}
public int getTicketNumber() { return ticketNumber; }
public Passenger getPassenger() { return passenger; }
@Override
public String toString() {
return "Ticket#" + ticketNumber + " | Bus: " + busNumber + " | Passenger: " +
passenger;
}
}
// 3. Booking class
class Booking {
private Passenger passenger;
private Ticket ticket;
public Booking(Passenger passenger, Ticket ticket) {
[Link] = passenger;
[Link] = ticket;
}
public Passenger getPassenger() { return passenger; }
public Ticket getTicket() { return ticket; }
}
// 4. Bus class
class Bus {
private String busNumber;
private int capacity;
private List<Booking> bookings = new ArrayList<>();
public Bus(String busNumber, int capacity) {
[Link] = busNumber;
[Link] = capacity;
}
public boolean bookSeat(Passenger p) {
if ([Link]() < capacity) {
Ticket t = new Ticket(busNumber, p);
[Link](new Booking(p, t));
[Link]("Seat booked! " + t);
return true;
}
[Link](" Bus " + busNumber + " is full. Cannot book for " + [Link]());
return false;
}
public Passenger findPassenger(String name) {
for (Booking b : bookings) {
if ([Link]().getName().equalsIgnoreCase(name)) {
return [Link]();
}
}
return null;
}
public int getBookedSeats() { return [Link](); }
public String getStatus() {
return "Bus " + busNumber + " | Capacity: " + capacity + " | Booked: " + [Link]();
}
}
// 5. BusCompany class
class BusCompany {
private String companyName;
private List<Bus> buses = new ArrayList<>();
public BusCompany(String companyName) {
[Link] = companyName;
}
public void addBus(Bus bus) {
[Link](bus);
}
public Bus getBus(String busNumber) {
for (Bus b : buses) {
if ([Link]().contains(busNumber)) {
return b;
}
}
return null;
}
public void showAllBuses() {
[Link]("\n--- " + companyName + " Bus Status ---");
for (Bus b : buses) {
[Link]([Link]());
}
}
}
// 6. BusManagement class (main driver)
public class BusManagement {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Create company
BusCompany company = new BusCompany("CityLine");
// Add one bus for demo
[Link]("Enter bus number: ");
String busNumber = [Link]();
[Link]("Enter bus capacity: ");
int capacity = [Link]();
[Link]();
Bus bus = new Bus(busNumber, capacity);
[Link](bus);
// Book seats
for (int i = 0; i < capacity + 1; i++) {
[Link]("\nEnter passenger name: ");
String name = [Link]();
[Link]("Enter passenger age: ");
int age = [Link]();
[Link]();
Passenger p = new Passenger(name, age);
[Link](p);
}
// Check total booked seats
[Link]("\nTotal booked seats: " + [Link]());
// Search passenger
[Link]("\nEnter name to search passenger: ");
String searchName = [Link]();
Passenger found = [Link](searchName);
if (found != null) {
[Link]("Found passenger: " + found);
} else {
[Link]("Passenger not found.");
}
// Show company bus status
[Link]();
[Link]();
}
}

INPUT:
Enter bus number: TN45A1234
Enter bus capacity: 2
Enter passenger name: Arun
Enter passenger age: 25
Seat booked! Ticket#1000 | Bus: TN45A1234 | Passenger: Arun (age 25)
Enter passenger name: Priya
Enter passenger age: 30
Seat booked! Ticket#1001 | Bus: TN45A1234 | Passenger: Priya (age 30)
Enter passenger name: Karthik
Enter passenger age: 28
Bus TN45A1234 is full. Cannot book for Karthik

OUTPUT:
Total booked seats: 2
Enter name to search passenger: Priya
Found passenger: Priya (age 30)
--- CityLine Bus Status ---
Bus TN45A1234 | Capacity: 2 | Booked: 2

Common questions

Powered by AI

The Bus Management System applies object-oriented principles extensively. Encapsulation is shown by wrapping data and methods inside classes such as Passenger, Ticket, Bus, and BusCompany. Inheritance isn't explicitly used here, but polymorphism and abstraction can be indirectly noted in class constructions and methods like bookSeat and getStatus, which manage and display encapsulated state. Moreover, modular interaction is evident through methods like addBus, bookSeat, and findPassenger, fostering loose coupling and high cohesion, core traits of object-oriented design .

The process begins with starting the program and creating a bus company named CityLine. The user is prompted to enter a bus number and capacity, which are used to create a Bus object. This bus is then added to the company using the addBus() method. For each passenger, the user must enter their name and age, which are used to create a Passenger object. The bookSeat(passenger) method attempts to book the seat; if successful, a Ticket and a Booking are added to the bus. The total number of booked seats is found using getBookedSeats(). Users can search for passengers by name through the findPassenger(name) method. The showAllBuses() method displays the status of all buses. The program ends after these operations .

To extend the system with route management, a new Route class could be introduced to encapsulate route details such as starting point, destination, and intermediate stops. This class could be associated with each Bus. For dynamic pricing, a PricingStrategy interface could define methods for determining ticket prices based on factors such as demand, time, or route length. Bus could utilize this interface to dynamically calculate costs during ticket booking. Additional methods like updateRoute() in BusCompany could manage route changes, and applyPricing() could adjust fares in Booking or Ticket. A database layer or configuration files might store routing and pricing data for persistence .

The current system model heavily relies on user input at several stages, such as entering bus details, passenger information, and querying bookings. While this interactivity can be useful for tailored input, it may not scale efficiently with larger data or multiple buses. To optimize, the process could leverage batch processing or preloaded data for bus and passenger setup, reducing real-time input dependency. An improved GUI or form-based input could streamline interactions, allowing users to input data simultaneously rather than via sequential prompts .

The Bus class uses the bookSeat(Passenger p) method for seat booking. It checks if the current number of bookings is less than the bus's capacity. If a seat is available, it creates a Ticket object with the bus number and passenger information, then adds a new Booking consisting of the Passenger and Ticket to the bus's list of bookings. If the bus is full, it outputs a message indicating that no booking can be made for the passenger. This demonstrates both object creation (Passenger, Ticket, Booking) and adherence to capacity constraints .

The Ticket class ensures unique ticket identification using a static integer counter that starts at 1000. Each time a Ticket object is instantiated, the constructor increments the counter and assigns its value to the ticketNumber of the new Ticket, ensuring that every ticket has a unique number. This unique identifier is used in conjunction with the bus number and passenger details to represent each ticket .

Using a static ticket counter for generating ticket numbers implies that ticket numbers remain unique across all instances of the system. This can be beneficial, ensuring no duplication as the counter increments globally for every Ticket created. However, in real-world applications, particularly those distributed or managing numerous buses across different services or systems, a centralized mechanism might be needed to reset or adjust this counter, potentially requiring persistence layers or unique identifiers incorporating additional context like service date or bus route .

The Passenger class effectively employs encapsulation by making its fields private and providing public accessor methods (getName, getAge) to interact with those fields. This design encapsulates the data, controlling access and modifications, thus enhancing security and data integrity. The use of a toString() method for representation also adds practical utility, enabling direct logging and output that are human-readable. However, further methods for data validation or additional encapsulated attributes could enhance its robustness for broader applications .

The status of all buses is checked and displayed using the showAllBuses() method of the BusCompany class. This method iterates over the list of buses owned by the company, calling the getStatus() method for each bus. The getStatus() method in the Bus class returns a string showing the bus number, its capacity, and the current number of booked seats .

The findPassenger() method in the Bus class searches through the list of bookings to find a passenger by name. It iterates over the bookings, accessing each Booking's Passenger and comparing their name to the provided search term using equalsIgnoreCase(). If a match is found, it returns the Passenger object; otherwise, it returns null. This linear search approach serves effectively within the context of potentially small collections of bookings per bus .

You might also like