-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterface.cpp
145 lines (130 loc) · 2.57 KB
/
Interface.cpp
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
#include "Interface.h"
using namespace std;
void welcome(void)
{
cout << "Welcome to use etnAtker's telephone directory!\nInitializing..." << endl;
}
TelDir* init(void)
{
cout << "Loading save file..." << endl;
fstream sav("data.sav", ios::in);
TelDir *dir;
if (sav) {
dir = new TelDir(sav);
cout << "Success." << endl;
} else {
cout << "No save file." << endl;
dir = new TelDir();
}
system("pause");
return dir;
}
void mainLoop(TelDir *dir)
{
menu();
readChoise(dir);
}
void menu(void)
{
system("cls");
cout << "**************MENU**************" << endl;
cout << "1. Add an entry to the directory." << endl;
cout << "2. Show all entries." << endl;
cout << "3. Search entries by name." << endl;
cout << "4. Delete entries by name." << endl;
cout << "5. Modify entries by name." << endl;
cout << "6. Search entries intelligently(by the first character)." << endl;
cout << "0. Save & exit.\n" << endl;
cout << "Your choise:" << endl;
}
void readChoise(TelDir *dir)
{
int op = getNumber(0, 6);
switch (op) {
case 0:
quitAndSave(dir);
case 1:
addNewEntry(dir);
return;
case 2:
dir->showTelDir();
system("pause");
return;
case 3:
showEntryByName(dir);
return;
case 4:
delEntryByName(dir);
return;
case 5:
modifyEntryByName(dir);
return;
case 6:
showEntryByFirstChar(dir);
return;
}
}
void addNewEntry(TelDir *dir)
{
system("cls");
string name, telNumber;
cout << "Name:";
inputLineFromCin(name);
checkName(name);
cout << "\nTelephone number:";
inputLineFromCin(telNumber);
checkName(telNumber);
if (!dir->addNewEntry(name, telNumber)) {
cout << "Failed to allocate memery." << endl;
} else {
cout << "Success" << endl;
}
system("pause");
}
void showEntryByName(TelDir *dir)
{
string name;
cout << "Name:";
inputLineFromCin(name);
dir->showEntryByName(name);
system("pause");
}
void delEntryByName(TelDir *dir)
{
string name;
cout << "Name:";
inputLineFromCin(name);
dir->delEntryByName(name);
system("pause");
}
void modifyEntryByName(TelDir *dir)
{
string name;
cout << "Name:";
inputLineFromCin(name);
dir->modifyEntryByName(name);
system("pause");
}
void showEntryByFirstChar(TelDir *dir)
{
string name;
cout << "First character(the first character will be taken if you input more than one):";
inputLineFromCin(name);
dir->showEntryByName(name[0]);
system("pause");
}
void quitAndSave(TelDir *dir)
{
fstream sav("data.sav", ios::out);
if (sav) {
dir->save(sav);
delete dir;
exit(0);
} else {
cout << "Fail to open the save file. Program will quit.";
if (confirm()) {
delete dir;
exit(0);
}
}
}