-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMember.py
More file actions
56 lines (48 loc) · 1.86 KB
/
Member.py
File metadata and controls
56 lines (48 loc) · 1.86 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
class Member:
"""
Represents a library member with name, phone, and email.
"""
def __init__(self, name, phone, email):
# Strip input and check if valid
name = name.strip() if name else ""
phone = phone.strip() if phone else ""
email = email.strip() if email else ""
# Validation of inputs
if not name or not phone or not email:
raise ValueError("One of the inputs is None, empty, or contains only spaces!")
elif not all(c.isalpha() or c.isspace() for c in name):
raise ValueError("Name must contain only letters and spaces.")
elif not phone.isdigit() or len(phone) != 10:
raise ValueError("Not a legal phone number.")
elif not email.endswith("@gmail.com") or " " in email:
raise ValueError("Not a legal email address, use gmail, with no spaces.")
else:
self.name = name
self.phone = phone
self.email = email
self.notification = []
def __str__(self):
# Return member info as a string
return f"Name: {self.name} | Phone: {self.phone} | Email: {self.email}"
def update(self, text):
# Add a new notification to the member
self.notification.append(text)
print(f"New message in '{self.name}' Notifications: {text}\n")
class MemberIterator:
"""
Iterator for iterating through a list of members.
"""
def __init__(self, members):
self._members = members
self._index = 0
def __iter__(self):
# Return the iterator object itself
return self
def __next__(self):
# Get the next member or raise StopIteration
if self._index < len(self._members):
member = self._members[self._index]
self._index += 1
return member
else:
raise StopIteration