Добавьте файлы проекта.

This commit is contained in:
zcher
2025-12-14 18:05:36 +03:00
committed by yato
parent 64b6e01512
commit 84abc28e16
7 changed files with 228 additions and 0 deletions

36
CollectionUtils.cs Normal file
View File

@@ -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<T>(this IEnumerable<T> collection)
{
return collection == null || !collection.Any();
}
public static List<T> ToSafeList<T>(this IEnumerable<T> collection)
{
return collection?.ToList() ?? new List<T>();
}
public static void Shuffle<T>(this IList<T> 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;
}
}
}
}

4
First week.slnx Normal file
View File

@@ -0,0 +1,4 @@
<Solution>
<Project Path="../ContactsApp/ContactsApp.csproj" Id="a2bc9165-c669-4d35-8a64-2c0bbeff9da4" />
<Project Path="UtilityLibrary.csproj" />
</Solution>

43
StringUtils.cs Normal file
View File

@@ -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);
}
}
}

13
Test.cs Normal file
View File

@@ -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()

61
User.cs Normal file
View File

@@ -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;
}
}
}

60
UserManager.cs Normal file
View File

@@ -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 <User> 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}");
}
}
}
}

11
UtilityLibrary.csproj Normal file
View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>First_week</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>