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
16 changes: 13 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -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%
22 changes: 22 additions & 0 deletions semester4/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", "{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
34 changes: 34 additions & 0 deletions semester4/PhoneBook/PhoneBook/PhoneBook.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module PhoneBook

open System
open System.IO

/// Simple phonebook with names and phone numbers
type PhoneBook =

Choose a reason for hiding this comment

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

Тип с только статическими методами --- это же буквально модуль, модули именно в это и компилятся. Сделали б module PhoneBook = ... или, что то же самое, просто module 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))
12 changes: 12 additions & 0 deletions semester4/PhoneBook/PhoneBook/PhoneBook.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="PhoneBook.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions semester4/PhoneBook/PhoneBook/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
open System
open System.IO
open PhoneBook

[<EntryPoint>]
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<string * string>
71 changes: 71 additions & 0 deletions semester4/PhoneBook/PhoneBookTests/PhoneBookTest.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module TestProject1

open NUnit.Framework
open FsUnit
open PhoneBook

[<Test>]
let ``add test`` () =
let seq = PhoneBook.Add "Hello" "213-222-555" Seq.empty<string * string>
seq |> should contain ("Hello", "213-222-555")

[<Test>]
let ``add two equal test`` () =
let seq = Seq.empty<string * string> |> PhoneBook.Add "hey" "213-222-555"
|> PhoneBook.Add "hey" "213-222-555"
Seq.length seq |> should equal 1

[<Test>]
let ``add two test`` () =
let seq = Seq.empty<string * string> |> 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")

[<Test>]
let ``search name test`` () =
let seq = Seq.empty<string * string> |> 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

[<Test>]
let ``search number test`` () =
let seq = Seq.empty<string * string> |> 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

[<Test>]
let ``search name test two matches`` () =
let seq = Seq.empty<string * string> |> 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

[<Test>]
let ``search number test two matches`` () =
let seq = Seq.empty<string * string> |> 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

[<Test>]
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

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

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<RootNamespace>TestProject1</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FsCheck" Version="3.0.0-alpha4" />
<PackageReference Include="FsUnit" Version="4.0.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

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

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

</Project>
1 change: 1 addition & 0 deletions semester4/PhoneBook/PhoneBookTests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Program = let [<EntryPoint>] main _ = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ivan.....111-222-333
Ivanov.....123-456-222