diff --git a/Phonebook/Phonebook.Test/Phonebook.Test.fsproj b/Phonebook/Phonebook.Test/Phonebook.Test.fsproj new file mode 100644 index 0000000..bb28560 --- /dev/null +++ b/Phonebook/Phonebook.Test/Phonebook.Test.fsproj @@ -0,0 +1,28 @@ + + + + net6.0 + + false + false + + + + + + + + + + + + + + + + + + + + + diff --git a/Phonebook/Phonebook.Test/PhonebookTest.fs b/Phonebook/Phonebook.Test/PhonebookTest.fs new file mode 100644 index 0000000..f8f6c25 --- /dev/null +++ b/Phonebook/Phonebook.Test/PhonebookTest.fs @@ -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"}] + +[] +let addTest () = + let book = addPhone "Oleg" "12345" book + + book |> should contain {Name = "Oleg"; Phone = "12345"} + +[] +let findNameTest () = + let name = findNameByPhone "456" book + let name1 = findNameByPhone "1" book + + name |> should equal ["Ele"] + name1 |> should be Empty + +[] +let findPhoneTest () = + let phone = findPhoneByName "Ele" book + let phone1 = findPhoneByName "Ele1" book + + phone |> should equal ["456"] + phone1 |> should be Empty + +[] +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 \ No newline at end of file diff --git a/Phonebook/Phonebook.Test/Program.fs b/Phonebook/Phonebook.Test/Program.fs new file mode 100644 index 0000000..176a7b6 --- /dev/null +++ b/Phonebook/Phonebook.Test/Program.fs @@ -0,0 +1,4 @@ +module Program = + + [] + let main _ = 0 diff --git a/Phonebook/Phonebook.Test/Test.txt b/Phonebook/Phonebook.Test/Test.txt new file mode 100644 index 0000000..e69de29 diff --git a/Phonebook/Phonebook.sln b/Phonebook/Phonebook.sln new file mode 100644 index 0000000..5cb7a50 --- /dev/null +++ b/Phonebook/Phonebook.sln @@ -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 diff --git a/Phonebook/Phonebook/Phonebook.fsproj b/Phonebook/Phonebook/Phonebook.fsproj new file mode 100644 index 0000000..4f83377 --- /dev/null +++ b/Phonebook/Phonebook/Phonebook.fsproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + Windows + + + + + + + + diff --git a/Phonebook/Phonebook/PhonebookFunc.fs b/Phonebook/Phonebook/PhonebookFunc.fs new file mode 100644 index 0000000..3fe1070 --- /dev/null +++ b/Phonebook/Phonebook/PhonebookFunc.fs @@ -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 + +// запись в файл +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 "Файл не найден.") + + + \ No newline at end of file diff --git a/Phonebook/Phonebook/Program.fs b/Phonebook/Phonebook/Program.fs new file mode 100644 index 0000000..64719a9 --- /dev/null +++ b/Phonebook/Phonebook/Program.fs @@ -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 [] \ No newline at end of file