-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase
169 lines (151 loc) · 4.48 KB
/
database
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
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>
#include <fstream> //form work with files
using namespace std;
const int MAX_USERS = 500;
struct User {
string username;
string password;
};
class PasswordDatabase {
private:
User database[MAX_USERS];
int count;
public:
PasswordDatabase() : count(0) {}
void addPassword(const string& username, const string& password) {
if (count >= MAX_USERS) {
cout << "Database is full!\n";
return;
}
for (int i = 0; i < count; i++) {
if (database[i].username == username) {
cout << "Username already exists!\n";
return;
}
}
database[count].username = username;
database[count].password = password;
count++;
cout << "Password added for user: " << username << endl;
}
void getPassword(const string& username) const {
for (int i = 0; i < count; i++) {
if (database[i].username == username) {
cout << "Password for user " << username << ": " << database[i].password << endl;
return;
}
}
cout << "Username not found.\n";
}
void deletePassword(const string& username) {
for (int i = 0; i < count; i++) {
if (database[i].username == username) {
for (int j = i; j < count - 1; j++) {
database[j] = database[j + 1];
}
count--;
cout << "Password for user " << username << " deleted.\n";
return;
}
}
cout << "Username not found.\n";
}
void showAllUsers() const {
if (count == 0) {
cout << "Database is empty.\n";
}
else {
cout << "User list:\n";
for (int i = 0; i < count; i++) {
cout << "User: " << database[i].username << ", Password: " << database[i].password << endl;
}
}
}
void saveToFile(const string& filename) const {
ofstream file(filename);
if (file.is_open()) {
for (int i = 0; i < count; i++) {
file << database[i].username << " " << database[i].password << endl;
}
file.close();
cout << "Database saved to file: " << filename << endl;
}
else {
cout << "Failed to open file for writing.\n";
}
}
void loadFromFile(const string& filename) {
ifstream file(filename);
if (file.is_open()) {
string username, password;
while (file >> username >> password) {
addPassword(username, password);
}
file.close();
cout << "Database loaded from file: " << filename << endl;
}
else {
cout << "Failed to open file for reading.\n";
}
}
};
void showMenu() {
cout << "\n=== Password Database Menu ===\n";
cout << "1. Add password\n";
cout << "2. Get password\n";
cout << "3. Delete password\n";
cout << "4. Show all users\n";
cout << "5. Save database to file\n";
cout << "6. Load database from file\n";
cout << "0. Exit\n";
cout << "Choose an option: ";
}
int main() {
PasswordDatabase db;
int choice;
string username, password, filename;
cout << "Create by Yaroslav Yatsenko\n";
do {
showMenu();
cin >> choice;
switch (choice) {
case 1:
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
db.addPassword(username, password);
break;
case 2:
cout << "Enter username: ";
cin >> username;
db.getPassword(username);
break;
case 3:
cout << "Enter username: ";
cin >> username;
db.deletePassword(username);
break;
case 4:
db.showAllUsers();
break;
case 5:
cout << "Enter filename: ";
cin >> filename;
db.saveToFile(filename);
break;
case 6:
cout << "Enter filename: ";
cin >> filename;
db.loadFromFile(filename);
break;
case 0:
cout << "Exiting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);
return 0;
}