-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxiBooking.java
More file actions
91 lines (75 loc) · 3.15 KB
/
TaxiBooking.java
File metadata and controls
91 lines (75 loc) · 3.15 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
public class TaxiBooking {
static ArrayList<Taxi> taxis = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
static int customerIdCounter = 1;
public static void main(String[] args) {
System.out.println("Enter the number of Taxis:");
int numberOfTaxis = sc.nextInt();
initializeTaxis(numberOfTaxis);
while (true) {
System.out.println("\nChoose the Menu:\n1. Book Taxi\n2. Display Details\n3. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
bookTaxi();
break;
case 2:
displayDetails();
break;
case 3:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid Input");
}
}
}
public static void initializeTaxis(int number) {
for (int i = 1; i <= number; i++) {
taxis.add(new Taxi(i));
}
}
public static void bookTaxi() {
int customerId = customerIdCounter++;
System.out.print("Enter Pickup Point (A-F): ");
char pickup = sc.next().toUpperCase().charAt(0);
System.out.print("Enter Drop Point (A-F): ");
char drop = sc.next().toUpperCase().charAt(0);
System.out.print("Enter Pickup Time (1-12): ");
int pickupTime = sc.nextInt();
Taxi selectedTaxi = null;
int minDistance = Integer.MAX_VALUE;
for (Taxi t : taxis) {
if (t.isAvailable(pickupTime)) {
int distance = Math.abs(t.Current_point - pickup);
if (selectedTaxi == null || distance < minDistance ||
(distance == minDistance && t.total_Earning < selectedTaxi.total_Earning)) {
selectedTaxi = t;
minDistance = distance;
}
}
}
if (selectedTaxi == null) {
System.out.println("No Taxi Available");
return;
}
int dropTime = pickupTime + Math.abs(drop - pickup);
int amount = selectedTaxi.calculateEarning(pickup, drop);
int bookingId = selectedTaxi.bookings.size() + 1;
Booking booking = new Booking(bookingId, customerId, pickupTime, dropTime, pickup, drop, amount);
selectedTaxi.addBooking(booking);
System.out.println("Taxi " + selectedTaxi.id + " is Allocated.");
}
public static void displayDetails() {
for (Taxi t : taxis) {
System.out.println("Taxi ID: " + t.id + " | Total Earning: ₹" + t.total_Earning);
for (Booking b : t.bookings) {
System.out.println(" BookingID: " + b.bookingId + ", CustomerID: " + b.customerId +
", From: " + b.from + ", To: " + b.to +
", Pickup: " + b.pickupTime + ", Drop: " + b.DropTime + ", Amount: ₹" + b.amount);
}
}
}
}