0% found this document useful (0 votes)
0 views

CabBookingApp Java OOP

The document outlines a Java-based cab booking system utilizing object-oriented programming principles such as classes, inheritance, and interfaces. It includes a basic structure with classes for User, Rider, Driver, and Cab, as well as a booking mechanism and a menu-driven interface for riders and admins. Additionally, it features functionality for saving ride history to a file and viewing it through an admin login.
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)
0 views

CabBookingApp Java OOP

The document outlines a Java-based cab booking system utilizing object-oriented programming principles such as classes, inheritance, and interfaces. It includes a basic structure with classes for User, Rider, Driver, and Cab, as well as a booking mechanism and a menu-driven interface for riders and admins. Additionally, it features functionality for saving ride history to a file and viewing it through an admin login.
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/ 4

CAB BOOKING SYSTEM IN JAVA (Using OOP)

Concepts Covered:
- Class & Object
- Constructor
- Inheritance
- Encapsulation
- Abstraction & Interface
- Polymorphism
- static / final
- Exception Handling

Phase 1: Basic Cab Booking Structure

1. User Class
```java
public class User {
protected String name;
protected String phone;
public User(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
```

2. Rider Class (extends User)


```java
public class Rider extends User {
public Rider(String name, String phone) {
super(name, phone);
}
public void requestRide() {
System.out.println(name + " is requesting a ride...");
}
}
```

3. Driver Class
```java
public class Driver {
private String name;
private String cabType;
public Driver(String name, String cabType) {
this.name = name;
this.cabType = cabType;
}
public String getCabType() { return cabType; }
public String getName() { return name; }
}
```

4. Cab Class
```java
public class Cab {
private static int availableCabs = 3;
public static final double PRICE_PER_KM = 10.0;
public static boolean isAvailable() {
return availableCabs > 0;
}
public static void assignCab() throws Exception {
if (!isAvailable()) throw new Exception("No cabs available!");
availableCabs--;
}
public static void endRide() {
availableCabs++;
}
}
```

5. Bookable Interface
```java
public interface Bookable {
void bookCab(double distance);
}
```

6. CabBooking Class (implements Bookable)


```java
public class CabBooking implements Bookable {
private Rider rider;
private Driver driver;
private double distance;

public CabBooking(Rider rider, Driver driver, double distance) {


this.rider = rider;
this.driver = driver;
this.distance = distance;
}

@Override
public void bookCab(double distance) {
try {
Cab.assignCab();
System.out.println("Cab booked for " + rider.name + " with driver " + driver.getName());
double fare = distance * Cab.PRICE_PER_KM;
System.out.println("Distance: " + distance + " km. Total Fare: INR" + fare);
} catch (Exception e) {
System.out.println("Booking failed: " + e.getMessage());
}
}
}
```

7. Main Method (Initial Test)


```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Driver driver = new Driver("Ravi", "Sedan");
Rider rider = new Rider("Amit", "9876543210");

System.out.print("Enter distance to travel (in km): ");


double distance = sc.nextDouble();

CabBooking booking = new CabBooking(rider, driver, distance);


booking.bookCab(distance);

sc.close();
}
}
```

Phase 2: Menu-Driven System with Admin & File History

1. Main Menu Loop


```java
while (true) {
System.out.println("\n=== CAB BOOKING SYSTEM ===");
System.out.println("1. Login as Rider");
System.out.println("2. Login as Admin");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1: loginAsRider(); break;
case 2: loginAsAdmin(); break;
case 3: System.exit(0);
default: System.out.println("Invalid choice.");
}
}
```

2. Rider Login & Booking


```java
public static void loginAsRider() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your phone: ");
String phone = sc.nextLine();

Rider rider = new Rider(name, phone);


System.out.println("Available Cab Types: 1. Mini 2. Sedan");
int type = sc.nextInt();
sc.nextLine();

Driver driver = new Driver((type == 1) ? "Sunil" : "Ravi", (type == 1) ? "Mini" : "Sedan");


System.out.print("Enter distance (in km): ");
double distance = sc.nextDouble();

CabBooking booking = new CabBooking(rider, driver, distance);


booking.bookCab(distance);

saveRideHistory(rider, driver, distance);


}
```

3. Save Ride History to File


```java
public static void saveRideHistory(Rider rider, Driver driver, double distance) {
try {
FileWriter fw = new FileWriter("ride_history.txt", true);
fw.write(rider.name + " booked a " + driver.getCabType() + " with " + driver.getName() +
" for " + distance + " km\n");
fw.close();
} catch (IOException e) {
System.out.println("Error saving ride history.");
}
}
```

4. Admin View Ride History


```java
public static void loginAsAdmin() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Admin Password: ");
String pass = sc.nextLine();
if (!pass.equals("admin123")) {
System.out.println("Access Denied.");
return;
}

try {
BufferedReader reader = new BufferedReader(new FileReader("ride_history.txt"));
String line;
System.out.println("\n--- Ride History ---");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
System.out.println("No history found.");
}
}
```

You might also like