Advertisement
asdfg0998

Untitled

Mar 19th, 2025
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class User {
  4.     String username;
  5.     String contactDetails;
  6.     int x, y; // Location coordinates
  7.  
  8.     public User(String username, String contactDetails, int x, int y) {
  9.         this.username = username;
  10.         this.contactDetails = contactDetails;
  11.         this.x = x;
  12.         this.y = y;
  13.     }
  14.  
  15.     public void updateDetails(String contactDetails) {
  16.         this.contactDetails = contactDetails;
  17.     }
  18.  
  19.     public void updateLocation(int x, int y) {
  20.         this.x = x;
  21.         this.y = y;
  22.     }
  23. }
  24.  
  25. class Driver {
  26.     String name;
  27.     String vehicleDetails;
  28.     int x, y; // Location coordinates
  29.     boolean available;
  30.     double earnings;
  31.  
  32.     public Driver(String name, String vehicleDetails, int x, int y) {
  33.         this.name = name;
  34.         this.vehicleDetails = vehicleDetails;
  35.         this.x = x;
  36.         this.y = y;
  37.         this.available = true;
  38.         this.earnings = 0;
  39.     }
  40.  
  41.     public void updateLocation(int x, int y) {
  42.         this.x = x;
  43.         this.y = y;
  44.     }
  45.  
  46.     public void changeStatus(boolean available) {
  47.         this.available = available;
  48.     }
  49. }
  50.  
  51. class CabBookingApp {
  52.     List<User> users = new ArrayList<>();
  53.     List<Driver> drivers = new ArrayList<>();
  54.  
  55.     public void addUser(String username, String contactDetails, int x, int y) {
  56.         users.add(new User(username, contactDetails, x, y));
  57.     }
  58.  
  59.     public void addDriver(String name, String vehicleDetails, int x, int y) {
  60.         drivers.add(new Driver(name, vehicleDetails, x, y));
  61.     }
  62.  
  63.     public List<Driver> findRide(String username, int destX, int destY) {
  64.         List<Driver> availableDrivers = new ArrayList<>();
  65.         for (Driver driver : drivers) {
  66.             if (driver.available && getDistance(driver.x, driver.y, destX, destY) <= 5) {
  67.                 availableDrivers.add(driver);
  68.             }
  69.         }
  70.         return availableDrivers;
  71.     }
  72.  
  73.     public void chooseRide(String username, String driverName, int destX, int destY) {
  74.         for (Driver driver : drivers) {
  75.             if (driver.name.equals(driverName) && driver.available) {
  76.                 driver.available = false;
  77.                 double fare = calculateBill(driver.x, driver.y, destX, destY);
  78.                 driver.earnings += fare;
  79.                 System.out.println("Ride booked with " + driverName + ". Fare: " + fare);
  80.                 return;
  81.             }
  82.         }
  83.         System.out.println("Driver not available!");
  84.     }
  85.  
  86.     private double calculateBill(int startX, int startY, int destX, int destY) {
  87.         return getDistance(startX, startY, destX, destY) * 10; // Assume 10 currency units per distance unit
  88.     }
  89.  
  90.     public double findTotalEarnings() {
  91.         return drivers.stream().mapToDouble(d -> d.earnings).sum();
  92.     }
  93.  
  94.     private double getDistance(int x1, int y1, int x2, int y2) {
  95.         return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  96.     }
  97. }
  98.  
  99. public class Main {
  100.     public static void main(String[] args) {
  101.         CabBookingApp app = new CabBookingApp();
  102.         app.addUser("Alice", "12345", 0, 0);
  103.         app.addDriver("Bob", "Toyota", 2, 3);
  104.         app.addDriver("Charlie", "Honda", 5, 5);
  105.  
  106.         List<Driver> rides = app.findRide("Alice", 5, 5);
  107.         System.out.println("Available Drivers: " + rides.size());
  108.         if (!rides.isEmpty()) {
  109.             app.chooseRide("Alice", rides.get(0).name, 5, 5);
  110.         }
  111.  
  112.         System.out.println("Total Earnings: " + app.findTotalEarnings());
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement