Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. object CoolTaxi {
  2.  
  3. class User(val firstName: String, val lastName: String, val custType: String)
  4.  
  5. class Location(val latitue: Double = 0.00, val longitude: Double = 0.00)
  6.  
  7. class Cab(val typ: String, val totalSeats: Int, val seatsAvlbl: Int,
  8. val currentLocation: Location, val finalDestination: Location) {
  9. def distanceFromHere(here: Location): Double = 0.00
  10.  
  11. def timeToDrop(here: Location): Long = 0
  12.  
  13. def isOnPromotion: Boolean = false
  14.  
  15. }
  16.  
  17. class Source() extends Location
  18.  
  19. class Destination() extends Location
  20.  
  21. //encapsulated a ride request
  22. class RideRequest(val source: Source, val destination: Destination, val service: String, val seats: Int)
  23.  
  24. // represents a filter function that take a collection of cabs, filters and returns the result
  25. type Filter = List[Cab] => List[Cab]
  26.  
  27. /* a simple filter to filter cabs based on source and destination, */
  28. def matchRoutes(source: Source, destination: Destination): Filter = {
  29. cabs: List[Cab] => cabs.filter(c => c.finalDestination == destination && isWithinRange(c.currentLocation, source))
  30. }
  31.  
  32. def isWithinRange(cabLoc: Location, myLoc: Location) = true
  33.  
  34. /* a simple filter to filter cabs based on service type - pool, premier, etc, */
  35. def matchClass(typName: String): Filter = {
  36. cabs: List[Cab] => cabs.filter(_.typ.equalsIgnoreCase(typName))
  37. }
  38.  
  39. /* a simple filter to filter cabs based on some kind of promotion */
  40. def matchPromotionCabs: Filter = {
  41. cabs: List[Cab] => cabs.filter(_.isOnPromotion)
  42. }
  43.  
  44. /* a long laborious process of matching the best cabs based on multitude of factors
  45. * such as driver ratings, user ratings, co-passenger ratings, traffic conditions, etc
  46. * */
  47. def matchSeats(numOfSeats: Int): Filter = {
  48. cabs: List[Cab] => cabs.filter(_.seatsAvlbl == numOfSeats)
  49. }
  50.  
  51. def matchDistanceToPickUp(here: Location, dis: Double): Filter = {
  52. cabs: List[Cab] => cabs.filter(_.distanceFromHere(here) == dis)
  53. }
  54.  
  55. def matchTimeToDrop(here: Location, time: Int): Filter = {
  56. cabs: List[Cab] => cabs.filter(_.timeToDrop(here) <= time)
  57. }
  58. }
  59.  
  60.  
  61. object CabService{
  62.  
  63. val MAX_DIST = 4.00
  64. val MAX_TIME_WAIT = 60
  65.  
  66. /* predfined recipe for implementing abstract notion of convinience; */
  67. def matchConvinience(location: Location) = matchDistanceToPickUp(location, MAX_DIST) andThen matchTimeToDrop(location, MAX_TIME_WAIT)
  68.  
  69. /* more coarse grained filters have been defined based on underlying ones; */
  70. def matchBasic(start:Source, end: Destination) = matchRoutes(start, end) andThen matchClass("basic")
  71. def matchPool(start:Source, end: Destination)(seats: Int) = matchRoutes(start, end) andThen matchClass("pool") andThen matchSeats(seats)
  72. def matchPremium(start:Source, end: Destination) = matchRoutes(start, end) andThen matchClass("premium")
  73.  
  74. /* more abtstract constructs defined by business needs however the client is not aware of
  75. how these constructs are composed to generate final outcomes */
  76. def matchMyDailyCommute(start:Source, end: Destination) = matchPool(start, end)(1) andThen matchConvinience(start)
  77. def matchOffersOnWheels(start:Source, end: Destination) = matchPool(start, end)(1) andThen matchPromotionCabs
  78.  
  79. }
  80.  
  81.  
  82. object SmartOccupancyManager {
  83.  
  84. import CabService.{matchMyDailyCommute, matchOffersOnWheels}
  85. import CoolTaxi.RideRequest
  86.  
  87. /* a non trivial process of fetching cabs from, say, in memory DB based on source and destination */
  88. val cabsFromDB = List[Cab]()
  89.  
  90. def getCabs(start:Source, end: Destination, user: User, request: RideRequest): List[Cab] = {
  91. (request.service, request.seats) match {
  92. case ("ShareMyCab", seats) => matchPool(start, end)(seats)(cabsFromDB)
  93. case ("OffersOnWheels", _) => matchOffersOnWheels(start, end)(cabsFromDB)
  94. case ("MyDailyCommute", _) => matchMyDailyCommute(start, end)(cabsFromDB)
  95. case _ => cabsFromDB
  96. }
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement