-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRideSharingService.java
More file actions
71 lines (60 loc) · 2.52 KB
/
RideSharingService.java
File metadata and controls
71 lines (60 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.example.LLD.RideSharingApplication;
import org.example.LLD.RideSharingApplication.entities.*;
import org.example.LLD.RideSharingApplication.enums.Feature;
import org.example.LLD.RideSharingApplication.enums.Gender;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class RideSharingService {
int totalRides;
List<Ride> avilableRidesList;
List<User> users;
int totalUsers;
public RideSharingService() {
this.totalRides = 0;
this.totalUsers = 0;
this.avilableRidesList = new LinkedList<>();
this.users = new LinkedList<>();
}
public void offerRide(User offeredBy, String source, String destination, Vehicle vehicle, int availableSeats) throws Exception {
boolean ridesAvailable = checkIfActiveAvailableRides(offeredBy);
if (ridesAvailable) {
throw new Exception("You already offered a ride");
}
Ride ride = new Ride(source, destination, offeredBy, availableSeats, vehicle);
ride.setRideId(totalRides+1);
avilableRidesList.add(ride);
totalRides ++;
}
public User addUserToSystem(String name, Gender gender, int age, List<Vehicle> vehicles) {
User user = null;
if (vehicles == null || vehicles.isEmpty()) {
user = new User(totalUsers+1, name, gender, age);
} else {
user = new RideOfferUsers(totalUsers+1, name, gender, age, vehicles);
}
users.add(user);
this.totalUsers ++;
return user;
}
private boolean checkIfActiveAvailableRides(User offeredBy) {
Optional<Ride> ride = avilableRidesList.stream().filter(ride1 -> ride1.getRideOfferedBy().equals(offeredBy)).findAny();
return ride.isPresent();
}
public List<Ride> searchRide(RideRequest request1) {
return this.avilableRidesList.stream().filter(ride ->
ride.getOrigin().equals(request1.getSource())
&& ride.getDestination().equals(request1.getDestination())
&& (request1.getFeature() != null ? getFeatureMatch(request1, ride) : true)
&& ride.getAvailableSeats() == request1.getSeatsRequest()
).collect(Collectors.toList());
}
private boolean getFeatureMatch(RideRequest rideRequest, Ride ride) {
switch (rideRequest.getFeature()) {
case SPACIOUS:
return rideRequest.getSeatsRequest() < ride.getAvailableSeats();
}
return false;
}
}