asdfg0998

Untitled

Mar 20th, 2025
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. // Base class for Users
  4. abstract class Person {
  5.     protected String name;
  6.     protected String contact;
  7.     protected Location location;
  8.  
  9.     public Person(String name, String contact, Location location) {
  10.         this.name = name;
  11.         this.contact = contact;
  12.         this.location = location;
  13.     }
  14.  
  15.     public void updateLocation(Location location) {
  16.         this.location = location;
  17.     }
  18. }
  19.  
  20. // Location class
  21. class Location {
  22.     double x, y;
  23.    
  24.     public Location(double x, double y) {
  25.         this.x = x;
  26.         this.y = y;
  27.     }
  28.  
  29.     public double distanceTo(Location other) {
  30.         return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
  31.     }
  32. }
  33.  
  34. // User class
  35. class User extends Person {
  36.     public User(String name, String contact, Location location) {
  37.         super(name, contact, location);
  38.     }
  39. }
  40.  
  41. // Driver class
  42. class Driver extends Person {
  43.     private String vehicleDetails;
  44.     private boolean available;
  45.     private double earnings;
  46.  
  47.     public Driver(String name, String contact, Location location, String vehicleDetails) {
  48.         super(name, contact, location);
  49.         this.vehicleDetails = vehicleDetails;
  50.         this.available = true;
  51.         this.earnings = 0;
  52.     }
  53.  
  54.     public boolean isAvailable() {
  55.         return available;
  56.     }
  57.  
  58.     public void setAvailability(boolean status) {
  59.         this.available = status;
  60.     }
  61.  
  62.     public void addEarnings(double fare) {
  63.         this.earnings += fare;
  64.     }
  65.    
  66.     public double getEarnings() {
  67.         return earnings;
  68.     }
  69. }
  70.  
  71. // Ride class
  72. class Ride {
  73.     private User user;
  74.     private Driver driver;
  75.     private Location source;
  76.     private Location destination;
  77.     private double fare;
  78.  
  79.     public Ride(User user, Driver driver, Location source, Location destination, double fare) {
  80.         this.user = user;
  81.         this.driver = driver;
  82.         this.source = source;
  83.         this.destination = destination;
  84.         this.fare = fare;
  85.     }
  86. }
  87.  
  88. // RideService to manage rides
  89. class RideService {
  90.     private List<Ride> rideHistory = new ArrayList<>();
  91.  
  92.     public Ride bookRide(User user, Driver driver, Location source, Location destination) {
  93.         double distance = source.distanceTo(destination);
  94.         double fare = distance * 10; // Example fare calculation
  95.         driver.addEarnings(fare);
  96.         Ride ride = new Ride(user, driver, source, destination, fare);
  97.         rideHistory.add(ride);
  98.         driver.setAvailability(false);
  99.         return ride;
  100.     }
  101. }
  102.  
  103. // CabBooking system
  104. class CabBooking {
  105.     private List<User> users = new ArrayList<>();
  106.     private List<Driver> drivers = new ArrayList<>();
  107.     private RideService rideService = new RideService();
  108.  
  109.     public void addUser(User user) {
  110.         users.add(user);
  111.     }
  112.  
  113.     public void addDriver(Driver driver) {
  114.         drivers.add(driver);
  115.     }
  116.  
  117.     public List<Driver> findNearbyDrivers(Location userLocation) {
  118.         List<Driver> nearbyDrivers = new ArrayList<>();
  119.         for (Driver driver : drivers) {
  120.             if (driver.isAvailable() && driver.location.distanceTo(userLocation) <= 5) {
  121.                 nearbyDrivers.add(driver);
  122.             }
  123.         }
  124.         return nearbyDrivers;
  125.     }
  126.  
  127.     public Ride bookRide(User user, Driver driver, Location source, Location destination) {
  128.         return rideService.bookRide(user, driver, source, destination);
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment