Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- // Base class for Users
- abstract class Person {
- protected String name;
- protected String contact;
- protected Location location;
- public Person(String name, String contact, Location location) {
- this.name = name;
- this.contact = contact;
- this.location = location;
- }
- public void updateLocation(Location location) {
- this.location = location;
- }
- }
- // Location class
- class Location {
- double x, y;
- public Location(double x, double y) {
- this.x = x;
- this.y = y;
- }
- public double distanceTo(Location other) {
- return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
- }
- }
- // User class
- class User extends Person {
- public User(String name, String contact, Location location) {
- super(name, contact, location);
- }
- }
- // Driver class
- class Driver extends Person {
- private String vehicleDetails;
- private boolean available;
- private double earnings;
- public Driver(String name, String contact, Location location, String vehicleDetails) {
- super(name, contact, location);
- this.vehicleDetails = vehicleDetails;
- this.available = true;
- this.earnings = 0;
- }
- public boolean isAvailable() {
- return available;
- }
- public void setAvailability(boolean status) {
- this.available = status;
- }
- public void addEarnings(double fare) {
- this.earnings += fare;
- }
- public double getEarnings() {
- return earnings;
- }
- }
- // Ride class
- class Ride {
- private User user;
- private Driver driver;
- private Location source;
- private Location destination;
- private double fare;
- public Ride(User user, Driver driver, Location source, Location destination, double fare) {
- this.user = user;
- this.driver = driver;
- this.source = source;
- this.destination = destination;
- this.fare = fare;
- }
- }
- // RideService to manage rides
- class RideService {
- private List<Ride> rideHistory = new ArrayList<>();
- public Ride bookRide(User user, Driver driver, Location source, Location destination) {
- double distance = source.distanceTo(destination);
- double fare = distance * 10; // Example fare calculation
- driver.addEarnings(fare);
- Ride ride = new Ride(user, driver, source, destination, fare);
- rideHistory.add(ride);
- driver.setAvailability(false);
- return ride;
- }
- }
- // CabBooking system
- class CabBooking {
- private List<User> users = new ArrayList<>();
- private List<Driver> drivers = new ArrayList<>();
- private RideService rideService = new RideService();
- public void addUser(User user) {
- users.add(user);
- }
- public void addDriver(Driver driver) {
- drivers.add(driver);
- }
- public List<Driver> findNearbyDrivers(Location userLocation) {
- List<Driver> nearbyDrivers = new ArrayList<>();
- for (Driver driver : drivers) {
- if (driver.isAvailable() && driver.location.distanceTo(userLocation) <= 5) {
- nearbyDrivers.add(driver);
- }
- }
- return nearbyDrivers;
- }
- public Ride bookRide(User user, Driver driver, Location source, Location destination) {
- return rideService.bookRide(user, driver, source, destination);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment