-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (144 loc) · 4.79 KB
/
app.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class taxi{
constructor()
{
this.location = 'A';
this.amount = 0;
this.custId=[];
this.time=[];
}
}
class customer {
constructor(pickUpPoint, dropPoint, pickUpTime, dropTime, fare) {
this.pickUpPoint = pickUpPoint;
this.dropPoint = dropPoint;
this.pickUpTime = pickUpTime;
this.dropTime = dropTime;
this.fare = fare;
}
}
var taxiCount=3, customerId=0, taxis=[], customers=[], table;
var book = document.getElementById('bookTaxi');
book.addEventListener('click', bookTaxi, false);
var taxiState = document.getElementById('taxiStatus');
taxiState.addEventListener('click', taxiStatusUtility, false);
var object = document.getElementById('taxiAlloted');
var display = document.getElementById('display');
document.getElementById('pickUpPoint').addEventListener('focus', function(){
display.textContent='';
object.textContent='';
}, false);
window.onload = function() {
pickUpPoint.focus();
var today = new Date();
var hrs = today.getHours();
var greeting;
if(hrs > 18)
greeting = 'Good Evening';
else if(hrs > 12)
greeting = 'Good Afternoon';
else if (hrs > 0)
greeting = 'Good Morning';
else
greeting = 'Welcome';
document.getElementById('greeting').innerHTML=`${greeting}! Book a Taxi ♥`;
for(var taxiNo=0; taxiNo<taxiCount; taxiNo++)
{
taxis.push(new taxi());
for(var hr=0; hr<24; hr++)
taxis[taxiNo].time.push(0);
}
};
function bookTaxi()
{
display.innerHTML='';
var pickUpPoint, dropPoint, pickUpTime, dropTime, distance, bookedTaxi=-1, optimalDis = Number.MAX_VALUE, time, fare;
pickUpPoint = document.getElementById('pickUpPoint').value;
dropPoint = document.getElementById('dropPoint').value;
pickUpTime = document.getElementById('pickUpTime').value;
if(pickUpPoint=='' || dropPoint=='' || pickUpTime=='' || pickUpPoint==dropPoint)
{
object.textContent='Invalid input !';
return;
}
time = Math.abs(dropPoint.charCodeAt(0) - pickUpPoint.charCodeAt(0));
distance = time*15;
fare = (distance-5)*10 + 100;
dropTime = parseInt(pickUpTime) + time;
customers.push(new customer(pickUpPoint, dropPoint, pickUpTime, dropTime, fare));
if(customerId==0)
bookedTaxi=0;
else
{
for(var taxiNo=0; taxiNo<taxiCount; taxiNo++)
{
if(taxis[taxiNo].time[pickUpTime] == 0)
{
var temp = Math.abs(taxis[taxiNo].location.charCodeAt(0)-pickUpPoint.charCodeAt(0));
if(temp < optimalDis)
{
optimalDis = temp;
bookedTaxi = taxiNo;
}
else if(temp == optimalDis)
{
if(taxis[bookedTaxi].amount > taxis[taxiNo].amount)
bookedTaxi = taxiNo;
}
}
}
}
if(bookedTaxi != -1)
{
for(var hr=pickUpTime; hr<dropTime; hr++)
taxis[bookedTaxi].time[hr]=1;
taxis[bookedTaxi].amount+=fare;
taxis[bookedTaxi].location=dropPoint;
taxis[bookedTaxi].custId.push(customerId);
object.textContent = `Taxi - ${bookedTaxi+1} is alloted. Taxi fare - Rs.${fare}. Happy ride!`;
}
else
object.textContent = 'We are sorry! All taxis are booked.';
customerId++;
clear();
}
function clear()
{
document.getElementById('pickUpPoint').value='';
document.getElementById('dropPoint').value='';
document.getElementById('pickUpTime').value='';
}
function taxiStatusUtility()
{
clear();
object.textContent='';
table='';
for(var taxiNo=0; taxiNo<taxiCount; taxiNo++)
taxiStatus(taxiNo);
display.innerHTML = table;
}
function taxiStatus(taxiNo)
{
table+=`<h1> Taxi - ${taxiNo*1 + 1} </h1> <br>
<table class='center'>
<tr>
<th> CustomerId </th>
<th> Pickup point </th>
<th> Drop point </th>
<th> Pickup Time </th>
<th> Drop Time </th>
<th> Amount </th>
</tr>`;
for(var cust=0; cust<taxis[taxiNo].custId.length; cust++)
{
var index=taxis[taxiNo].custId[cust];
table+=`<tr>
<td>${index*1 + 1}</td>
<td> ${customers[index].pickUpPoint} </td>
<td> ${customers[index].dropPoint} </td>
<td> ${customers[index].pickUpTime} </td>
<td> ${customers[index].dropTime} </td>
<td> ${customers[index].fare} </td>
</tr>`;
}
table+=`</table> <br> Earnings - Rs.${taxis[taxiNo].amount} <br> <br> <br>`;
}