-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRide.cs
203 lines (181 loc) · 6.76 KB
/
Ride.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace MYRIDE
{
internal class Ride : Vehicle
{
Location start_location;
Location end_location;
int price;
Driver driver;
Passenger passenger;
float fuel_price = 272;
public Ride() : base()
{
start_location = new Location();
end_location = new Location();
driver = new Driver();
passenger = new Passenger();
}
///////////////////////////////////////Properties//////////////////////////////////////////
public Location StartLocation
{
get { return start_location; }
//set { start_location = value; }
}
public Location EndLocation
{
get { return end_location; }
//set { end_location = value; }
}
public int Price
{
get { return price; }
set { price = value; }
}
public Driver Driver
{
get { return driver; }
}
public Passenger Passenger
{
get { return passenger; }
}
public override string Type
{
get => base.Type;
set => base.Type = value;
}
public float FuelPrice
{
get { return fuel_price; }
}
//////////////////////////////////////////////////////////////////////////////////////////////
public void assignPassenger(Passenger passenger)
{ this.passenger = passenger; }
public int assignDriver(string sLoc,string rideType)
{
string connectionString = "Data Source=(localdb)\\ProjectModels;Initial Catalog=myRideDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False";
SqlConnection conn = new SqlConnection(connectionString);
string query = $"SELECT * FROM driver WHERE vType = '{rideType}' AND availability = 'Y'";
conn.Open();
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
int driverID = -1;
float distance = 0;
int counter = 0;
Location dLoc = new Location();
while(reader.Read())
{
Console.Write(reader["name"]);
dLoc.Latitude = (float)Convert.ToDouble(reader["curr_latitude"]);
dLoc.Longitude = (float)Convert.ToDouble(reader["curr_longitude"]);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(" curr_Location: " + dLoc.Latitude + "," + dLoc.Longitude + "\n");
Console.ResetColor();
if (counter == 0)
{
distance = calculateDistance(dLoc, sLoc);
driverID = Convert.ToInt32(reader["d_id"]);
}
else
{
float tempDistance = calculateDistance(dLoc, sLoc);
if(tempDistance < distance)
{
distance = tempDistance;
driverID = Convert.ToInt32(reader["d_id"]);
}
}
counter++;
}
conn.Close();
query = $"SELECT name FROM driver WHERE d_id = {driverID}";
conn.Open();
command = new SqlCommand(query, conn);
SqlDataReader r = command.ExecuteReader();
if (r.Read())
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Congratulations your driver " + r["name"] +" is on its way :)");
Console.ResetColor();
}
conn.Close();
return driverID;
}
public int calculatePrice()
{
float distance = calculateDistance();
if (Type == "Car" || Type == "car")
{
price = (int)((distance * fuel_price) / 15);
float commission = (float)(price * 0.2);
price += (int)commission;
}
else if (Type == "Bike" || Type == "bike")
{
price = (int)((distance * fuel_price) / 50);
float commission = (float)(price * 0.05);
price += (int)commission;
}
else if (Type == "Rickshaw" || Type == "rickshaw")
{
price = (int)((distance * fuel_price) / 35);
float commission = (float)(price * 0.1);
price += (int)commission;
}
return price;
}
public void setLocations(string sloc, string eloc)
{
start_location.setLocation(sloc);
end_location.setLocation(eloc);
}
/////////////////////////////////////////////////HELPER FUNCTIONS/////////////////////////////////////////////////////////
public void go(string name, string phNo, string sLoc, string eLoc, string rideType)
{
passenger.Name = name;
passenger.PhoneNo = phNo;
start_location.setLocation(sLoc);
end_location.setLocation(eLoc);
Type = rideType;
}
float calculateDistance()
{
float lat1 = start_location.getLatitude();
float lon1 = start_location.getLongitude();
float lat2 = end_location.getLatitude();
float lon2 = end_location.getLongitude();
float a = lon2 - lon1;
float b = lat2 - lat1;
a = a * a;
b = b * b;
float val = a + b;
float distance = (float)Math.Sqrt(val);
return distance;
}
float calculateDistance(Location dLoc, string sLoc) //Overloading
{
float lat1 = dLoc.getLatitude();
float lon1 = dLoc.getLongitude();
Location sLocOriginal = new Location();
sLocOriginal.setLocation(sLoc);
float lat2 = sLocOriginal.getLatitude();
float lon2 = sLocOriginal.getLongitude();
float a = lon2 - lon1;
float b = lat2 - lat1;
a = a * a;
b = b * b;
float val = a + b;
float distance = (float)Math.Sqrt(val);
return distance;
}
}
}