Advertisement
asdfg0998

Untitled

Mar 20th, 2025
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. // Base class for a Person
  4. abstract class Person {
  5.     protected String name;
  6.     protected String contact;
  7.  
  8.     public Person(String name, String contact) {
  9.         this.name = name;
  10.         this.contact = contact;
  11.     }
  12.  
  13.     public String getName() {
  14.         return name;
  15.     }
  16.  
  17.     public void setContact(String contact) {
  18.         this.contact = contact;
  19.     }
  20. }
  21.  
  22. // User class
  23. class User extends Person {
  24.     private int x, y;
  25.  
  26.     public User(String name, String contact, int x, int y) {
  27.         super(name, contact);
  28.         this.x = x;
  29.         this.y = y;
  30.     }
  31.  
  32.     public void updateLocation(int x, int y) {
  33.         this.x = x;
  34.         this.y = y;
  35.     }
  36.    
  37.     public int[] getLocation() {
  38.         return new int[]{x, y};
  39.     }
  40. }
  41.  
  42. // Driver class
  43. class Driver extends Person {
  44.     private String vehicleDetails;
  45.     private int x, y;
  46.     private boolean available;
  47.  
  48.     public Driver(String name, String contact, String vehicleDetails, int x, int y) {
  49.         super(name, contact);
  50.         this.vehicleDetails = vehicleDetails;
  51.         this.x = x;
  52.         this.y = y;
  53.         this.available = true;
  54.     }
  55.  
  56.     public void updateLocation(int x, int y) {
  57.         this.x = x;
  58.         this.y = y;
  59.     }
  60.  
  61.     public void changeStatus(boolean available) {
  62.         this.available = available;
  63.     }
  64.  
  65.     public int[] getLocation() {
  66.         return new int[]{x, y};
  67.     }
  68.  
  69.     public boolean isAvailable() {
  70.         return available;
  71.     }
  72. }
  73.  
  74. // Cab Booking System
  75. class CabBooking {
  76.     private List<User> users = new ArrayList<>();
  77.     private List<Driver> drivers = new ArrayList<>();
  78.  
  79.     public void addUser(String name, String contact, int x, int y) {
  80.         users.add(new User(name, contact, x, y));
  81.     }
  82.  
  83.     public void addDriver(String name, String contact, String vehicleDetails, int x, int y) {
  84.         drivers.add(new Driver(name, contact, vehicleDetails, x, y));
  85.     }
  86.  
  87.     public List<Driver> findRides(String userName, int destX, int destY) {
  88.         User user = users.stream().filter(u -> u.getName().equals(userName)).findFirst().orElse(null);
  89.         if (user == null) return Collections.emptyList();
  90.        
  91.         int[] userLocation = user.getLocation();
  92.         List<Driver> availableDrivers = new ArrayList<>();
  93.        
  94.         for (Driver driver : drivers) {
  95.             if (driver.isAvailable()) {
  96.                 int[] driverLocation = driver.getLocation();
  97.                 double distance = Math.sqrt(Math.pow(driverLocation[0] - userLocation[0], 2) + Math.pow(driverLocation[1] - userLocation[1], 2));
  98.                 if (distance <= 5) {
  99.                     availableDrivers.add(driver);
  100.                 }
  101.             }
  102.         }
  103.         return availableDrivers;
  104.     }
  105.  
  106.     public void calculateBill(String userName, int destX, int destY) {
  107.         User user = users.stream().filter(u -> u.getName().equals(userName)).findFirst().orElse(null);
  108.         if (user == null) return;
  109.        
  110.         int[] userLocation = user.getLocation();
  111.         double distance = Math.sqrt(Math.pow(destX - userLocation[0], 2) + Math.pow(destY - userLocation[1], 2));
  112.         double fare = distance * 10;
  113.         System.out.println("Bill for " + userName + ": $" + fare);
  114.     }
  115. }
  116.  
  117. // Main class to test the system
  118. public class Main {
  119.     public static void main(String[] args) {
  120.         CabBooking cabBooking = new CabBooking();
  121.         cabBooking.addUser("Alice", "12345", 0, 0);
  122.         cabBooking.addDriver("Bob", "67890", "Sedan", 2, 3);
  123.         cabBooking.addDriver("Charlie", "54321", "SUV", 7, 8);
  124.  
  125.         List<Driver> availableDrivers = cabBooking.findRides("Alice", 5, 5);
  126.         System.out.println("Available Drivers:");
  127.         for (Driver driver : availableDrivers) {
  128.             System.out.println(driver.getName());
  129.         }
  130.  
  131.         cabBooking.calculateBill("Alice", 5, 5);
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement