-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form2.cs
315 lines (242 loc) · 8.01 KB
/
Form2.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.Intrinsics.Arm;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
namespace JAT_AIR_Airline_Form
{
public partial class Form2 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\tnt52\source\repos\JAT-AIR Airline Form\FlightReservationDB.mdf"";Integrated Security=True");
SqlCommand cmd;
SqlCommand checkFullCmd;
SqlCommand checkDupes;
//Constructor
public Form2()
{
InitializeComponent();
DeleteButton.Enabled = false;
ReservationInfo.Enabled = false;
ArrowPointing.Enabled = false;
ArrowPointing.Visible = false;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private bool SaveInfo_Clickbutton = false;
private void SaveInfo_Click(object sender, EventArgs e)
{
try
{
///Console.WriteLine(LastNameTextBox.Text.ToString());
///
//Checks if Flight is at capacity for testing purposes set atCapacity at 1 to demonstrate error
checkFullCmd = new SqlCommand("SELECT CASE WHEN COUNT(*) >= 200 THEN 1 ELSE 0 END FROM ReservedPassengerInfoTable WHERE seating_num BETWEEN 1 AND 200", con);
con.Open();
int atCapacity = Convert.ToInt32(checkFullCmd.ExecuteScalar());
///atCapacity = 1;
con.Close();
if (atCapacity == 1)
{
MessageBox.Show("SORRY, FLIGHT IS AT CAPACITY!");
return;
//return exits function and prevents data entry
}
//Establishes Upper and Lower limit of Seat numbers and prevents record entry if limit is exceeded
if (Convert.ToInt32(SeatingNumberTextBox.Text) > 200 || Convert.ToInt32(SeatingNumberTextBox.Text) < 1)
{
MessageBox.Show("THE SEAT LIMIT IS 1 - 200! choose a between that range");
return;
}
//Test if Value already exists in field Checks for Duplicates
// parameterized query to avoid SQL injection as stated in Class
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\tnt52\source\repos\JAT-AIR Airline Form\FlightReservationDB.mdf"";Integrated Security=True"))
{
con.Open();
using (SqlCommand checkDupes = new SqlCommand("SELECT COUNT(*) FROM ReservedPassengerInfoTable WHERE seating_num = @seatnumber", con))
{
checkDupes.Parameters.AddWithValue("@seatnumber", Convert.ToInt32(SeatingNumberTextBox.Text));
int count = Convert.ToInt32(checkDupes.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("Seat Number is Already Occupied! Please Choose another Number");
return;
}
else
{
MessageBox.Show("Seating Number Ok");
}
}
}
//used to insert passenger record into data base
cmd = new SqlCommand("insert into ReservedPassengerInfoTable(passenger_firstname,passenger_lastname,destination,flight_date,seating_preference,seating_num) " +
"values(@passen_fname, @passen_lname, @passen_destin, @passenf_date, @seat_pref, @seat_number)", con);
con.Open();
cmd.Parameters.AddWithValue("@passen_fname", FirstNameTextBox.Text);
cmd.Parameters.AddWithValue("@passen_lname", LastNameTextBox.Text);
cmd.Parameters.AddWithValue("@passen_destin", DestinationComboBox.Text);
cmd.Parameters.AddWithValue("@passenf_date", FlightDatePicker.Text);
cmd.Parameters.AddWithValue("@seat_pref", SeatingPreferenceComboBox.Text);
cmd.Parameters.AddWithValue("@seat_number", SeatingNumberTextBox.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully, Reservation Made");
//Enabling several form controls after save button is clicked successfully
DeleteButton.Enabled = true;
ReservationInfo.Enabled = true;
ArrowPointing.Enabled = true;
ArrowPointing.Visible = true;
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error Message");
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void FlightDatePicker_ValueChanged(object sender, EventArgs e)
{
}
private void DeleteButton_Click(object sender, EventArgs e)
{
try
{
cmd = new SqlCommand("DELETE FROM ReservedPassengerInfoTable WHERE passenger_id = (SELECT TOP 1 passenger_id FROM ReservedPassengerInfoTable ORDER BY passenger_id DESC)", con);
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (rowsAffected > 0)
{
MessageBox.Show("Last Record Deleted Successfully!");
}
else
{
MessageBox.Show("No records found to delete.", "Info");
}
DeleteButton.Enabled = false; // Disable the button
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error Message");
}
}
private void QuitButton_Click(object sender, EventArgs e)
{
DialogResult confirm = MessageBox.Show("Are you sure you want to exit?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirm == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
}
private void FirstNameTextBox_TextChanged(object sender, EventArgs e)
{
}
private void LastNameTextBox_TextChanged(object sender, EventArgs e)
{
}
private void SeatingNumberTextBox_TextChanged(object sender, EventArgs e)
{
}
public double airfarecost { set; get; }
public double distance { set; get; }
public double TotalTicketCostbefTax { set; get; }
public double TotalTicketCostAfterTax { set; get; }
public const double TAX = 0.165;
private void label8_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
//Reservation Button is Used to Perform Several Calculations and assign value to fields
private void ReservationInfo_Click(object sender, EventArgs e)
{
try
{
switch (SeatingPreferenceComboBox.Text)
{
case "First Class – $18 per km":
airfarecost = 18;
break;
case "Business Class – $12 per km":
airfarecost = 12;
break;
case "Economy Class – $8 per km":
airfarecost = 8;
break;
}
switch (DestinationComboBox.Text)
{
case "Jamaica to Azerbaijan - Distance 11,381 (km)":
distance = 11381;
break;
case "Jamaica to Timbuktu - Distance 7,819 (km)":
distance = 7819;
break;
case "Jamaica to Eritrea - Distance 12,184 (km)":
distance = 12184;
break;
case "Jamaica to Somali - Distance 13,318 (km)":
distance = 13318;
break;
}
TotalTicketCostbefTax = airfarecost * distance;
TotalTicketCostAfterTax = TotalTicketCostbefTax + (TAX * TotalTicketCostbefTax);
/*
label8.Text = TotalTicketCostAfterTax.ToString();
label8.Text = TotalTicketCostbefTax.ToString();
*/
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
//creating a form3 object and passing this form2 (this) as a parameter so we can extract data from it in form3
Form3 f3 = new Form3(this);
this.Hide();
f3.ShowDialog();
}
private void ArrowPointing_Click(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void SeatingPreferenceComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox2_Click_1(object sender, EventArgs e)
{
}
}
}