-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.cpp
More file actions
184 lines (161 loc) · 6.14 KB
/
Copy pathoop.cpp
File metadata and controls
184 lines (161 loc) · 6.14 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <fstream>
#include <string>
// Abstract class for abstraction
class Person {
protected:
std::string name;
int age;
public:
// Pure virtual function to ensure derived class implements it
virtual void getDetails() = 0;
virtual void displayDetails() const = 0;
};
// Derived class implementing abstraction and encapsulation
class User : public Person {
private:
std::string occupation;
static const std::string databaseFile; // File to store data
public:
// Constructor
User() : occupation("Unknown") {}
// Function to get user input (Override base class)
void getDetails() override {
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Enter age: ";
std::cin >> age;
std::cout << "Enter occupation: ";
std::cin >> occupation;
}
// Function to display user details (Override base class)
void displayDetails() const override {
std::cout << "Name: " << name << ", Age: " << age << ", Occupation: " << occupation << std::endl;
}
// Function to write user data to file (Persistence)
void writeToFile() {
std::ofstream outFile(databaseFile, std::ios::app); // Append mode to avoid overwriting
if (outFile.is_open()) {
outFile << name << " " << age << " " << occupation << std::endl; // Save details
outFile.close();
} else {
std::cout << "Error opening file for writing." << std::endl;
}
}
// Static function to read and display all users from file
static void readFromFile() {
std::ifstream inFile(databaseFile);
std::string file_name, file_occupation;
int file_age;
if (inFile.is_open()) {
while (inFile >> file_name >> file_age >> file_occupation) {
std::cout << "Name: " << file_name << ", Age: " << file_age << ", Occupation: " << file_occupation << std::endl;
}
inFile.close();
} else {
std::cout << "Error opening file for reading." << std::endl;
}
}
// Static function to edit a user by name
static void editUserDetails(const std::string &searchName, const std::string &newOccupation) {
std::ifstream inFile(databaseFile);
std::ofstream tempFile("temp.txt"); // Temporary file for edits
std::string file_name, file_occupation;
int file_age;
bool found = false;
if (inFile.is_open() && tempFile.is_open()) {
while (inFile >> file_name >> file_age >> file_occupation) {
if (file_name == searchName) {
file_occupation = newOccupation; // Update occupation
found = true;
}
tempFile << file_name << " " << file_age << " " << file_occupation << std::endl;
}
inFile.close();
tempFile.close();
std::remove(databaseFile.c_str());
std::rename("temp.txt", databaseFile.c_str());
if (found) {
std::cout << "User updated successfully.\n";
} else {
std::cout << "User not found.\n";
}
} else {
std::cout << "Error opening files for editing.\n";
}
}
// Static function to delete a user by name
static void deleteUser(const std::string &searchName) {
std::ifstream inFile(databaseFile);
std::ofstream tempFile("temp.txt"); // Temporary file for edits
std::string file_name, file_occupation;
int file_age;
bool found = false;
if (inFile.is_open() && tempFile.is_open()) {
while (inFile >> file_name >> file_age >> file_occupation) {
if (file_name != searchName) {
tempFile << file_name << " " << file_age << " " << file_occupation << std::endl;
} else {
found = true;
}
}
inFile.close();
tempFile.close();
std::remove(databaseFile.c_str());
std::rename("temp.txt", databaseFile.c_str());
if (found) {
std::cout << "User deleted successfully.\n";
} else {
std::cout << "User not found.\n";
}
} else {
std::cout << "Error opening files for deletion.\n";
}
}
};
// Initialize static variable
const std::string User::databaseFile = "user_database.txt";
// Main menu-driven program
int main() {
User user;
int choice;
std::string searchName, newOccupation;
do {
std::cout << "\n*** User Management System ***\n";
std::cout << "1. Add User\n";
std::cout << "2. View All Users\n";
std::cout << "3. Edit User\n";
std::cout << "4. Delete User\n";
std::cout << "5. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
user.getDetails(); // Get user input
user.writeToFile(); // Write data to file
break;
case 2:
User::readFromFile(); // Display all users from file
break;
case 3:
std::cout << "Enter the name of the user to edit: ";
std::cin >> searchName;
std::cout << "Enter new occupation: ";
std::cin >> newOccupation;
User::editUserDetails(searchName, newOccupation); // Edit user by name
break;
case 4:
std::cout << "Enter the name of the user to delete: ";
std::cin >> searchName;
User::deleteUser(searchName); // Delete user by name
break;
case 5:
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid choice, try again.\n";
break;
}
} while (choice != 5);
return 0;
}