-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
290 lines (247 loc) · 9.22 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3
{
//1-Optional arguments-d
class Book
{
public string Title { get; }
public string Author { get; }
public Book(string title, string author = "Unknown")
{
Title = title;
Author = author;
}
}
//2-Generics:
//a.MyList<T> generic class:
class MyList<T>
{
private List<T> items = new List<T>();
public void Add(T item)
{
items.Add(item);
}
public bool Remove(T item)
{
return items.Remove(item);
}
public void Display()
{
foreach (var item in items)
{
Console.WriteLine(item);
}
}
}
class Program
{
//1-Optional arguments-a
static void GreetMethod(string greeting = "Hello", string name = "World")
{
Console.WriteLine($"{greeting}, {name}!");
}
//1-Optional arguments-b
static double CalculateArea(double length = 1.0, double width = 1.0)
{
return length * width;
}
//1-Optional arguments-c
static int AddNumbers(int a, int b)
{
return a + b;
}
static int AddNumbers(int a, int b, int c = 0)
{
return a + b + c;
}
//2-Generics:
//b- Swap<T> generic method:
static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
//2-Generics:
//c- SUM<T> generic method:
static T Sum<T>(params T[] values)
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(long) || typeof(T) == typeof(double))
{
dynamic sum = 0;
foreach (T value in values)
{
sum += value;
}
return sum;
}
else
{
throw new ArgumentException("Unsupported data type");
}
}
//Student DataBase Task
static Dictionary<int, string> studentDatabase = new Dictionary<int, string>();
static void InitializeDatabase()
{
studentDatabase.Add(101, "Alice");
studentDatabase.Add(102, "Bob");
studentDatabase.Add(103, "Charlie");
studentDatabase.Add(104, "David");
}
static void DisplayStudentDatabase()
{
Console.WriteLine("Student Database:");
foreach (var entry in studentDatabase)
{
Console.WriteLine($"Student ID: {entry.Key}, Name: {entry.Value}");
}
}
static void SearchStudentByID()
{
Console.Write("Enter student ID: ");
int studentID = int.Parse(Console.ReadLine());
if (studentDatabase.ContainsKey(studentID))
{
Console.WriteLine($"Student ID: {studentID}, Name: {studentDatabase[studentID]}");
}
else
{
Console.WriteLine("Student ID not found.");
}
}
static void UpdateStudentName()
{
Console.Write("Enter student ID: ");
int idToUpdate = int.Parse(Console.ReadLine());
Console.Write("Enter new name: ");
string newName = Console.ReadLine();
if (studentDatabase.ContainsKey(idToUpdate))
{
studentDatabase[idToUpdate] = newName;
Console.WriteLine("Name updated successfully.");
}
else
{
Console.WriteLine("Student ID not found.");
}
}
static void Main(string[] args)
{
Console.WriteLine("\n---------------------------------------------------------------------------------------------------------");
Console.WriteLine("\t*** TASK1+TASK2 TESTING IMPEMENTATION (Menu Driven for Database system is Below)***\t\t\t");
Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
//1-Optional arguments-a
Console.WriteLine("Optional arguments-a-GreetMethod");
GreetMethod();
GreetMethod("Hi");
GreetMethod("Hi", "John");
Console.WriteLine();
//1-Optional arguments-b
Console.WriteLine("Optional arguments-b-CalculateMethod");
double area1 = CalculateArea(); // Default value
double area2 = CalculateArea(4.5);
double area3 = CalculateArea(3.0, 2.0);
Console.WriteLine(area1);
Console.WriteLine(area2);
Console.WriteLine(area3);
Console.WriteLine();
//1-Optional arguments-c
Console.WriteLine("Optional arguments-c-AddNumberMethod");
int sum1 = AddNumbers(2, 3);
int sum2 = AddNumbers(1, 2, 3);
Console.WriteLine(sum1);
Console.WriteLine(sum2);
Console.WriteLine();
////1-Optional arguments-d
Console.WriteLine("1-Optional arguments-d-Book Class");
Console.WriteLine();
Book book1 = new Book("The Book");
Book book2 = new Book("Another Book", "Author Name");
Console.WriteLine($"Book 1: {book1.Title}, Author: {book1.Author}"); // Author will be "Unknown"
Console.WriteLine($"Book 2: {book2.Title}, Author: {book2.Author}");
Console.WriteLine();
//2-Generics:
//b- Swap<T> generic method:
Console.WriteLine("2-Generics b- Swap<T> generic method:");
Console.WriteLine();
int num1 = 5, num2 = 10;
Swap(ref num1, ref num2);
Console.WriteLine($"Swapped numbers are: {num1}, {num2}");
string str1 = "Hello", str2 = "World";
Swap(ref str1, ref str2);
Console.WriteLine($"Swapped strings are: {str1}, {str2}");
Console.WriteLine();
//2-Generics:
//c- SUM<T> generic method:
Console.WriteLine("2-Generics:b- SUM<T> generic method:");
Console.WriteLine();
int su1 = Sum(1, 2, 3);
double su2 = Sum(2.5, 3.7, 1.2);
Console.WriteLine(sum1);
Console.WriteLine(sum2);
Console.WriteLine("2-Generics:b- LIST<T> generic method:");
Console.WriteLine();
//GENERIC LIST
MyList<int> intL = new MyList<int>();
intL.Add(20);
intL.Add(30);
Console.WriteLine("Integer List:");
intL.Display();
intL.Remove(20);
// Display the modified integer list
Console.WriteLine("Modified Integer List:");
intL.Display();
//String List
MyList<string> stringList = new MyList<string>();
stringList.Add("Apple");
stringList.Add("Banana");
stringList.Add("Cherry");
Console.WriteLine("String List:");
stringList.Display();
stringList.Remove("Banana");
Console.WriteLine("Modified String List:");
stringList.Display();
Console.WriteLine();
// Student database system
Console.WriteLine("\n----------------------------------------------------------------------------------");
Console.WriteLine("\t\t\t*** Student Management System ***\t\t\t");
Console.WriteLine("----------------------------------------------------------------------------------");
InitializeDatabase();
bool exit = false;
while (!exit)
{
Console.WriteLine("Menu:");
Console.WriteLine("1. View the student database");
Console.WriteLine("2. Search for a student by ID");
Console.WriteLine("3. Update a student's name");
Console.WriteLine("4. Exit");
Console.WriteLine();
Console.WriteLine("Please Enter Your Choice:");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
DisplayStudentDatabase();
break;
case 2:
SearchStudentByID();
break;
case 3:
UpdateStudentName();
break;
case 4:
exit = true;
break;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
}
}
}