Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- // Base class for a Person
- abstract class Person {
- protected String name;
- protected String contact;
- public Person(String name, String contact) {
- this.name = name;
- this.contact = contact;
- }
- public String getName() {
- return name;
- }
- public void setContact(String contact) {
- this.contact = contact;
- }
- }
- // User class
- class User extends Person {
- private int x, y;
- public User(String name, String contact, int x, int y) {
- super(name, contact);
- this.x = x;
- this.y = y;
- }
- public void updateLocation(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public int[] getLocation() {
- return new int[]{x, y};
- }
- }
- // Driver class
- class Driver extends Person {
- private String vehicleDetails;
- private int x, y;
- private boolean available;
- public Driver(String name, String contact, String vehicleDetails, int x, int y) {
- super(name, contact);
- this.vehicleDetails = vehicleDetails;
- this.x = x;
- this.y = y;
- this.available = true;
- }
- public void updateLocation(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public void changeStatus(boolean available) {
- this.available = available;
- }
- public int[] getLocation() {
- return new int[]{x, y};
- }
- public boolean isAvailable() {
- return available;
- }
- }
- // Cab Booking System
- class CabBooking {
- private List<User> users = new ArrayList<>();
- private List<Driver> drivers = new ArrayList<>();
- public void addUser(String name, String contact, int x, int y) {
- users.add(new User(name, contact, x, y));
- }
- public void addDriver(String name, String contact, String vehicleDetails, int x, int y) {
- drivers.add(new Driver(name, contact, vehicleDetails, x, y));
- }
- public List<Driver> findRides(String userName, int destX, int destY) {
- User user = users.stream().filter(u -> u.getName().equals(userName)).findFirst().orElse(null);
- if (user == null) return Collections.emptyList();
- int[] userLocation = user.getLocation();
- List<Driver> availableDrivers = new ArrayList<>();
- for (Driver driver : drivers) {
- if (driver.isAvailable()) {
- int[] driverLocation = driver.getLocation();
- double distance = Math.sqrt(Math.pow(driverLocation[0] - userLocation[0], 2) + Math.pow(driverLocation[1] - userLocation[1], 2));
- if (distance <= 5) {
- availableDrivers.add(driver);
- }
- }
- }
- return availableDrivers;
- }
- public void calculateBill(String userName, int destX, int destY) {
- User user = users.stream().filter(u -> u.getName().equals(userName)).findFirst().orElse(null);
- if (user == null) return;
- int[] userLocation = user.getLocation();
- double distance = Math.sqrt(Math.pow(destX - userLocation[0], 2) + Math.pow(destY - userLocation[1], 2));
- double fare = distance * 10;
- System.out.println("Bill for " + userName + ": $" + fare);
- }
- }
- // Main class to test the system
- public class Main {
- public static void main(String[] args) {
- CabBooking cabBooking = new CabBooking();
- cabBooking.addUser("Alice", "12345", 0, 0);
- cabBooking.addDriver("Bob", "67890", "Sedan", 2, 3);
- cabBooking.addDriver("Charlie", "54321", "SUV", 7, 8);
- List<Driver> availableDrivers = cabBooking.findRides("Alice", 5, 5);
- System.out.println("Available Drivers:");
- for (Driver driver : availableDrivers) {
- System.out.println(driver.getName());
- }
- cabBooking.calculateBill("Alice", 5, 5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement