-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserList.cs
104 lines (88 loc) · 2.41 KB
/
UserList.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeManagementSystem
{
internal class UserList
{
List<User> userSet = new List<User>();
public UserList(List<User> user)
{
userSet = user;
}
public void AddUser(User user)
{
if (userSet.Any(e => e.Username == user.Username))
{
throw new DublicateUsernameException("Username already exists in the system.");
}
else
{
// Generate a new unique ID
int newId = userSet.Any() ? (userSet.Max(usr => usr.Id) + 1) : 0;
user.Id = newId;
userSet.Add(user);
}
}
public bool RemoveUser(int id)
{
User userToRemove = null;
foreach (User u in userSet)
{
if (u.Id == id)
{
userToRemove = u;
break;
}
}
if (userToRemove != null)
{
userSet.Remove(userToRemove);
return true;
}
else
{
Console.WriteLine($"User with ID {id} not found.");
return false;
}
}
public User GetUser(int id)
{
foreach (User user in userSet)
{
if (user.Id == id)
{
return user;
}
}
return null;
}
public User GetByUsername(string username)
{
foreach (User user in userSet)
{
if (user.Username == username)
{
return user;
}
}
return null;
}
public int GetAssignedTaskCount(User user)
{
int assignedTasks = 0;
foreach (Tasks task in Program.tasksList.GetAllTasks())
{
if (task.AssignedTo == user.Name)
{
assignedTasks++;
}
}
return assignedTasks;
}
public List<User> GetAllUsers() => userSet;
public List<User> GetAllEmployees() => userSet.Where(user => user.IsEmployee == true).ToList();
}
}