-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShortestPath.cs
210 lines (195 loc) · 6.61 KB
/
ShortestPath.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
204
205
206
207
208
209
210
using Lab_8;
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace Travelling_SalesMan_Problem
{
public partial class ShortestPath : Form
{
int interval = 1;
string receiver = "";
int check = 0;
public static double speed = 50;
public static float costPerKm = 4;
static DynamicQueue boy = new DynamicQueue();
ArrayList Address = new ArrayList();
public ShortestPath()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void printShortestRoute(Route shortestRoute)
{
ArrayList city = shortestRoute.getCities();
City final = (City)city[city.Count - 1];
double distance = (shortestRoute.calculateTotalDistance() * 1.609)+ final.measureDistance(new City("Bahria University", 24.893277, 67.08818145));
Distance.Text = distance.ToString("0.##") + " Km";
Time.Text = ((distance / speed)+0.5).ToString("0.##") +" hrs";
Cost.Text = "Rs "+(costPerKm * distance).ToString("0.##");
}
public double[] geData(string Name)
{
double[] data = new double[2];
StreamReader reader = new StreamReader(@"Data.csv");
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (!String.IsNullOrWhiteSpace(line))
{
string[] values = line.Split(',');
if (values[0].ToLower() == Name)
{
data[0] = double.Parse(values[1]);
data[1] = double.Parse(values[2]);
return data;
}
}
}
data[0] = -1;
return data;
}
public double[] getData(string Name)
{
double[] data = new double[2];
StreamReader reader = new StreamReader(@"Data.csv");
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (!String.IsNullOrWhiteSpace(line))
{
string[] values = line.Split(',');
if(values[0].ToLower() == Name)
{
data[0] = double.Parse(values[1]);
data[1] = double.Parse(values[2]);
return data;
}
}
}
data[0] = -1;
return data;
}
private void button1_Click(object sender, EventArgs e)
{
if(interval == 1)
{
Timer.Enabled = true;
Timer.Start();
RouteTimer.Enabled = true;
RouteTimer.Start();
}
double[] data = getData(Area.Text.ToLower());
if (data[0] == -1)
MessageBox.Show("Area Not Found. Make sure you have write correct spellings");
else
{
Address.Add(new City(House.Text + " , " + Area.Text, data[0], data[1]));
Input.Text = Input.Text + House.Text + " , " + Area.Text + "\n";
}
House.Text = null;
Area.Text = null;
}
public static void UpdateQueue(int id, InsertBoy _)
{
boy.Enque(new DeliveryBoy().GetData(id));
MessageBox.Show("Updated Successfully");
_.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void setDeliveryBoy()
{
if (check == 0)
{
StreamReader reader = new StreamReader(@"DeliveryBoy.csv");
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (!String.IsNullOrWhiteSpace(line))
{
string[] values = line.Split(',');
boy.Enque(new DeliveryBoy(int.Parse(values[0]), values[1].ToString(), values[2].ToString()));
}
}
check = -1;
}
else if (boy.UnderFlow())
{
DBoy.Text = "No DeliveryBoy Present at The Moment";
return;
}
DeliveryBoy temp = boy.Deque();
DBoy.Text = temp.getName();
receiver = temp.getEmail();
}
private void FindRoute()
{
Route route = new Route(Address);
setDeliveryBoy();
printShortestRoute(new NearestNeighbour(this).FindShortestRoute(Address));
RouteTimer.Stop();
Timer.Stop();
}
private void button2_Click_1(object sender, EventArgs e)
{
FindRoute();
}
private void button3_Click(object sender, EventArgs e)
{
Address = new ArrayList();
Input.Text = null;
output.Text = null;
Distance.Text = null;
Cost.Text = null;
Time.Text = null;
DBoy.Text = null;
interval = 0;
Timer.Stop();
second.Text = "0";
RouteTimer.Stop();
}
private void button4_Click(object sender, EventArgs e)
{
new InsertBoy().ShowDialog();
}
private void button5_Click(object sender, EventArgs e)
{
new Email().SendEmail(receiver, output.Text + "\n Distance: " + Distance.Text + "\n Expected Cost: " + Cost.Text+"\n Expected Time: "+Time.Text);
MessageBox.Show("Email Send Successfully");
}
private void Timer_Tick_1(object sender, EventArgs e)
{
interval++;
second.Text = interval.ToString();
}
private void RouteTimer_Tick(object sender, EventArgs e)
{
FindRoute();
}
private void button6_Click(object sender, EventArgs e)
{
new Settings().ShowDialog();
}
private void button7_Click(object sender, EventArgs e)
{
Timer.Start();
RouteTimer.Start();
}
private void button8_Click(object sender, EventArgs e)
{
Timer.Stop();
RouteTimer.Stop();
}
public void NoInternet()
{
MessageBox.Show("No Internet Connection");
}
private void ShortestPath_Load(object sender, EventArgs e)
{
}
}
}