-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment6.dart
152 lines (135 loc) · 3.5 KB
/
Assignment6.dart
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'dart:io';
class Contact {
List<List<String>> allContacts = [];
String name = "";
String phone = "0";
String email = "";
String dateOfBirth = "";
Contact() {}
checkPhoneBook() {
if (this.allContacts.isEmpty) {
print("PhoneBook is empty!! \n");
return true;
}
}
addContact() {
print("\nPlease provide details: \n");
print("Enter Name: ");
this.name = stdin.readLineSync()!;
bool correctPhone = false;
while (!correctPhone) {
print("\nEnter Phone number: ");
this.phone = stdin.readLineSync()!;
if (phone.length == 11) {
correctPhone = true;
} else {
print("Invalid Phone Number!");
continue;
}
}
bool correctEmail = false;
while (!correctEmail) {
print("\nEnter email: ");
this.email = stdin.readLineSync()!;
if (email.contains("@")) {
correctEmail = true;
} else {
print("Invalid Email!");
continue;
}
}
print("\nEnter Birth details: \n");
print("Enter year of birth: ");
String year = stdin.readLineSync()!;
print("\nEnter month of birth: ");
String month = stdin.readLineSync()!;
print("\nEnter date of birth: ");
String date = stdin.readLineSync()!;
this.dateOfBirth = month + date + ", " + year;
this.allContacts.add([this.name, this.phone, this.email, this.dateOfBirth]);
}
updateContact() {
if (this.allContacts.isEmpty) {
print("PhoneBook is empty!! \n");
return;
}
print("Enter the index of contact: \n");
String index = stdin.readLineSync()!;
this.allContacts.removeAt(int.parse(index));
addContact();
print("\nContact updated successully!! \n");
}
displayContact() {
if (this.allContacts.isEmpty) {
print("PhoneBook is empty!! \n");
return;
}
for (var contact in this.allContacts) {
print("Name: ${contact[0]}");
print("Phone: ${contact[1]}");
print("Email: ${contact[2]}");
print("Date Of Birth: ${contact[3]}\n");
}
}
deleteContact() {
if (checkPhoneBook()) {
return;
}
print("Enter the index of contact: \n");
String index = stdin.readLineSync()!;
this.allContacts.removeAt(int.parse(index));
print("\nContact deleted successfully!!\n");
}
}
void main() {
bool chaltaRahy = true;
String choice = "";
List<String> contacts = [];
Contact contact_1 = Contact();
print("===== Welcome to Contact Book =====\n");
Mainloop:
while (chaltaRahy) {
print("=========== Main Menu ===========\n");
print('''
1: Add Contact
2: Update Contact
3: List All Contacts
4: Delete a Contact
5: Exit\n
Please enter your choice:
''');
try {
choice = stdin.readLineSync()!;
} catch (e) {
print(e);
}
switch (choice) {
case "1":
{
contact_1.addContact();
print("\nContact added to phone book successfully!!\n");
break;
}
case "2":
{
contact_1.updateContact();
break;
}
case "3":
{
contact_1.displayContact();
break;
}
case "4":
{
contact_1.deleteContact();
break;
}
case "5":
{
print("Exiting from phoneBook.......");
break Mainloop;
}
}
}
}