CabBookingApp Java OOP
CabBookingApp Java OOP
Concepts Covered:
- Class & Object
- Constructor
- Inheritance
- Encapsulation
- Abstraction & Interface
- Polymorphism
- static / final
- Exception Handling
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;
}
}
```
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);
}
```
@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());
}
}
}
```
sc.close();
}
}
```
switch (choice) {
case 1: loginAsRider(); break;
case 2: loginAsAdmin(); break;
case 3: System.exit(0);
default: System.out.println("Invalid choice.");
}
}
```
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.");
}
}
```