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
28 changes: 28 additions & 0 deletions Phonebook/Phonebook.Test/Phonebook.Test.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="PhonebookTest.fs" />
<Compile Include="Program.fs" />
<Content Include="Test.txt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsUnit" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Phonebook\Phonebook.fsproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions Phonebook/Phonebook.Test/PhonebookTest.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Phonebook.Test

open System.IO
open NUnit.Framework
open FsUnit
open PhoneBookFunc

let book = [{Name = "Ulu"; Phone = "123"}
{Name = "Ele"; Phone = "456"}
{Name = "Yly"; Phone = "789"}]

[<Test>]
let addTest () =
let book = addPhone "Oleg" "12345" book

book |> should contain {Name = "Oleg"; Phone = "12345"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь было бы лучше проверить весь справочник. Вдруг addPhone перетирает остальные записи?


[<Test>]
let findNameTest () =
let name = findNameByPhone "456" book
let name1 = findNameByPhone "1" book

name |> should equal ["Ele"]
name1 |> should be Empty

[<Test>]
let findPhoneTest () =
let phone = findPhoneByName "Ele" book
let phone1 = findPhoneByName "Ele1" book

phone |> should equal ["456"]
phone1 |> should be Empty

[<Test>]
let fileTest () =
writeToFile "Test.txt" book

readFromFile "Test.txt" |> should equal [{Name = "Yly"; Phone = "789"}; {Name = "Ele"; Phone = "456"}; {Name = "Ulu"; Phone = "123"}]
(fun () -> readFromFile "123.txt" |> ignore) |> should throw typeof<System.IO.FileNotFoundException>
4 changes: 4 additions & 0 deletions Phonebook/Phonebook.Test/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Program =

[<EntryPoint>]
let main _ = 0
Empty file.
22 changes: 22 additions & 0 deletions Phonebook/Phonebook.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Phonebook", "Phonebook\Phonebook.fsproj", "{0A348E4D-6709-4B56-87D1-4DC9FE4EF75D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Phonebook.Test", "Phonebook.Test\Phonebook.Test.fsproj", "{D18B0338-8259-4FDC-A332-488EACB4C5E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0A348E4D-6709-4B56-87D1-4DC9FE4EF75D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A348E4D-6709-4B56-87D1-4DC9FE4EF75D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A348E4D-6709-4B56-87D1-4DC9FE4EF75D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A348E4D-6709-4B56-87D1-4DC9FE4EF75D}.Release|Any CPU.Build.0 = Release|Any CPU
{D18B0338-8259-4FDC-A332-488EACB4C5E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D18B0338-8259-4FDC-A332-488EACB4C5E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D18B0338-8259-4FDC-A332-488EACB4C5E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D18B0338-8259-4FDC-A332-488EACB4C5E9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions Phonebook/Phonebook/Phonebook.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<Compile Include="PhonebookFunc.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions Phonebook/Phonebook/PhonebookFunc.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module PhoneBookFunc

open System.IO

// справочник
type BookOfNumber =
{Name : string; Phone : string}

// добавление контакта
let addPhone name phone book =
{Name = name; Phone = phone} :: book

// поиск имени по телефону
let rec findNameByPhone phone (book : BookOfNumber list) =
book |> List.filter (fun {Phone = x} -> x = phone) |> List.map(fun {Name = x} -> x)

// поиск телефона по имени
let rec findPhoneByName name (book : BookOfNumber list) =
let getSecond ({Name = a; Phone = b} : BookOfNumber) = b
book |> List.filter (fun {Name = x; Phone = _} -> x = name) |> List.map(getSecond)

// вывести все контакты
let rec printAll book =
let person (book : BookOfNumber list) = book.Head
if (not(book |> List.isEmpty)) then
printf "%s %s\n" (person book).Name (person book).Phone
printAll book.Tail
Comment on lines +24 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

проще было бы через паттерн-матчинг со списком записать


// запись в файл
let writeToFile (path : string) book =
use writer = new StreamWriter(path)
book |> List.iter (fun {Name = x; Phone = y} -> (writer.WriteLine $"{x} {y}"))

// считать с файла
let readFromFile path =
let lines path = File.ReadAllLines(path) |> Array.toList
let split (line : string) = line.Split ' '
let getFirst (arr : string array) = arr |> Array.item 0
let getSecond arr = arr |> Array.item 1
if (File.Exists(path)) then
let rec read list book =
match list with
| h :: tail ->
let line = split h
let book = addPhone (getFirst line) (getSecond line) book
read tail book
| [] -> book
read (lines path) []
else raise (FileNotFoundException "Файл не найден.")



57 changes: 57 additions & 0 deletions Phonebook/Phonebook/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
open System
open PhoneBookFunc

let rule () =
printfn
"%s"
"0 - выйти;\n\
1 - добавить запись (имя и телефон);\n\
2 - найти телефон по имени;\n\
3 - найти имя по телефону;\n\
4 - вывести всё текущее содержимое базы;\n\
5 - сохранить текущие данные в файл;\n\
6 - считать данные из файла."

let getCommand () =
printf"%s" "\nВведите команду - "
Console.ReadLine()

let getName () =
printf "%s" "Введите имя - "
Console.ReadLine()

let getPhone () =
printf "%s" "Введите номер - "
Console.ReadLine()

let getPath () =
printf "%s" "Введите путь - "
Console.ReadLine()

let tryAgain () =
printf "%s" "Попробуйте еще раз."

let rec program book =
let command = getCommand ()
match command with
| "0" -> ()
| "1" -> book |> addPhone (getName ()) (getPhone ()) |> program
| "2" ->
(findPhoneByName (getName()) book) |> List.iter(fun x -> printf "%s" x)
program book
| "3" ->
(findNameByPhone (getPhone ()) book) |> List.iter(fun x -> printf "%s" x)
program book
| "4" ->
printAll book
program book
| "5" ->
writeToFile (getPath ()) book
program book
| "6" ->
program (readFromFile (getPath()))
| _ ->
tryAgain ()
program book
rule ()
program []