Ride Sharing app
unknown
java
a year ago
5.3 kB
10
Indexable
// Enum to represent the status of a ride
public enum RideStatus {
BOOKED,
IN_PROGRESS,
COMPLETED,
CANCELLED
}
// Interface for User
public interface User {
String getName();
String getContactInfo();
void setName(String name);
void setContactInfo(String contactInfo);
}
// Interface for Ride
public interface Ride {
void bookRide(User user, String pickupLocation, String dropLocation);
void startRide();
void endRide();
void cancelRide();
double calculateFare();
RideStatus getStatus(); // Get the current status of the ride
}
// Interface for PaymentProcessor
public interface PaymentProcessor {
boolean processPayment(User user, double amount);
boolean processRefund(User user, double amount); // Handle refunds
}
// Interface for Rating
public interface Rateable {
void rateRide(int rating);
}
// Implementation of User for a Rider
public class Rider implements User {
private String name;
private String contactInfo;
public Rider(String name, String contactInfo) {
this.name = name;
this.contactInfo = contactInfo;
}
@Override
public String getName() {
return name;
}
@Override
public String getContactInfo() {
return contactInfo;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setContactInfo(String contactInfo) {
this.contactInfo = contactInfo;
}
}
// Implementation of Ride for a CarRide
public class CarRide implements Ride, Rateable {
private User user;
private String pickupLocation;
private String dropLocation;
private double fare;
private int rating;
private RideStatus status;
@Override
public void bookRide(User user, String pickupLocation, String dropLocation) {
this.user = user;
this.pickupLocation = pickupLocation;
this.dropLocation = dropLocation;
this.status = RideStatus.BOOKED;
System.out.println("Ride booked from " + pickupLocation + " to " + dropLocation);
}
@Override
public void startRide() {
if (status != RideStatus.BOOKED) {
System.out.println("Cannot start ride. Current status: " + status);
return;
}
this.status = RideStatus.IN_PROGRESS;
System.out.println("Ride started.");
}
@Override
public void endRide() {
if (status != RideStatus.IN_PROGRESS) {
System.out.println("Cannot end ride. Current status: " + status);
return;
}
this.status = RideStatus.COMPLETED;
System.out.println("Ride ended.");
}
@Override
public void cancelRide() {
if (status == RideStatus.IN_PROGRESS || status == RideStatus.COMPLETED) {
System.out.println("Cannot cancel ride. Current status: " + status);
return;
}
this.status = RideStatus.CANCELLED;
System.out.println("Ride has been cancelled.");
}
@Override
public double calculateFare() {
if (status == RideStatus.CANCELLED) {
fare = 0.0;
System.out.println("No fare for cancelled ride.");
} else if (status == RideStatus.COMPLETED) {
fare = 20.0; // Simplified fare calculation
}
return fare;
}
@Override
public RideStatus getStatus() {
return status;
}
@Override
public void rateRide(int rating) {
if (status != RideStatus.COMPLETED) {
System.out.println("Can only rate completed rides.");
return;
}
this.rating = rating;
System.out.println("Ride rated with " + rating + " stars.");
}
}
// Implementation of PaymentProcessor for Credit Card Payments
public class CreditCardPaymentProcessor implements PaymentProcessor {
@Override
public boolean processPayment(User user, double amount) {
// Simplified payment processing
System.out.println("Processing payment of $" + amount + " for " + user.getName());
return true;
}
@Override
public boolean processRefund(User user, double amount) {
// Simplified refund processing
System.out.println("Processing refund of $" + amount + " for " + user.getName());
return true;
}
}
// Main Application to demonstrate the ride-sharing service
public class RideSharingApp {
public static void main(String[] args) {
User rider = new Rider("John Doe", "john@example.com");
Ride ride = new CarRide();
PaymentProcessor paymentProcessor = new CreditCardPaymentProcessor();
// Book a ride
ride.bookRide(rider, "123 Main St", "456 Elm St");
// Try to cancel the ride
ride.cancelRide();
// If the ride was not cancelled, start and end the ride
if (ride.getStatus() != RideStatus.CANCELLED) {
ride.startRide();
ride.endRide();
double fare = ride.calculateFare();
if (fare > 0) {
paymentProcessor.processPayment(rider, fare);
}
}
// If the ride was completed, rate it
if (ride.getStatus() == RideStatus.COMPLETED) {
ride.rateRide(5);
}
}
}
Editor is loading...
Leave a Comment