-
Notifications
You must be signed in to change notification settings - Fork 0
Phonebook #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MikePuzanov
wants to merge
5
commits into
master
Choose a base branch
from
Phonebook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Phonebook #6
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} | ||
|
|
||
| [<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> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module Program = | ||
|
|
||
| [<EntryPoint>] | ||
| let main _ = 0 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 "Файл не найден.") | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 [] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
здесь было бы лучше проверить весь справочник. Вдруг addPhone перетирает остальные записи?