-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoctor.cpp
More file actions
177 lines (161 loc) · 6.73 KB
/
Doctor.cpp
File metadata and controls
177 lines (161 loc) · 6.73 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
#include "Doctor.h"
#include "Index.h"
#include "Record.h"
#include "Appointment.h"
void Doctor:: addDoctor(const string& doctorID, const string& name, const string& address,map<string, int>& primaryIndex) {
if (primaryIndex.find(doctorID) != primaryIndex.end()) {
cout << "Doctor ID already exists! Enter another ID" << endl;
return;
}
fstream outfile;
outfile.open("Doctor.txt", ios::in|ios::out);
Record r;
int offset=r.writeRecord(outfile, doctorID, name, address, primaryIndex, doctorAvailList);
useprimaryIndex p;
p.addPrimaryIndex(primaryIndex,doctorID,offset);
p.savePrimaryIndexToFile(primaryIndex, "PrimaryIndexOnDocID.txt");
r.saveAvailListToFile(doctorAvailList,"doctorAvailList.txt");
useSecondaryIndex secIdx;
secIdx.loadSecondaryIndexFromFile("SecondaryIndexOnDrName.txt");
secIdx.addSecondaryIndex(name, doctorID);
secIdx.saveSecondaryIndexToFile("SecondaryIndexOnDrName.txt");
cout << "Doctor added successfully.\n";
outfile.close();
}
void Doctor:: deleteDoctor(const string &doctorID,const string& doctorName) {
if (primaryIndex.find(doctorID) == primaryIndex.end()) {
cout << "Doctor ID you want to delete does not exist. Deletion failed.\n";
return;
}
fstream file("Doctor.txt", ios::in | ios::out);
if (!file) {
cerr << "Error opening file for deleting doctor.\n";
return;
}
useprimaryIndex p ;
useSecondaryIndex secIdx;
UseSecondaryIndexonAppoint secApp;
int offset = p.binarySearchPrimaryIndex(primaryIndex,doctorID);
Record r1;
r1.deleteRecord(file, offset, doctorAvailList);
p.removePrimaryIndex(primaryIndex,doctorID);
secIdx.loadSecondaryIndexFromFile("SecondaryIndexOnDrName.txt");
if(secIdx.removeSecondaryIndex(doctorName,doctorID))
{
cout << "Secondary index updated after doctor deletion.\n";
}
else {
cout << "Doctor not found in secondary index.\n";
}
cout << "Doctor record deleted successfully.\n";
p.savePrimaryIndexToFile(primaryIndex, "PrimaryIndexOnDocID.txt");
secIdx.saveSecondaryIndexToFile("SecondaryIndexOnDrName.txt");
r1.saveAvailListToFile(doctorAvailList, "doctorAvailList.txt");
secApp.loadSecondaryIndexFromFile("SecondaryIndexOnDocID.txt");
vector<string> v;
v=secApp.searchByDoctorID(doctorID);
for (int i = 0; i < v.size(); ++i) {
secApp.removeSecondaryIndex(doctorID,v[i]);
}
secApp.saveSecondaryIndexToFile("SecondaryIndexOnDocID.txt");
}
//to delete from secondary index on name omnly not app secindex as we use it in updata on doctor name because it the same docID
void Doctor:: deleteDoctor2(const string &doctorID,const string& doctorName) {
if (primaryIndex.find(doctorID) == primaryIndex.end()) {
cout << "Doctor ID you want to delete does not exist. Deletion failed.\n";
return;
}
fstream file("Doctor.txt", ios::in | ios::out);
if (!file) {
cerr << "Error opening file for deleting doctor.\n";
return;
}
useprimaryIndex p ;
useSecondaryIndex secIdx;
UseSecondaryIndexonAppoint secApp;
int offset = p.binarySearchPrimaryIndex(primaryIndex,doctorID);
Record r1;
r1.deleteRecord(file, offset, doctorAvailList);
p.removePrimaryIndex(primaryIndex,doctorID);
secIdx.loadSecondaryIndexFromFile("SecondaryIndexOnDrName.txt");
if(secIdx.removeSecondaryIndex(doctorName,doctorID))
{
cout << "Secondary index updated after doctor deletion.\n";
}
else {
cout << "Doctor not found in secondary index.\n";
}
cout << "Doctor record deleted successfully.\n";
p.savePrimaryIndexToFile(primaryIndex, "PrimaryIndexOnDocID.txt");
secIdx.saveSecondaryIndexToFile("SecondaryIndexOnDrName.txt");
r1.saveAvailListToFile(doctorAvailList, "doctorAvailList.txt");
}
void Doctor:: printDoctorInfo(const string& doctorID) {
if (primaryIndex.find(doctorID) == primaryIndex.end()) {
cout << "Doctor ID not found.\n";
return;
}
useprimaryIndex p ;
int offset = p.binarySearchPrimaryIndex(primaryIndex,doctorID);
fstream file("Doctor.txt", ios::in);
if (!file) {
cerr << "Error opening file to read doctor information.\n";
return;
}
Record record = record.readRecord(file, offset);
file.close();
cout << "Doctor Information:\n";
cout << "Doctor ID: " << record.s1 << "\n";
cout << "Doctor Name: " << record.s2 << "\n";
cout << "Address: " << record.s3 << "\n";
}
void Doctor:: updateDoctorName(const string& doctorID, const string& newDoctorName) {
if (primaryIndex.find(doctorID) == primaryIndex.end()) {
cout << "Doctor ID not found. Update failed.\n";
return;
}
if (newDoctorName.length() > 30) {
cout << "Error: Doctor name cannot exceed 30 characters.\n";
return;
}
fstream file("Doctor.txt", ios::in | ios::out);
if (!file) {
cerr << "Error opening file to update doctor information.\n";
return;
}
useprimaryIndex p;
int offset = p.binarySearchPrimaryIndex(primaryIndex, doctorID);
Record record;
Record currentRecord = record.readRecord(file, offset);
string oldDoctorName = currentRecord.s2;
if (newDoctorName.length() <= oldDoctorName.length()) {
// Pad with spaces if the new name is shorter or equal in length best fit strategy
string paddedName = newDoctorName;
paddedName.append(oldDoctorName.length() - newDoctorName.length(), ' ');
currentRecord.s2 = paddedName;
// Save the updated record back to the file
string updatedRecord = currentRecord.s1 + "|" + currentRecord.s2 + "|" + currentRecord.s3 + "|";
file.seekp(offset, ios::beg);
file << updatedRecord.size() << updatedRecord;
cout << "Doctor name updated successfully from " << oldDoctorName << " to " << newDoctorName << ".\n";
} else {
// If the new name is longer than the old one, remove and re-add the record
file.close();
deleteDoctor2(doctorID, oldDoctorName);
addDoctor(doctorID, newDoctorName, currentRecord.s3, primaryIndex);
cout << "Doctor name updated successfully by deleting and adding a new record.\n";
return;
}
file.close();
// Update the secondary index
useSecondaryIndex secIdx;
secIdx.loadSecondaryIndexFromFile("SecondaryIndexOnDrName.txt");
// Remove the old name from the secondary index
if (!secIdx.removeSecondaryIndex(oldDoctorName, doctorID)) {
cout << "Old doctor name not found in secondary index.\n";
}
// Add the new name to the secondary index
secIdx.addSecondaryIndex(newDoctorName, doctorID);
// Save the updated secondary index
secIdx.saveSecondaryIndexToFile("SecondaryIndexOnDrName.txt");
}