From b2eaafd750a2a8c780f8a6d35ee2f8812a5d1440 Mon Sep 17 00:00:00 2001 From: dmitry Date: Sat, 22 Aug 2020 20:46:27 +0300 Subject: [PATCH 1/2] Added all files --- appveyor.yml | 16 +++- semester4/PhoneBook/PhoneBook/PhoneBook.fs | 34 +++++++++ .../PhoneBook/PhoneBook/PhoneBook.fsproj | 12 +++ semester4/PhoneBook/PhoneBook/Program.fs | 75 +++++++++++++++++++ .../bin/Debug/netcoreapp3.1/TestFile.txt | 2 + 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 semester4/PhoneBook/PhoneBook/PhoneBook.fs create mode 100644 semester4/PhoneBook/PhoneBook/PhoneBook.fsproj create mode 100644 semester4/PhoneBook/PhoneBook/Program.fs create mode 100644 semester4/PhoneBook/PhoneBookTests/bin/Debug/netcoreapp3.1/TestFile.txt diff --git a/appveyor.yml b/appveyor.yml index 855f34f..24b8be2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,17 @@ -image: Visual Studio 2017 +image: Visual Studio 2019 + +init: + - git config --global core.autocrlf true + +environment: + matrix: + - solution: Semester4/PhoneBook/PhoneBook.sln before_build: - - nuget restore semester2/6.1/HW6T2.sln + - nuget restore %solution% build: - project: semester2/2.3/2.3.sln + project: $(solution) + +test_script: + - dotnet test %solution% diff --git a/semester4/PhoneBook/PhoneBook/PhoneBook.fs b/semester4/PhoneBook/PhoneBook/PhoneBook.fs new file mode 100644 index 0000000..6c35cdd --- /dev/null +++ b/semester4/PhoneBook/PhoneBook/PhoneBook.fs @@ -0,0 +1,34 @@ +module PhoneBook + +open System +open System.IO + +/// Simple phonebook with names and phone numbers +type PhoneBook = + + /// Add new record + static member Add name number seq = + if not <| Seq.contains (name, number) seq then + Seq.append seq [(name, number)] + else seq + + /// Get name of person by its telephone number + static member GetName number seq = + Seq.filter(fun x -> snd x = number) seq + + /// Get telephone number of person by its name + static member GetNumber name seq = + Seq.filter(fun x -> fst x = name) seq + + /// Read some records from file + static member ReadFromFile filePath seq = + File.ReadLines (filePath) + |> Seq.filter(fun x -> x |> System.String.IsNullOrWhiteSpace |> not) + |> Seq.map(fun x -> x.Split (".....", StringSplitOptions.RemoveEmptyEntries)) + |> Seq.map(fun x -> (x.[0], x.[1])) + |> Seq.filter(fun x -> Seq.contains x seq |> not) + |> Seq.append seq + + /// Write all your records in file + static member WriteToFile filePath seq = + File.WriteAllLines(filePath, seq |> Seq.map(fun x -> fst x + "....." + snd x)) \ No newline at end of file diff --git a/semester4/PhoneBook/PhoneBook/PhoneBook.fsproj b/semester4/PhoneBook/PhoneBook/PhoneBook.fsproj new file mode 100644 index 0000000..5296847 --- /dev/null +++ b/semester4/PhoneBook/PhoneBook/PhoneBook.fsproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + \ No newline at end of file diff --git a/semester4/PhoneBook/PhoneBook/Program.fs b/semester4/PhoneBook/PhoneBook/Program.fs new file mode 100644 index 0000000..f7f35e6 --- /dev/null +++ b/semester4/PhoneBook/PhoneBook/Program.fs @@ -0,0 +1,75 @@ +open System +open System.IO +open PhoneBook + +[] +let main argv = + /// Main menu of phonebook + let rec menu (seq) = + match Console.ReadLine () with + | "1" -> 0 + | "2" -> + printfn "Please enter name" + let name = Console.ReadLine () + printfn "Please enter phone" + let number = Console.ReadLine () + printfn "Done!" + menu (PhoneBook.Add name number seq) + | "3" -> + printfn "Please enter name to search" + let name = Console.ReadLine () + let answer = PhoneBook.GetNumber name seq + if Seq.length answer = 0 then + printfn "Sorry, name not found" + menu seq + else + Seq.iter (fun x -> x |> snd |> printfn "Number is: %A") answer + menu seq + | "4" -> + printfn "Please enter phone to search" + let number = Console.ReadLine () + let answer = PhoneBook.GetName number seq + if Seq.length answer = 0 then + printfn "Sorry, number not found" + menu seq + else + Seq.iter (fun x -> x |> fst |> printfn "Name is: %A") answer + menu seq + | "5" -> + printfn "Your data:" + seq |> Seq.iter(fun (name, number) -> printfn "%A %A" name number) + menu seq + | "6" -> + printfn "Please enter file path" + let path = Console.ReadLine () + PhoneBook.WriteToFile path seq + printfn "Done!" + menu seq + | "7" -> + printfn "Please enter file path" + let path = Console.ReadLine () + try + let answer = PhoneBook.ReadFromFile path seq + printfn "Done!" + answer |> menu + with + | :? FileNotFoundException -> + printfn "File not found!" + menu seq + | :? ArgumentException -> + printfn "Something bad with file path" + menu seq + | _ -> + printfn "IO Exception!" + menu seq + | _ -> menu seq + + printfn "Welcome to the PhoneBook!" + printfn "Press 1 to exit" + printfn "Press 2 to add data" + printfn "Press 3 to find number by name" + printfn "Press 4 to find name by number" + printfn "Press 5 to print all data" + printfn "Press 6 to save all data in file" + printfn "Press 7 to input data from file" + menu <| Seq.empty \ No newline at end of file diff --git a/semester4/PhoneBook/PhoneBookTests/bin/Debug/netcoreapp3.1/TestFile.txt b/semester4/PhoneBook/PhoneBookTests/bin/Debug/netcoreapp3.1/TestFile.txt new file mode 100644 index 0000000..a225b5d --- /dev/null +++ b/semester4/PhoneBook/PhoneBookTests/bin/Debug/netcoreapp3.1/TestFile.txt @@ -0,0 +1,2 @@ +Ivan.....111-222-333 +Ivanov.....123-456-222 \ No newline at end of file From 33012d856cdf61b6584c113db9f811dfb310d933 Mon Sep 17 00:00:00 2001 From: dmitry Date: Sat, 22 Aug 2020 20:50:33 +0300 Subject: [PATCH 2/2] Add some files --- semester4/PhoneBook/PhoneBook.sln | 22 ++++++ .../PhoneBook/PhoneBookTests/PhoneBookTest.fs | 71 +++++++++++++++++++ .../PhoneBookTests/PhoneBookTests.fsproj | 28 ++++++++ semester4/PhoneBook/PhoneBookTests/Program.fs | 1 + 4 files changed, 122 insertions(+) create mode 100644 semester4/PhoneBook/PhoneBook.sln create mode 100644 semester4/PhoneBook/PhoneBookTests/PhoneBookTest.fs create mode 100644 semester4/PhoneBook/PhoneBookTests/PhoneBookTests.fsproj create mode 100644 semester4/PhoneBook/PhoneBookTests/Program.fs diff --git a/semester4/PhoneBook/PhoneBook.sln b/semester4/PhoneBook/PhoneBook.sln new file mode 100644 index 0000000..8a81b58 --- /dev/null +++ b/semester4/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", "{79F13619-3F30-4C58-ADA5-3ED232855DE0}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "PhoneBookTests", "PhoneBookTests\PhoneBookTests.fsproj", "{83419304-CBAE-4658-8FB7-85AC0545BC76}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {79F13619-3F30-4C58-ADA5-3ED232855DE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79F13619-3F30-4C58-ADA5-3ED232855DE0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79F13619-3F30-4C58-ADA5-3ED232855DE0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79F13619-3F30-4C58-ADA5-3ED232855DE0}.Release|Any CPU.Build.0 = Release|Any CPU + {83419304-CBAE-4658-8FB7-85AC0545BC76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83419304-CBAE-4658-8FB7-85AC0545BC76}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83419304-CBAE-4658-8FB7-85AC0545BC76}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83419304-CBAE-4658-8FB7-85AC0545BC76}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/semester4/PhoneBook/PhoneBookTests/PhoneBookTest.fs b/semester4/PhoneBook/PhoneBookTests/PhoneBookTest.fs new file mode 100644 index 0000000..9b539ef --- /dev/null +++ b/semester4/PhoneBook/PhoneBookTests/PhoneBookTest.fs @@ -0,0 +1,71 @@ +module TestProject1 + +open NUnit.Framework +open FsUnit +open PhoneBook + +[] +let ``add test`` () = + let seq = PhoneBook.Add "Hello" "213-222-555" Seq.empty + seq |> should contain ("Hello", "213-222-555") + +[] +let ``add two equal test`` () = + let seq = Seq.empty |> PhoneBook.Add "hey" "213-222-555" + |> PhoneBook.Add "hey" "213-222-555" + Seq.length seq |> should equal 1 + +[] +let ``add two test`` () = + let seq = Seq.empty |> PhoneBook.Add "Hello" "213-222-555" + |> PhoneBook.Add "hey" "213-222-556" + seq |> should contain ("Hello", "213-222-555") + seq |> should contain ("hey", "213-222-556") + +[] +let ``search name test`` () = + let seq = Seq.empty |> PhoneBook.Add "Hello" "213-222-555" + |> PhoneBook.Add "hey" "213-222-556" + let answer = PhoneBook.GetName "213-222-555" seq + Seq.length answer |> should equal 1 + Seq.contains ("Hello", "213-222-555") answer |> should equal true + +[] +let ``search number test`` () = + let seq = Seq.empty |> PhoneBook.Add "Hello" "213-222-555" + |> PhoneBook.Add "hey" "213-222-556" + let answer = PhoneBook.GetNumber "hey" seq + Seq.length answer |> should equal 1 + Seq.contains ("hey", "213-222-556") answer |> should equal true + +[] +let ``search name test two matches`` () = + let seq = Seq.empty |> PhoneBook.Add "Hello" "213-222-555" + |> PhoneBook.Add "hey" "213-222-556" + |> PhoneBook.Add "Hello again" "213-222-555" + let answer = PhoneBook.GetName "213-222-555" seq + Seq.length answer |> should equal 2 + Seq.contains ("Hello", "213-222-555") answer |> should equal true + Seq.contains ("Hello again", "213-222-555") answer |> should equal true + +[] +let ``search number test two matches`` () = + let seq = Seq.empty |> PhoneBook.Add "Ivan" "213-222-555" + |> PhoneBook.Add "Ivan" "213-222-556" + |> PhoneBook.Add "Ivan Ivanov" "213-222-555" + let answer = PhoneBook.GetNumber "Ivan" seq + Seq.length answer |> should equal 2 + Seq.contains ("Ivan", "213-222-555") answer |> should equal true + Seq.contains ("Ivan", "213-222-556") answer |> should equal true + +[] +let ``read from file to empty sequence test`` () = + let data = PhoneBook.ReadFromFile "TestFile.txt" Seq.empty + Seq.contains ("Ivan", "111-222-333") data |> should equal true + Seq.contains ("Ivanov", "123-456-222") data |> should equal true + +[] +let ``read from file to sequence test`` () = + let data = Seq.empty |> PhoneBook.Add "Badnames" "132-132-132"|> PhoneBook.ReadFromFile "TestFile.txt" + Seq.contains ("Ivan", "111-222-333") data |> should equal true + Seq.contains ("Badnames", "132-132-132") data |> should equal true \ No newline at end of file diff --git a/semester4/PhoneBook/PhoneBookTests/PhoneBookTests.fsproj b/semester4/PhoneBook/PhoneBookTests/PhoneBookTests.fsproj new file mode 100644 index 0000000..2460b14 --- /dev/null +++ b/semester4/PhoneBook/PhoneBookTests/PhoneBookTests.fsproj @@ -0,0 +1,28 @@ + + + + netcoreapp3.1 + + false + false + TestProject1 + + + + + + + + + + + + + + + + + + + + diff --git a/semester4/PhoneBook/PhoneBookTests/Program.fs b/semester4/PhoneBook/PhoneBookTests/Program.fs new file mode 100644 index 0000000..0695f84 --- /dev/null +++ b/semester4/PhoneBook/PhoneBookTests/Program.fs @@ -0,0 +1 @@ +module Program = let [] main _ = 0