This article explores the object-oriented design and implementation of a Ride-Sharing Service similar to Uber using Java.
We focus on the various aspects of ride-sharing, including user and driver management, ride booking, and fare calculation.
The Ride-Sharing Service should:
- User and Driver Account Management: Handle the registration and profiles of users and drivers.
- Ride Booking: Enable users to book rides and drivers to accept them.
- Fare Calculation: Compute fares based on distance and other factors.
- Ride History Management: Keep a record of past rides for users and drivers.
- Registering and Managing User and Driver Accounts
- Booking and Managing Rides
- Calculating and Processing Fares
- Maintaining Ride History
RideSharingService
: Manages the overall system.User
: Represents a service user.Driver
: Represents a driver.Ride
: Manages ride details.FareCalculator
: Calculates ride fares.
Manages user account information.
public class User {
private String userId;
private String name;
private String phone;
public User(String userId, String name, String phone) {
this.userId = userId;
this.name = name;
this.phone = phone;
}
// Getters and setters...
}
Represents a driver in the system.
public class Driver {
private String driverId;
private String name;
private String vehicleDetails;
public Driver(String driverId, String name, String vehicleDetails) {
this.driverId = driverId;
this.name = name;
this.vehicleDetails = vehicleDetails;
}
// Getters and setters...
}
Manages ride details.
import java.util.Date;
public class Ride {
private String rideId;
private User user;
private Driver driver;
private Date rideDate;
private double fare;
public Ride(String rideId, User user, Driver driver, Date rideDate) {
this.rideId = rideId;
this.user = user;
this.driver = driver;
this.rideDate = rideDate;
}
public void setFare(double fare) {
FareCalculator fareCalculator = new FareCalculator();
this.fare = fareCalculator.calculateFare(distance, duration);
}
// Getters and setters...
}
Calculates the fare of a ride.
public class FareCalculator {
private static final double BASE_FARE = 2.50; // Base fare for a ride
private static final double COST_PER_MILE = 1.50; // Cost per mile
private static final double COST_PER_MINUTE = 0.25; // Cost per minute
public double calculateFare(double distanceInMiles, double durationInMinutes) {
double distanceCharge = distanceInMiles * COST_PER_MILE;
double timeCharge = durationInMinutes * COST_PER_MINUTE;
return BASE_FARE + distanceCharge + timeCharge;
}
}
Manages the ride-sharing service operations.
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
public class RideSharingService {
private List<User> users;
private List<Driver> drivers;
private List<Ride> rides;
public RideSharingService() {
this.users = new ArrayList<>();
this.drivers = new ArrayList<>();
this.rides = new ArrayList<>();
}
public Ride bookRide(User user, Driver driver, Date rideDate) {
Ride newRide = new Ride(generateRideId(), user, driver, rideDate);
FareCalculator fareCalculator = new FareCalculator();
double fare = fareCalculator.calculateFare(newRide);
newRide.setFare(fare);
rides.add(newRide);
return newRide;
}
private String generateRideId() {
return "RIDE_" + System.currentTimeMillis();
}