1- /*
2- *===================================================================================================
1+ /**
2+ * ===================================================================================================
33 *
4- * No. of taxies (Assume 4)
5- * 6 Points A B C D E F -> Each have 15Km between 2
6- * one point to another it takes 60 mins
7- * first 5km -> Rs.100 , then 10 for every Km
8- * Time: Hrs (for Simplicity)
9- * All taxies are initiated at A
10- * When customer book taxi, free taxi will be allocated
11- * two free taxies are same point -> one with lower earning will be allocated
12- * Taxi charges only from customer pickup point to drop point
13- * if no taxi available then booking rejected
14- *________________________________________________
4+ * <p>No. of taxis (Assume 4) 6 Points A B C D E F -> Each have 15Km between 2 one point to another
5+ * it takes 60 min first 5km -> Rs.100 , then 10 for every Km Time: Hrs (for Simplicity) All taxis
6+ * are initiated at A When customer book taxi, free taxi will be allocated two free taxis are same
7+ * point -> one with lower earning will be allocated Taxi charges only from customer pickup point to
8+ * drop point if no taxi available then booking rejected
9+ * ________________________________________________
1510 *
16- * Taxi :
17- * TaxiNumber :
18- * CurrentLocation :
19- * IsBooked
20- * TotalEarnings :
21- * FreeTime :
22- *________________________________________________
11+ * <p>Taxi : TaxiNumber : CurrentLocation : IsBooked TotalEarnings : FreeTime :
12+ * ________________________________________________
2313 *
24- * BookingProcess:
25- * DistanceBetweenTaxiAndCustomer -> ToFindNearestTaxi
26- * DistanceBetweenPickupAndDrop -> ToCalculateFee
27- * NextFreeTime -> ForNextBooking
28- * NextPickupPoint -> ToAssignNextPickUpPoint
29- *________________________________________________
30- * (A-0)-----------(B-0)
31- * 1---------------2
32- * Check Taxi availability time and taxi must reach before their pickup time
14+ * <p>BookingProcess: DistanceBetweenTaxiAndCustomer -> ToFindNearestTaxi
15+ * DistanceBetweenPickupAndDrop -> ToCalculateFee NextFreeTime -> ForNextBooking NextPickupPoint ->
16+ * ToAssignNextPickUpPoint ________________________________________________ (A-0)-----------(B-0)
17+ * 1---------------2 Check Taxi availability time and taxi must reach before their pickup time
3318 *
34- *===================================================================================================
19+ * <p> ===================================================================================================
3520 */
36-
37- import java .util .ArrayList ;
21+ import java .util .Arrays ;
3822import java .util .Comparator ;
3923import java .util .List ;
4024import java .util .Scanner ;
25+ import java .util .stream .Collectors ;
4126
4227public class TaxiBookingApplication {
4328 static int customerId = 1 ;
@@ -50,8 +35,9 @@ public static void main(String[] args) {
5035
5136 Scanner input = new Scanner (System .in );
5237 boolean userWantsToContinue = true ;
53- // Create No of Taxies
54- List <Taxi > allTaxies = createTaxiList ();
38+ // Create No of Taxis
39+ List <Taxi > allTaxis =
40+ Arrays .stream (new int [4 ]).mapToObj (i -> new Taxi ()).collect (Collectors .toList ());
5541
5642 while (userWantsToContinue ) {
5743 System .out .println (
@@ -65,15 +51,15 @@ public static void main(String[] args) {
6551 char dropLocation = input .next ().toUpperCase ().charAt (0 );
6652 System .out .println ("\n PickUp Time = " );
6753 int pickUpTime = input .nextInt ();
68- List <Taxi > FreeTaxies = getAllFreeTaxies (pickUpTime , pickUpLocation , allTaxies );
69- if (FreeTaxies .size () == 0 ) {
54+ List <Taxi > FreeTaxis = getAllFreeTaxis (pickUpTime , pickUpLocation , allTaxis );
55+ if (FreeTaxis .size () == 0 ) {
7056 System .out .println ("Booking Not Available!" );
7157 break ;
7258 }
73- BookTaxi .BookNewTaxi (customerId ++, pickUpLocation , dropLocation , pickUpTime , FreeTaxies );
59+ BookTaxi .BookNewTaxi (customerId ++, pickUpLocation , dropLocation , pickUpTime , FreeTaxis );
7460 break ;
7561 case 2 :
76- for (Taxi taxi : allTaxies ) {
62+ for (Taxi taxi : allTaxis ) {
7763 BookTaxi .ShowTaxiDetails (taxi );
7864 }
7965 break ;
@@ -86,25 +72,15 @@ public static void main(String[] args) {
8672 }
8773 }
8874
89- private static List <Taxi > getAllFreeTaxies (
90- int PickUpTime , char PickUpLocation , List <Taxi > allTaxies ) {
91- List <Taxi > availableFreeTaxies = new ArrayList <>();
92- for (Taxi currentTaxi : allTaxies ) {
93- if (currentTaxi .FreeTime <= PickUpTime
94- && Math .abs ((currentTaxi .CurrentLocation - '0' ) - (PickUpLocation - '0' ))
95- <= PickUpTime - currentTaxi .FreeTime ) {
96- availableFreeTaxies .add (currentTaxi );
97- }
98- }
99- availableFreeTaxies .sort (Comparator .comparingInt (a -> a .TotalEarnings ));
100- return availableFreeTaxies ;
101- }
102-
103- private static List <Taxi > createTaxiList () {
104- List <Taxi > createdTaxiList = new ArrayList <>();
105- for (int i = 0 ; i < 4 ; i ++) {
106- createdTaxiList .add (new Taxi ());
107- }
108- return createdTaxiList ;
75+ private static List <Taxi > getAllFreeTaxis (
76+ int PickUpTime , char PickUpLocation , List <Taxi > allTaxis ) {
77+ return allTaxis .stream ()
78+ .filter (
79+ taxi ->
80+ taxi .FreeTime <= PickUpTime
81+ && (Math .abs (taxi .CurrentLocation - '0' ) - (PickUpLocation - '0' )
82+ <= PickUpTime - taxi .FreeTime ))
83+ .sorted (Comparator .comparing (taxi -> taxi .TotalEarnings ))
84+ .collect (Collectors .toList ());
10985 }
11086}
0 commit comments