-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallCategories.cs
More file actions
280 lines (216 loc) · 9.26 KB
/
allCategories.cs
File metadata and controls
280 lines (216 loc) · 9.26 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project
{
public partial class allCategories : Form
{
string connectionString = "Data Source= SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
List<Product> cartProducts = new List<Product>(); // List to store cart products
private int customerId;
// Constructor to accept customer ID
public allCategories(int customerId)
{
InitializeComponent();
this.customerId = customerId;
LoadCategories();
}
private void LoadCategories()
{
// Clear existing items in the listBox1
listBox1.Items.Clear();
// SQL query to select all categories
string query = "SELECT * FROM Categories";
// Create a SqlConnection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the database connection
connection.Open();
// Create a SqlCommand to execute the query
using (SqlCommand command = new SqlCommand(query, connection))
{
// Execute the query and get a data reader
using (SqlDataReader reader = command.ExecuteReader())
{
// Check if the reader has rows
while (reader.Read())
{
// Assuming the second column is CategoryName
string categoryName = reader.GetString(1);
// Add the category name to the listBox1
listBox1.Items.Add(categoryName);
}
}
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
string selectedCategory = listBox1.SelectedItem.ToString();
// Clear existing items in the listBox2
listBox2.Items.Clear();
// SQL query to select products for the selected category with their prices
string query = "SELECT P.ProductID, P.Name, P.Price " +
"FROM Products P " +
"JOIN Categories C ON P.CategoryID = C.CategoryID " +
"WHERE C.CategoryName = @CategoryName";
// Create a SqlConnection to the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the database connection
connection.Open();
// Create a SqlCommand to execute the query
using (SqlCommand command = new SqlCommand(query, connection))
{
// Add a parameter to the query to filter by category name
command.Parameters.AddWithValue("@CategoryName", selectedCategory);
// Execute the query and get a data reader
using (SqlDataReader reader = command.ExecuteReader())
{
// Check if the reader has rows
while (reader.Read())
{
// Assuming the first column is ProductID, the second is Product Name, and the third is Price
int productID = reader.GetInt32(0);
string productName = reader.GetString(1);
decimal productPrice = reader.GetDecimal(2);
// Add the product name and right-aligned price to the listBox2 with adjusted spacing
listBox2.Items.Add(new Product { ProductID = productID, Name = productName, Price = productPrice });
}
}
}
}
}
}
private void allCategories_Load(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void addToCartButton_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
{
Product selectedProduct = (Product)listBox2.SelectedItem;
// Add the selected product to the cartProducts list
cartProducts.Add(selectedProduct);
// Update the cartListBox to show the cart contents and get the total cost
decimal totalCost = UpdateCartListBox();
totalCostLabel.Text = $"Total Cost: {totalCost:C}";
// Clear the selected index in listBox2
//listBox2.SelectedIndex = -1;
}
}
private void cartListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private decimal UpdateCartListBox()
{
// Clear existing items in the cartListBox
cartListBox.Items.Clear();
decimal totalCost = 0;
// Add the cart products to the cartListBox
foreach (Product product in cartProducts)
{
cartListBox.Items.Add($"{product.Name,-30} {product.Price,10:C}");
totalCost += product.Price;
}
// Display the total cost in a label (you can use a label to display additional information)
totalCostLabel.Text = $"Total Cost: {totalCost:C}";
return totalCost;
}
// Define a simple Product class to store product information
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public override string ToString()
{
return $"{Name} - {Price:C}";
}
}
private void totalCostLabel_Click(object sender, EventArgs e)
{
}
private void removeButton_Click(object sender, EventArgs e)
{
// Check if an item is selected in the cartListBox
if (cartListBox.SelectedIndex != -1)
{
// Retrieve the selected item (assumed to be a string)
string selectedCartItem = (string)cartListBox.SelectedItem;
// Extract the product name from the selected string
string productName = selectedCartItem.Split(' ')[0]; // Assuming the product name is the first part of the string
// Find and remove the corresponding product from the cartProducts list
Product productToRemove = cartProducts.FirstOrDefault(product => product.Name == productName);
if (productToRemove != null)
{
cartProducts.Remove(productToRemove);
}
// Update the cartListBox and total cost
decimal totalCost = UpdateCartListBox();
totalCostLabel.Text = $"Total Cost: {totalCost:C}";
}
}
private string GenerateReceipt()
{
StringBuilder receipt = new StringBuilder();
receipt.AppendLine("Receipt");
receipt.AppendLine("-----------");
foreach (Product product in cartProducts)
{
receipt.AppendLine($"{product.Name,-30} {product.Price,10:C}");
}
receipt.AppendLine("-----------");
receipt.AppendLine($"Total Cost: {cartProducts.Sum(product => product.Price):C}");
receipt.AppendLine("-----------");
return receipt.ToString();
}
private void ReceiptForm_Load(object sender, EventArgs e)
{
// Display the generated receipt
string receiptContent = GenerateReceipt();
MessageBox.Show(receiptContent, "Receipt");
}
private void checkoutButton_Click(object sender, EventArgs e)
{
// Create an instance of the ReceiptForm
ConfirmOrderForm ConfirmOrderForm = new ConfirmOrderForm(cartProducts, cartProducts.Sum(product => product.Price));
// Show the ReceiptForm
ConfirmOrderForm.ShowDialog();
}
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
CustomerMenu customerMenu = new CustomerMenu(customerId);
customerMenu.Show();
}
private void Close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
this.Hide();
CustomerMenu customerMenu = new CustomerMenu(customerId);
customerMenu.Show();
}
}
}