Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions HomeWork12/HomeWork12.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeWork12", "HomeWork12\HomeWork12.csproj", "{2CF99823-1819-49D2-A0FC-2BE327E67693}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2CF99823-1819-49D2-A0FC-2BE327E67693}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CF99823-1819-49D2-A0FC-2BE327E67693}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CF99823-1819-49D2-A0FC-2BE327E67693}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CF99823-1819-49D2-A0FC-2BE327E67693}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {78924CCC-685A-4A9B-AD3E-86D1107CC7A3}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions HomeWork12/HomeWork12/Entities/ContactEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HomeWork12.Enum;

namespace HomeWork12.Entities
{
internal class ContactEntity
{
public Guid Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Number { get; set; }
public DateTime? CreateAt { get; set; }
public DirectoryType SectorType { get; set; }
}
}
9 changes: 9 additions & 0 deletions HomeWork12/HomeWork12/Enum/DirectoryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HomeWork12.Enum
{
internal enum DirectoryType
{
Alphabet,
HeshTeg,
Number
}
}
29 changes: 29 additions & 0 deletions HomeWork12/HomeWork12/Extentions/ContactExtention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HomeWork12.Models;

namespace HomeWork12
{
internal static class ContactExtention
{
public static List<Contact> SortContacts(this List<Contact> contacts)
{
var temp = new Contact();

for (int count = contacts.Count / 2; count >= 1; count /= 2)
{
for (int i = 0; i < contacts.Count; i++)
{
for (int c = i - count; c >= 0; c -= count)
{
if (contacts[c].CompareTo(contacts[c + count]) > 0)
{
temp = contacts[c];
contacts[c] = contacts[c + count];
contacts[c + count] = temp;
}
}
}
}
return contacts;
}
}
}
18 changes: 18 additions & 0 deletions HomeWork12/HomeWork12/HomeWork12.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions HomeWork12/HomeWork12/Models/Contact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using HomeWork12.Enum;

namespace HomeWork12.Models
{
internal class Contact : IComparable<Contact>
{
public Guid Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Number { get; set; }
public DateTime? CreateAt { get; set; }
public DirectoryType SectorType { get; set; }

public override string ToString()
{
return @$"firstname {FirstName} lastname {LastName}
number of phone: {Number}
CreateTime at {CreateAt}
Directory {SectorType}";
}

public int CompareTo(Contact? contact)
{
int secondComper = SectorType.CompareTo(contact?.SectorType);

if (secondComper != 0)
{
return secondComper;
}
else
{
int firstComper = FirstName.CompareTo(contact?.FirstName);

if (firstComper != 0)
{
return firstComper;
}
else
{
return LastName.CompareTo(contact?.LastName);
}
}
}
}
}
31 changes: 31 additions & 0 deletions HomeWork12/HomeWork12/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using HomeWork12.Repositories;
using HomeWork12.Repositories.Abstractions;
using HomeWork12.Services;
using HomeWork12.Services.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using System.Text;

namespace HomeWork12
{
internal class Program
{
static void Main(string[] args)
{
void ConfigreServices(ServiceCollection serviceCollection)
{
serviceCollection.AddTransient<IContactService, ContactService>()
.AddTransient<IContactRepository, ContactRepository>()
.AddTransient<StartUp>();
}

Console.OutputEncoding = UTF8Encoding.UTF8;

var serviceCollection = new ServiceCollection();
ConfigreServices(serviceCollection);
var provider = serviceCollection.BuildServiceProvider();

var start = provider.GetService<StartUp>();
start!.Start();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HomeWork12.Entities;

namespace HomeWork12.Repositories.Abstractions
{
internal interface IContactRepository
{
public Guid AddContact(ContactEntity contactEntity);
public List<ContactEntity> GetContacts();
}
}
32 changes: 32 additions & 0 deletions HomeWork12/HomeWork12/Repositories/ContactRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using HomeWork12.Entities;
using HomeWork12.Repositories.Abstractions;

namespace HomeWork12.Repositories
{
internal class ContactRepository : IContactRepository
{
private List<ContactEntity> _contacts = new List<ContactEntity>();

public Guid AddContact(ContactEntity contactEntity)
{
var contact = new ContactEntity()
{
Id = Guid.NewGuid(),
FirstName = contactEntity.FirstName,
LastName = contactEntity.LastName,
Number = contactEntity.Number,
CreateAt = contactEntity.CreateAt,
SectorType = contactEntity.SectorType
};

_contacts.Add(contact);

return contact.Id;
}

public List<ContactEntity> GetContacts()
{
return _contacts;
}
}
}
12 changes: 12 additions & 0 deletions HomeWork12/HomeWork12/Services/Abstractions/IContactService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using HomeWork12.Models;

namespace HomeWork12.Services.Abstractions
{
internal interface IContactService
{
public Guid AddContact(Contact contact);
public List<Contact> GetContacts();
public List<Contact> SortContacts();

}
}
80 changes: 80 additions & 0 deletions HomeWork12/HomeWork12/Services/ContactService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using HomeWork12.Entities;
using HomeWork12.Enum;
using HomeWork12.Models;
using HomeWork12.Repositories.Abstractions;
using HomeWork12.Services.Abstractions;
using System.Text.RegularExpressions;

namespace HomeWork12.Services
{
internal class ContactService : IContactService
{
private IContactRepository _contactRepository;

public ContactService(IContactRepository contactRepository)
{
_contactRepository = contactRepository;
}
public Guid AddContact(Contact contact)
{
var entity = new ContactEntity()
{
FirstName = contact.FirstName,
LastName = contact.LastName,
Number = contact.Number,
CreateAt = contact.CreateAt,
SectorType = GetDirectoryType(contact.FirstName, contact.LastName)
};

Guid id = _contactRepository.AddContact(entity);

return id;
}

public List<Contact> GetContacts()
{
var contacts = new List<Contact>();
var entities = _contactRepository.GetContacts();

foreach (var contact in entities)
{
contacts.Add(
new Contact()
{
Id = contact.Id,
FirstName = contact.FirstName,
LastName = contact.LastName,
Number = contact.Number,
CreateAt = contact.CreateAt,
SectorType = contact.SectorType
});
}

return contacts;
}

public List<Contact> SortContacts()
{
return GetContacts().SortContacts();
}

private DirectoryType GetDirectoryType(string firstName, string lastName)
{
Regex numberCheck = new Regex("[0-9]");

Regex symbolCheck = new Regex(@"[^0-9a-zа-яії ]", RegexOptions.IgnoreCase);

if (numberCheck.IsMatch(firstName) || numberCheck.IsMatch(lastName))
{
return DirectoryType.Number;
}

if (symbolCheck.IsMatch(firstName) || symbolCheck.IsMatch(lastName))
{
return DirectoryType.HeshTeg;
}

return DirectoryType.Alphabet;
}
}
}
82 changes: 82 additions & 0 deletions HomeWork12/HomeWork12/StartUp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using HomeWork12.Models;
using HomeWork12.Services.Abstractions;
using System.Globalization;

namespace HomeWork12
{
internal class StartUp
{
private readonly IContactService _contactService;

public StartUp(IContactService contactService)
{
_contactService = contactService;
}
public void Start()
{
UseCultur();

AddContact();

ShowContacts();
}

private void AddContact()
{
do
{
Console.WriteLine("enter name:");
var name = Console.ReadLine();

Console.WriteLine("enter lastName:");
var lastName = Console.ReadLine();

Console.WriteLine("enter number of phone");
var number = Console.ReadLine();

var newContact = new Contact()
{
FirstName = name,
LastName = lastName,
Number = number,
CreateAt = DateTime.UtcNow
};

_contactService.AddContact(newContact);

Console.WriteLine("Continue end construct? Y");
}
while (Console.ReadKey().Key != ConsoleKey.Y);

}

private void ShowContacts()
{
foreach (var item in _contactService.SortContacts())
{
Console.WriteLine(item.ToString());
}

}

private void UseCultur()
{
Console.WriteLine("Choose cultur language: Ukraine - U or English - E/ defoult en");

if(Console.ReadKey().Key == ConsoleKey.U)
{
CultureInfo.CurrentUICulture = new CultureInfo("uk-UA");
CultureInfo.CurrentCulture = new CultureInfo("uk-UA");
}
else
{
CultureInfo.CurrentUICulture = new CultureInfo("en-US");
CultureInfo.CurrentCulture = new CultureInfo("en-US");
}

Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
}
}