Skip to content

Commit c76eeb4

Browse files
committed
Taxi Booking application simplification
1 parent 73cf53a commit c76eeb4

File tree

3 files changed

+88
-112
lines changed

3 files changed

+88
-112
lines changed

TaxiBookingApp/src/BookTaxi.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,60 @@
22

33
public class BookTaxi {
44

5-
public static void BookNewTaxi(
6-
int customerId,
7-
char pickUpLocation,
8-
char dropLocation,
9-
int pickUpTime,
10-
List<Taxi> freeTaxies) {
11-
int min = 999;
12-
int DistanceBetweenTaxiAndCustomer;
13-
int DistanceBetweenPickupAndDrop;
14-
int dropTime = 0;
15-
int tripEarning = 0;
16-
String tripDetails = null;
17-
Taxi BookedTaxi = null;
5+
public static void BookNewTaxi(
6+
int customerId,
7+
char pickUpLocation,
8+
char dropLocation,
9+
int pickUpTime,
10+
List<Taxi> freeTaxies) {
11+
int min = 999;
12+
int DistanceBetweenTaxiAndCustomer;
13+
int DistanceBetweenPickupAndDrop;
14+
int dropTime = 0;
15+
int tripEarning = 0;
16+
String tripDetails = null;
17+
Taxi BookedTaxi = null;
1818

19-
for (Taxi currentTaxi : freeTaxies) {
20-
DistanceBetweenTaxiAndCustomer =
21-
Math.abs((currentTaxi.CurrentLocation - '0') - (pickUpLocation - '0')) * 15;
22-
DistanceBetweenPickupAndDrop = Math.abs((dropLocation - '0') - (pickUpLocation - '0')) * 15;
23-
if (DistanceBetweenTaxiAndCustomer < min) {
24-
BookedTaxi = currentTaxi;
25-
dropTime = pickUpTime + DistanceBetweenPickupAndDrop / 15;
26-
tripEarning = ((DistanceBetweenPickupAndDrop - 5) * 10 + 100);
27-
tripDetails =
28-
customerId
29-
+ " "
30-
+ customerId
31-
+ " "
32-
+ pickUpLocation
33-
+ " "
34-
+ dropLocation
35-
+ " "
36-
+ pickUpTime
37-
+ " "
38-
+ dropTime
39-
+ " "
40-
+ tripEarning;
41-
min = DistanceBetweenTaxiAndCustomer;
42-
}
43-
}
44-
assert BookedTaxi != null;
45-
BookedTaxi.BookingId = customerId;
46-
BookedTaxi.CurrentLocation = dropLocation;
47-
BookedTaxi.TotalEarnings += tripEarning;
48-
BookedTaxi.FreeTime = dropTime;
49-
BookedTaxi.TaxiDetails.add(tripDetails);
50-
System.out.println("Taxi -> " + BookedTaxi.TaxiNumber + " Booked !");
19+
for (Taxi currentTaxi : freeTaxies) {
20+
DistanceBetweenTaxiAndCustomer =
21+
Math.abs((currentTaxi.CurrentLocation - '0') - (pickUpLocation - '0')) * 15;
22+
DistanceBetweenPickupAndDrop = Math.abs((dropLocation - '0') - (pickUpLocation - '0')) * 15;
23+
if (DistanceBetweenTaxiAndCustomer < min) {
24+
BookedTaxi = currentTaxi;
25+
dropTime = pickUpTime + DistanceBetweenPickupAndDrop / 15;
26+
tripEarning = ((DistanceBetweenPickupAndDrop - 5) * 10 + 100);
27+
tripDetails =
28+
customerId
29+
+ " "
30+
+ customerId
31+
+ " "
32+
+ pickUpLocation
33+
+ " "
34+
+ dropLocation
35+
+ " "
36+
+ pickUpTime
37+
+ " "
38+
+ dropTime
39+
+ " "
40+
+ tripEarning;
41+
min = DistanceBetweenTaxiAndCustomer;
42+
}
5143
}
44+
assert BookedTaxi != null;
45+
BookedTaxi.BookingId = customerId;
46+
BookedTaxi.CurrentLocation = dropLocation;
47+
BookedTaxi.TotalEarnings += tripEarning;
48+
BookedTaxi.FreeTime = dropTime;
49+
BookedTaxi.TaxiDetails.add(tripDetails);
50+
System.out.println("Taxi -> " + BookedTaxi.TaxiNumber + " Booked !");
51+
}
5252

53-
public static void ShowTaxiDetails(Taxi taxi) {
54-
System.out.println(
55-
"BookingId CustomerId From To PickUpTime DropTime TripEarning\n");
56-
for (String detail : taxi.TaxiDetails) System.out.println(detail);
57-
System.out.println(
58-
"Taxi " + taxi.TaxiNumber + " : Earning -> Rs. " + taxi.TotalEarnings);
59-
System.out.println("==================================================================================");
60-
}
53+
public static void ShowTaxiDetails(Taxi taxi) {
54+
System.out.println(
55+
"BookingId CustomerId From To PickUpTime DropTime TripEarning\n");
56+
for (String detail : taxi.TaxiDetails) System.out.println(detail);
57+
System.out.println(taxi);
58+
System.out.println(
59+
"==================================================================================");
60+
}
6161
}

TaxiBookingApp/src/Taxi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public Taxi() {
2020
}
2121

2222
public String toString() {
23-
return "";
23+
return "Taxi " + this.TaxiNumber + " : Earning -> Rs. " + this.TotalEarnings;
2424
}
2525
}
Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,28 @@
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;
3822
import java.util.Comparator;
3923
import java.util.List;
4024
import java.util.Scanner;
25+
import java.util.stream.Collectors;
4126

4227
public 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("\nPickUp 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

Comments
 (0)