-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayController.cpp
159 lines (149 loc) · 4.43 KB
/
DisplayController.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Copyright Navid Mafi Ranji
This source code is provided only for reference purposes. This is not a public domain source code.
All rights reserved.
*/
// Migrated to iostream because i really did not want to manually handle char buffers.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include "DisplayController.h"
using std::cin;
using std::cout;
using std::endl;
// I will try to use the "Single Responsibility Principle" here as much as i can.
int DisplayController::readInput(int min, int max)
{
int userInput = 0;
while (true)
{
cin >> userInput;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
logger("error", "Invalid input, please try again");
}
else
{
if (userInput < min || userInput > max)
{
logger("error", "Invalid input, please try again");
}
else
{
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return userInput;
}
}
}
}
template <typename T>
T DisplayController::readOptionalInput(T defaultValue)
{
T userInput;
if (cin.peek() == '\n')
{
cin.ignore();
return defaultValue;
}
else
{
try
{
getline(cin, userInput);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
return userInput;
}
}
ContactType DisplayController::readContactInput()
{
ContactType contact;
cout << "First Name : ";
contact.firstName = readOptionalInput<string>();
cout << "Last Name: ";
contact.lastName = readOptionalInput<string>();
cout << "Age ( 0 For unknown ): ";
contact.age = readInput(0, 500);
cout << "Address: ";
contact.address = readOptionalInput<string>();
cout << "Email: ";
contact.email = readOptionalInput<string>();
cout << "Phone number: ";
contact.phoneNumber = readOptionalInput<string>();
return contact;
}
void DisplayController::clearScreen()
{
#if defined _WIN32
system("cls");
// clrscr(); // including header file : conio.h
#elif defined(__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
// std::cout<< u8"\033[2J\033[1;1H"; //Using ANSI Escape Sequences
#elif defined(__APPLE__)
system("clear");
#endif
}
void DisplayController::centerPrint(string text)
{
int textLength = text.length();
int padding = (outputWidth - textLength) / 2;
// printf("%*s%s%*s\n", padding, "", text.c_str(), padding, "");
cout << std::setw(padding) << " " << text << std::setw(padding) << " " << endl;
}
void DisplayController::showMainMenu()
{
cout << "-----------------------------------------\n";
centerPrint("Main Menu");
cout << "-----------------------------------------\n";
cout << "1. Add Contact\n";
cout << "2. List Contacts\n";
cout << "3. Edit Contact\n";
cout << "4. Delete Contact\n";
cout << "5. Clear Contacts Database\n";
cout << "6. Find Contact\n";
cout << "7. About\n";
cout << "8. Exit\n";
}
void DisplayController::confirmDBClear()
{
cout << "-----------------------------------------\n";
centerPrint("Clear Contacts Database");
cout << "-----------------------------------------\n";
cout << "Are you sure you want to clear the database?\n";
cout << "1. Yes 2. No\n";
}
void DisplayController::showContactSearchHeader()
{
cout << "-----------------------------------------\n";
centerPrint("Contacts Search");
cout << "-----------------------------------------\n";
}
void DisplayController::showContactListHeader()
{
cout << "-----------------------------------------\n";
centerPrint("Contacts");
cout << "-----------------------------------------\n";
}
// updateContactScreen
void DisplayController::showAbout()
{
cout << "-----------------------------------------\n";
centerPrint("About");
cout << "-----------------------------------------\n";
centerPrint("Software architecture and DB design");
centerPrint("Navid Mafi");
centerPrint("--------------");
centerPrint("Development");
centerPrint("Navid Mafi");
centerPrint("--------------");
centerPrint("Testing");
centerPrint("Navid Mafi");
centerPrint("--------------");
centerPrint("Copyright (c) Navid Mafi 2022");
}