-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDrug_Management_System.java
324 lines (304 loc) · 10.4 KB
/
Drug_Management_System.java
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
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
class PharmacyManagementSystem
{
private Dictionary<string, Dictionary<string, dynamic>> drugInventory = new Dictionary<string, Dictionary<string, dynamic>>();
private void AddDrug()
{
Console.Write("Enter drug name: ");
string name = Console.ReadLine();
Console.Write("Enter price: ");
float price = float.Parse(Console.ReadLine());
Console.Write("Enter quantity: ");
int quantity = int.Parse(Console.ReadLine());
drugInventory[name] = new Dictionary<string, dynamic> { { "price", price }, { "quantity", quantity } };
Console.WriteLine("Drug added successfully!");
}
private void UpdateDrug()
{
Console.Write("Enter drug name: ");
string name = Console.ReadLine();
if (drugInventory.ContainsKey(name))
{
Console.Write("Enter new price: ");
float price = float.Parse(Console.ReadLine());
Console.Write("Enter new quantity: ");
int quantity = int.Parse(Console.ReadLine());
drugInventory[name]["price"] = price;
drugInventory[name]["quantity"] = quantity;
Console.WriteLine("Drug information updated successfully!");
}
else
{
Console.WriteLine("Drug not found in the inventory!");
}
}
private void ViewDrug()
{
Console.Write("Enter drug name (leave blank to view all drugs): ");
string name = Console.ReadLine();
if (!string.IsNullOrEmpty(name))
{
if (drugInventory.ContainsKey(name))
{
Dictionary<string, dynamic> drug = drugInventory[name];
Console.WriteLine($"Drug Name: {name}");
Console.WriteLine($"Price: {drug["price"]}");
Console.WriteLine($"Quantity: {drug["quantity"]}");
}
else
{
Console.WriteLine("Drug not found in the inventory!");
}
}
else
{
if (drugInventory.Count == 0)
{
Console.WriteLine("No drugs in the inventory!");
}
else
{
foreach (KeyValuePair<string, Dictionary<string, dynamic>> drug in drugInventory)
{
Console.WriteLine($"Drug Name: {drug.Key}");
Console.WriteLine($"Price: {drug.Value["price"]}");
Console.WriteLine($"Quantity: {drug.Value["quantity"]}");
}
}
}
}
private void RecordPurchase()
{
Console.Write("Enter drug name: ");
string name = Console.ReadLine();
if (drugInventory.ContainsKey(name))
{
Console.Write("Enter quantity purchased: ");
int quantity = int.Parse(Console.ReadLine());
if (quantity <= drugInventory[name]["quantity"])
{
drugInventory[name]["quantity"] -= quantity;
Console.WriteLine("Purchase recorded successfully!");
}
else
{
Console.WriteLine("Insufficient quantity in the inventory!");
}
}
else
{
Console.WriteLine("Drug not found in the inventory!");
}
}
private void SearchDrug()
{
Console.Write("Enter a keyword to search for drugs: ");
string keyword = Console.ReadLine();
List<string> searchResults = new List<string>();
foreach (KeyValuePair<string, Dictionary<string, dynamic>> drug in drugInventory)
{
if (drug.Key.ToLower().Contains(keyword.ToLower()))
{
searchResults.Add(drug.Key);
}
}
if (searchResults.Count > 0)
{
Console.WriteLine("Search Results:");
foreach (string result in searchResults)
{
Console.WriteLine(result);
}
}
else
{
Console.WriteLine("No drugs found matching the keyword.");
}
}
private void DeleteDrug()
{
Console.Write("Enterdrug name to delete: ");
string name = Console.ReadLine();
if (drugInventory.ContainsKey(name))
{
drugInventory.Remove(name);
Console.WriteLine($"{name} deleted from the inventory.");
}
else
{
Console.WriteLine("Drug not found in the inventory!");
}
}
private void SetExpirationDate()
{
Console.Write("Enter drug name: ");
string name = Console.ReadLine();
if (drugInventory.ContainsKey(name))
{
Console.Write("Enter expiration date (YYYY-MM-DD): ");
string expirationDate = Console.ReadLine();
drugInventory[name]["expiration_date"] = expirationDate;
Console.WriteLine("Expiration date set successfully!");
}
else
{
Console.WriteLine("Drug not found in the inventory!");
}
}
private void CheckLowStockAlert()
{
Console.Write("Enter the minimum quantity threshold: ");
int threshold = int.Parse(Console.ReadLine());
List<string> lowStockDrugs = new List<string>();
foreach (KeyValuePair<string, Dictionary<string, dynamic>> drug in drugInventory)
{
if (drug.Value["quantity"] <= threshold)
{
lowStockDrugs.Add(drug.Key);
}
}
if (lowStockDrugs.Count > 0)
{
Console.WriteLine("Low Stock Drugs:");
foreach (string drug in lowStockDrugs)
{
Console.WriteLine(drug);
}
}
else
{
Console.WriteLine("No drugs are below the quantity threshold.");
}
}
private void GenerateSalesReport()
{
float totalSales = 0;
foreach (KeyValuePair<string, Dictionary<string, dynamic>> drug in drugInventory)
{
float price = drug.Value["price"];
int quantitySold = drug.Value["quantity"] - drugInventory[drug.Key]["quantity"];
totalSales += price * quantitySold;
}
Console.WriteLine($"Total Sales: ${totalSales:F2}");
List<KeyValuePair<string, Dictionary<string, dynamic>>> sortedDrugs = new List<KeyValuePair<string, Dictionary<string, dynamic>>>(drugInventory);
sortedDrugs.Sort((x, y) => y.Value["quantity"].CompareTo(x.Value["quantity"]));
Console.WriteLine("Top Selling Drugs:");
for (int i = 0; i < Math.Min(sortedDrugs.Count, 5); i++)
{
KeyValuePair<string, Dictionary<string, dynamic>> drug = sortedDrugs[i];
Console.WriteLine($"Drug Name: {drug.Key}");
Console.WriteLine($"Quantity Sold: {drug.Value["quantity"] - drugInventory[drug.Key]["quantity"]}");
}
}
private void UserAuthentication()
{
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
if (username == "admin" && password == "password")
{
Console.WriteLine("Authentication successful. Access granted.");
}
else
{
Console.WriteLine("Authentication failed. Access denied.");
}
}
private void SaveData()
{
string json = JsonConvert.SerializeObject(drugInventory);
File.WriteAllText("drug_inventory.json", json);
Console.WriteLine("Data saved successfully.");
}
private void LoadData()
{
try
{
string json = File.ReadAllText("drug_inventory.json");
drugInventory = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, dynamic>>>(json);
Console.WriteLine("Data loaded successfully.");
}
catch (FileNotFoundException)
{
Console.WriteLine("No previous data found.");
}
}
public void Menu()
{
while (true)
{
Console.WriteLine("\nPharmacy Management System");
Console.WriteLine("1. Add Drug");
ConsoleConsole.WriteLine("2. Update Drug Information");
Console.WriteLine("3. View Drug Information");
Console.WriteLine("4. Record Purchase");
Console.WriteLine("5. Search Drug");
Console.WriteLine("6. Delete Drug");
Console.WriteLine("7. Set Expiration Date");
Console.WriteLine("8. Check Low Stock Alert");
Console.WriteLine("9. Generate Sales Report");
Console.WriteLine("10. User Authentication");
Console.WriteLine("11. Save Data");
Console.WriteLine("12. Load Data");
Console.WriteLine("13. Quit");
Console.Write("Enter your choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
AddDrug();
break;
case "2":
UpdateDrug();
break;
case "3":
ViewDrug();
break;
case "4":
RecordPurchase();
break;
case "5":
SearchDrug();
break;
case "6":
DeleteDrug();
break;
case "7":
SetExpirationDate();
break;
case "8":
CheckLowStockAlert();
break;
case "9":
GenerateSalesReport();
break;
case "10":
UserAuthentication();
break;
case "11":
SaveData();
break;
case "12":
LoadData();
break;
case "13":
return;
default:
Console.WriteLine("Invalid choice. Try again!");
break;
}
}
}
}
class Program
{
static void Main()
{
PharmacyManagementSystem pharmacyManagementSystem = new PharmacyManagementSystem();
pharmacyManagementSystem.Menu();
}
}