Compare commits

...

4 Commits

Author SHA1 Message Date
yato
9019e66bf3 Исправлен Test.cs (я просто забыл...) 2025-12-15 11:11:52 +03:00
zcher
b1fc78eb26 Hohoho 2025-12-14 18:41:56 +03:00
zcher
84abc28e16 Добавьте файлы проекта. 2025-12-14 18:41:56 +03:00
zcher
64b6e01512 Добавить .gitattributes, .gitignore и README.md. 2025-12-14 18:41:56 +03:00
8 changed files with 266 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
{
public 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>

1
README.md Normal file
View File

@@ -0,0 +1 @@
# First week

48
StringUtils.cs Normal file
View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace First_week
{
public 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);
}
}
}

45
Test.cs Normal file
View File

@@ -0,0 +1,45 @@
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);
nekit.Deactivate();
Console.WriteLine(nekit.IsActive);
UserManager userManager = new UserManager();
User userToAdd1 = new User("Ivan", "Ivanov", new MailAddress("ivan@mail.ru"), new DateOnly(1990, 1, 1));
User userToAdd2 = new User("Petr", "Petrov", new MailAddress("petr@mail.ru"), new DateOnly(1991, 2, 2));
userManager.AddUser(userToAdd1);
userManager.AddUser(userToAdd2);
Console.WriteLine(userManager.Registory.Count);
User foundUser = userManager.Find(userToAdd1.Id);
Console.WriteLine(foundUser.GetFullName());
userManager.RemoveUser(userToAdd2.Id);
Console.WriteLine(userManager.Registory.Count);
List<int> numbers = null;
Console.WriteLine(numbers.IsNullOrEmpty());
List<int> numbersList = new List<int> { 1, 2, 3, 4, 5 };
numbersList.Shuffle();
numbersList.ForEach(x => Console.Write(x + ","));
Console.Write("\n");
string palindromeTest = "А роза упала на лапу Азора";
Console.WriteLine(StringUtils.IsPalindrome(palindromeTest));
string sentence = "утикиН юлбюл Я";
Console.WriteLine(StringUtils.ReverseWords(sentence));
string punctuationTest = "Hello, world!";
Console.WriteLine(StringUtils.RemovePunctuation(punctuationTest));

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
{
public 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
{
public 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>