-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
304 lines (268 loc) · 10.1 KB
/
Copy pathProgram.cs
File metadata and controls
304 lines (268 loc) · 10.1 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Linq;
namespace OOP
{
class Program
{
static void Main(string[] args)
{
// Read data from a CSV file named "value.csv"
string[] file = Readfile("value.csv");
// Extract and process the data to create a list of people
List<Person> people = getPeople(file);
// Print the details of each person in the list
PrintPeople(people);
// Implement additional features
while (true)
{
Console.WriteLine("\nAdditional Features:");
Console.WriteLine("1. Sort by Age");
Console.WriteLine("2. Sort by First Name");
Console.WriteLine("3. Sort by Last Name");
Console.WriteLine("4. Search by First Name");
Console.WriteLine("5. Search by Last Name");
Console.WriteLine("6. Search by Occupation");
Console.WriteLine("7. Export to CSV");
Console.WriteLine("8. Exit");
int choice = GetChoice(1, 8);
switch (choice)
{
case 1:
people = SortByAge(people);
PrintPeople(people);
break;
case 2:
people = SortByFirstName(people);
PrintPeople(people);
break;
case 3:
people = SortByLastName(people);
PrintPeople(people);
break;
case 4:
Console.Write("Enter First Name to search: ");
string firstNameToSearch = Console.ReadLine();
SearchByFirstName(people, firstNameToSearch);
break;
case 5:
Console.Write("Enter Last Name to search: ");
string lastNameToSearch = Console.ReadLine();
SearchByLastName(people, lastNameToSearch);
break;
case 6:
Console.Write("Enter Occupation to search: ");
string occupationToSearch = Console.ReadLine();
SearchByOccupation(people, occupationToSearch);
break;
case 7:
ExportToCSV(people);
break;
case 8:
Console.WriteLine("Exiting the program.");
return;
}
}
}
static string[] Readfile(string filename)
{
string[] lines = System.IO.File.ReadAllLines(filename);
return lines;
}
static List<Person> getPeople(string[] file)
{
Dictionary<int, List<string>> file_items = new Dictionary<int, List<string>>();
List<Person> people = new List<Person>();
// Extract CSV headers and data into a dictionary
for (int i = 0; i < file.Length; i++)
file_items.Add(i, GetItems(file[i]));
// Process each data row to create Person objects
for (int i = 1; i < file.Length; i++)
{
string firstname = "", lastname = "", occupation = "";
int age = 0;
// Extract data fields based on CSV headers
for (int j = 0; j < file_items[0].Count(); j++)
{
switch (file_items[0][j])
{
case "firstname":
firstname = file_items[i][j];
break;
case "lastname":
lastname = file_items[i][j];
break;
case "occupation":
occupation = file_items[i][j];
break;
case "age":
age = int.Parse(file_items[i][j]);
break;
default:
Console.WriteLine($"Header '{file_items[0][j]}' is not valid");
break;
}
}
// Create a Person object and add it to the list
Person e = new Person(firstname, lastname, occupation, age);
people.Add(e);
}
return people;
}
static List<string> GetItems(string line)
{
string current_word = "";
List<string> items = new List<string>();
// Parse the line character by character
foreach (char c in line)
{
if (c == ',')
{
if (current_word != "")
{
items.Add(current_word);
current_word = "";
}
}
else
{
current_word += c.ToString();
}
}
// Add the last item if it exists
if (current_word != "") items.Add(current_word);
return items;
}
static void PrintPeople(List<Person> people)
{
Console.WriteLine("\nList of People:");
foreach (Person p in people)
{
Console.WriteLine($"{p._FirstName} {p._LastName} is {p._Age.ToString()} years old and works as a {p._Occupation}");
}
}
static List<Person> SortByAge(List<Person> people)
{
return people.OrderBy(p => p._Age).ToList();
}
static List<Person> SortByFirstName(List<Person> people)
{
return people.OrderBy(p => p._FirstName).ToList();
}
static List<Person> SortByLastName(List<Person> people)
{
return people.OrderBy(p => p._LastName).ToList();
}
static void SearchByFirstName(List<Person> people, string firstName)
{
var results = people.Where(p => p._FirstName.Equals(firstName, StringComparison.OrdinalIgnoreCase)).ToList();
if (results.Count == 0)
{
Console.WriteLine($"No matching records found for first name: {firstName}");
}
else
{
Console.WriteLine($"\nSearch Results for First Name: {firstName}");
PrintPeople(results);
}
}
static void SearchByLastName(List<Person> people, string lastName)
{
var results = people.Where(p => p._LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase)).ToList();
if (results.Count == 0)
{
Console.WriteLine($"No matching records found for last name: {lastName}");
}
else
{
Console.WriteLine($"\nSearch Results for Last Name: {lastName}");
PrintPeople(results);
}
}
static void SearchByOccupation(List<Person> people, string occupation)
{
var results = people.Where(p => p._Occupation.Contains(occupation, StringComparison.OrdinalIgnoreCase)).ToList();
if (results.Count == 0)
{
Console.WriteLine($"No matching records found for occupation: {occupation}");
}
else
{
Console.WriteLine($"\nSearch Results for Occupation: {occupation}");
PrintPeople(results);
}
}
static void ExportToCSV(List<Person> people)
{
Console.Write("Enter the CSV file name to export: ");
string fileName = Console.ReadLine();
fileName += ".csv";
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(fileName))
{
// Write CSV headers
file.WriteLine("firstname,lastname,occupation,age");
// Write data for each person
foreach (Person p in people)
{
file.WriteLine($"{p._FirstName},{p._LastName},{p._Occupation},{p._Age}");
}
}
Console.WriteLine($"Data exported to {fileName}");
}
static int GetChoice(int min, int max)
{
int choice;
while (!int.TryParse(Console.ReadLine(), out choice) || choice < min || choice > max)
{
Console.WriteLine("Invalid choice. Please enter a valid option.");
Console.Write("Enter your choice: ");
}
return choice;
}
}
// Define a Person class to represent individual records
class Person
{
private string firstName = "";
private string lastName = "";
private int Age = 0;
private string Occupation = "";
public string _FirstName
{
get
{
return firstName;
}
}
public string _LastName
{
get
{
return lastName;
}
}
public string _Occupation
{
get
{
return Occupation;
}
}
public int _Age
{
get
{
return Age;
}
}
// Constructor to initialize Person objects
public Person(string firstname, string lastname, string occupation, int age)
{
firstName = firstname;
lastName = lastname;
Occupation = occupation;
Age = age;
}
}
}