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 HomeWork14/HomeWork14/HomeWork14.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}") = "HomeWork14", "HomeWork14\HomeWork14.csproj", "{DF0A9DBE-E8FD-4124-8E16-85303AE7F29F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DF0A9DBE-E8FD-4124-8E16-85303AE7F29F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF0A9DBE-E8FD-4124-8E16-85303AE7F29F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF0A9DBE-E8FD-4124-8E16-85303AE7F29F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF0A9DBE-E8FD-4124-8E16-85303AE7F29F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5E067C7F-01E4-4189-A2AF-BF9DD567A01F}
EndGlobalSection
EndGlobal
32 changes: 32 additions & 0 deletions HomeWork14/HomeWork14/HomeWork14/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace HomeWork14
{
internal class Calculator
{
public event CalculatorHandler? calculatorHandler;

public int Caculate(int x, int y)
{
int result = WrapperMethod((a, b) => a + b, x, y);

calculatorHandler?.Invoke($"Sum nuber {x} + {y} = {result}");

return result;
}

private int WrapperMethod(Func<int, int, int> func, int x, int y)
{
int result = 0;

try
{
result = func(x, y);
}
catch (Exception ex)
{
calculatorHandler?.Invoke($"some error: {ex.Message}");
}

return result;
}
}
}
10 changes: 10 additions & 0 deletions HomeWork14/HomeWork14/HomeWork14/HomeWork14.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
19 changes: 19 additions & 0 deletions HomeWork14/HomeWork14/HomeWork14/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace HomeWork14
{
public delegate void CalculatorHandler(string message);

internal class Program
{
static void Main(string[] args)
{
Calculator calculator = new Calculator();

calculator.calculatorHandler += SendFirst.Send;

calculator.calculatorHandler += SendSecond.Send;

calculator.Caculate(10, 5);

}
}
}
10 changes: 10 additions & 0 deletions HomeWork14/HomeWork14/HomeWork14/SendFirst.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HomeWork14
{
internal class SendFirst
{
public static void Send(string message)
{
Console.WriteLine($"First message: {message}");
}
}
}
10 changes: 10 additions & 0 deletions HomeWork14/HomeWork14/HomeWork14/SendSecond.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HomeWork14
{
internal class SendSecond
{
public static void Send(string message)
{
Console.WriteLine($"Second message: {message}");
}
}
}
25 changes: 25 additions & 0 deletions HomeWork14/HomeWork14LINQ/HomeWork14LINQ.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}") = "HomeWork14LINQ", "HomeWork14LINQ\HomeWork14LINQ.csproj", "{03C7EBF5-BF77-4A3E-8E05-BAF8E06FE888}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{03C7EBF5-BF77-4A3E-8E05-BAF8E06FE888}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03C7EBF5-BF77-4A3E-8E05-BAF8E06FE888}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03C7EBF5-BF77-4A3E-8E05-BAF8E06FE888}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03C7EBF5-BF77-4A3E-8E05-BAF8E06FE888}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3D029F2D-389F-453B-AEF5-2555B68102FD}
EndGlobalSection
EndGlobal
49 changes: 49 additions & 0 deletions HomeWork14/HomeWork14LINQ/HomeWork14LINQ/Contact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace HomeWork14LINQ
{
public class Contact
{
public string Name { get; set; }
public int Age { get; set; }
public string Number { get; set; }

public static string GetName()
{
var listName = new List<string>
{
"Anton", "Valera", "Dima", "Dinis",
"Vitalic", "Oleg", "Maxim", "Vlad",
"Yaroslav", "Alex"
};

int rendomndex = new Random().Next(0, listName.Count);

string name = listName.ElementAt(rendomndex);

return name;
}

public static string GetNumber()
{
string shablon = $"+38({new Random().Next(100, 1000)})-{new Random().Next(100, 1000)}-{new Random().Next(10, 100)}-{new Random().Next(10, 100)}";
return shablon;
}

public static int GetAge()
{
return new Random().Next(1, 100);
}

public static void ShowContacts(List<Contact> contacts)
{
foreach (var contact in contacts)
{
Console.WriteLine($"Name: {contact.Name}, Age: {contact.Age}, Number: {contact.Number}");
}
}
public override string ToString()
{
return $"Name: {Name ?? "none"}, Age: {Age}, Number: {Number ?? "none"}";

}
}
}
10 changes: 10 additions & 0 deletions HomeWork14/HomeWork14LINQ/HomeWork14LINQ/HomeWork14LINQ.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
119 changes: 119 additions & 0 deletions HomeWork14/HomeWork14LINQ/HomeWork14LINQ/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
namespace HomeWork14LINQ
{
internal class Program
{
static void Main(string[] args)
{
/*
Створіть список контактів і спробуйте всі основні методи LINQ
FirstOrDefault, Where, Select і ще 3
*/

var listContacts = GetRandomContacts();

ShowBaseList(listContacts);

FirstOfDefaultContact(listContacts);

WhereAge(listContacts);

SelectName(listContacts);

OrderNameAge(listContacts);

FirstAge(listContacts);

TakeWhileAD(listContacts);
}

private static void TakeWhileAD(List<Contact> listContacts)
{
Console.WriteLine(".............TakeWhile...........Name [0] == A");

var takeWhileList = listContacts?.OrderBy(x => x.Name).TakeWhile(x => x.Name[0] == 'A' || x.Name[0] == 'D').ToList();

Contact.ShowContacts(takeWhileList);
}
private static void FirstAge(List<Contact> listContacts)
{
Console.WriteLine(".............First...........Age = 18");

var firstNameAge = new Contact();

try
{
firstNameAge = listContacts.First(y => y.Age == 18);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.WriteLine("First Age 18 = " + firstNameAge?.ToString());
}

private static void OrderNameAge(List<Contact> listContacts)
{
Console.WriteLine(".............OrderBy...........Name,Age");

var orderByList = listContacts.OrderBy(x => x.Name).ThenBy(x => x.Age).ToList();

Contact.ShowContacts(orderByList);
}

private static void SelectName(List<Contact> listContacts)
{
Console.WriteLine(".............Select...........Name");

var selectList = listContacts.Select(x => x.Number);

foreach (var number in selectList)
{
Console.WriteLine(number);
}
}

private static List<Contact> GetRandomContacts()
{
var listContacts = new List<Contact>();


for (int i = 0; i <= 10; i++)
{
listContacts.Add(new Contact()
{
Name = Contact.GetName(),
Number = Contact.GetNumber(),
Age = Contact.GetAge()
});
}

return listContacts;
}

private static void ShowBaseList(List<Contact> listContacts)
{
Console.WriteLine(".............Base list...........");

Contact.ShowContacts(listContacts);
}

private static void FirstOfDefaultContact(List<Contact> listContacts)
{
Console.WriteLine(".............FirstOfDefault...........Name == Anton");

var firstOrDefaultList = listContacts?.FirstOrDefault(x => x.Name == "Anton");

Console.WriteLine("FirstOFDefault = " + (firstOrDefaultList?.ToString() ?? "Not finded Contact"));
}

private static void WhereAge(List<Contact> listContacts)
{
Console.WriteLine(".............Where...........Age >= 30");

var whereList = listContacts.Where(x => x.Age >= 30).ToList();

Contact.ShowContacts(whereList);
}
}
}