diff --git a/CollectionUtils.cs b/CollectionUtils.cs new file mode 100644 index 0000000..a95f990 --- /dev/null +++ b/CollectionUtils.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace First_week +{ + internal static class CollectionUtils + { + public static bool IsNullOrEmpty(this IEnumerable collection) + { + return collection == null || !collection.Any(); + } + public static List ToSafeList(this IEnumerable collection) + { + return collection?.ToList() ?? new List(); + } + + public static void Shuffle(this IList list) + { + if (list == null) + return; + + Random rng = new Random(); + int n = list.Count; + while (n > 1) + { + n--; + int k = rng.Next(n + 1); + T value = list[k]; + list[k] = list[n]; + list[n] = value; + } + } + } +} \ No newline at end of file diff --git a/First week.slnx b/First week.slnx new file mode 100644 index 0000000..82dc1bf --- /dev/null +++ b/First week.slnx @@ -0,0 +1,4 @@ + + + + diff --git a/StringUtils.cs b/StringUtils.cs new file mode 100644 index 0000000..11187ed --- /dev/null +++ b/StringUtils.cs @@ -0,0 +1,43 @@ +namespace First_week +{ + internal class StringUtils + { + public static bool IsPalindrome(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + string cleaned = new string(s.Where(c => char.IsLetterOrDigit(c)).ToArray()).ToLower(); + string reversed = new string(cleaned.Reverse().ToArray()); + + return cleaned == reversed; + } + + public static string ReverseWords(string sentence) + { + if (string.IsNullOrEmpty(sentence)) + return sentence; + + var words = sentence.Split(' '); + var reversedWords = words.Select(word => new string(word.Reverse().ToArray())); + + return string.Join(" ", reversedWords); + } + + public static string RemovePunctuation(string s) + { + if (string.IsNullOrEmpty(s)) + return s; + + return new string(s.Where(c => !char.IsPunctuation(c)).ToArray()); + } + + public static bool IsAlphaNumeric(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + return s.All(char.IsLetterOrDigit); + } + } +} diff --git a/Test.cs b/Test.cs new file mode 100644 index 0000000..b70c351 --- /dev/null +++ b/Test.cs @@ -0,0 +1,13 @@ +using System.Net.Mail; +using First_week; + +User nekit = new User("Nikita", "Polyanin", new MailAddress("polyanin@mail.ru"), new DateOnly(2004, 12, 8)); +Console.WriteLine(nekit.GetFullName()); + +nekit.UpdateEmail(new MailAddress("lox@mail.ru")); +Console.WriteLine(nekit.Email); + +nekit.UpdateDate(new DateOnly(2024,12, 8)); +Console.WriteLine(nekit.Date); + +CollectionUtils.Shuffle() \ No newline at end of file diff --git a/User.cs b/User.cs new file mode 100644 index 0000000..42d2adb --- /dev/null +++ b/User.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Net.Mail; +using System.Runtime.CompilerServices; +using System.Text; + +namespace First_week +{ + internal class User + { + public Guid Id { get; } + public string Name { get; private set; } + public string Surname { get; private set; } + public MailAddress? Email { get; private set; } + public DateOnly Date { get; private set; } + public bool IsActive { get; private set; } + + public User(string name, string surname, MailAddress email, DateOnly date) + { + if (name == null || name.Length > 100) + throw new Exception("Incorrect name"); + + if (surname == null || surname.Length > 100) + throw new Exception("Incorrect surname"); + + if (date.CompareTo(DateOnly.FromDateTime(DateTime.Now)) > 0) + throw new Exception("Incorrect birthdate"); + + Id = Guid.NewGuid(); + Name = name; + Surname = surname; + Email = email; + Date = date; + IsActive = true; + } + + public string GetFullName() + { + return ($"{Surname} {Name}"); + } + + public void UpdateEmail(MailAddress newMail) + { + Email = newMail; + } + + public void UpdateDate(DateOnly date) + { + if (date.CompareTo(DateOnly.FromDateTime(DateTime.Now)) > 0) + throw new Exception("Wrong date"); + + Date = date; + } + + public void Deactivate() + { + IsActive = false; + } + } +} diff --git a/UserManager.cs b/UserManager.cs new file mode 100644 index 0000000..d93c17c --- /dev/null +++ b/UserManager.cs @@ -0,0 +1,60 @@ +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Net.Mail; +using System.Text; + +namespace First_week +{ + internal class UserManager + { + public List Registory = new(); + + public void AddUser(User user) + { + CheckUniqe(user); + Registory.Add(user); + } + + public void RemoveUser(Guid id) + { + User? user = Registory.Find(x => x.Id == id); + if (user == null) + return; + + Registory.Remove(user); + } + + public User Find(Guid id) + { + User? user = Registory.Find(x => x.Id == id); + if (user == null) + throw new Exception("Not found"); + + return user; + } + + public User Find(MailAddress email) + { + User? user = Registory.Find(x => x.Email == email); + if (user == null) + throw new Exception("Not found"); + + return user; + } + + private void CheckUniqe(User user) + { + if (Registory.FirstOrDefault(x => x.Equals(user)) != null) + throw new Exception("Such a user already exists"); + } + + public void Show() + { + foreach (var user in Registory) + { + Console.WriteLine($"{user.GetFullName} {user.Id}"); + } + } + } +} diff --git a/UtilityLibrary.csproj b/UtilityLibrary.csproj new file mode 100644 index 0000000..843d59d --- /dev/null +++ b/UtilityLibrary.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + First_week + enable + enable + + +