-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathPhone.cs
More file actions
80 lines (66 loc) · 2.13 KB
/
Phone.cs
File metadata and controls
80 lines (66 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
namespace ClassLibrary
{
public class Phone
{
private string owner;
// string of 9 digits
private string phoneNumber;
// Dictionary of <name, number>
private readonly Dictionary<string, string> phoneBook;
public int PhoneBookCapacity => 100;
public Phone(string owner, string phoneNumber)
{
Owner = owner;
PhoneNumber = phoneNumber;
phoneBook = new Dictionary<string, string>();
}
public string Owner
{
get => owner;
private set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException($"Owner name is empty or null!");
owner = value;
}
}
private bool IsCorrectPhoneNumber(string number)
=> (number != null) && number.Length == 9 && number.All(c => char.IsDigit(c));
public string PhoneNumber
{
get => phoneNumber;
private set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException($"Phone number is empty or null!");
if (!IsCorrectPhoneNumber(value))
throw new ArgumentException($"Invalid phone number!");
phoneNumber = value;
}
}
public int Count => phoneBook.Count;
public bool AddContact(string name, string number)
{
if (Count == PhoneBookCapacity)
throw new InvalidOperationException("Phonebook is full!");
if (!phoneBook.ContainsKey(name))
{
phoneBook.Add(name, number);
return true;
}
return false;
}
public string Call(string name)
{
if (phoneBook.ContainsKey(name))
{
var number = phoneBook[name];
return $"Calling {number} ({name}) ...";
}
throw new InvalidOperationException("Person doesn't exists!");
}
}
}