-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregularCustomer.cpp
More file actions
130 lines (111 loc) · 3.64 KB
/
regularCustomer.cpp
File metadata and controls
130 lines (111 loc) · 3.64 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
#include "regularCustomer.h"
int RegularCustomer::count = 0;
RegularCustomer::RegularCustomer() {}
RegularCustomer::RegularCustomer(const int id, const string& name, int age, const string& contactInfo)
: Customer(name, age, contactInfo), id(id) {}
void RegularCustomer::setCount(int count) {
RegularCustomer::count = count;
}
int RegularCustomer::getCount() const {
return count;
}
void RegularCustomer::addCustomer(){
cout << "Enter customer name: ";
getline(cin, name);
cout << "Enter customer age: ";
cin >> age;
cin.ignore();
cout << "Enter contact information: ";
getline(cin, contactInfo);
count++;
if (!saveToFile()){
cout<<"Failed to save customer to file"<<endl;
}
}
void RegularCustomer::viewCustomer(Customer* customers, int size) {
if (size == 0) {
cout << "No regular customers to display." << endl;
return;
}
cout << "List of Customers:" << endl;
cout << "----------------" << endl;
for (int i = 0; i < size; i++) {
cout << static_cast<RegularCustomer*>(customers)[i] << endl;
cout<<"----------------" << endl;
}
cout << "Regular customers in record: " << count << endl;
}
void RegularCustomer::searchCustomer(Customer* c, int index) {
bool found=false;
string searchTerm;
cout<<"Enter name of regular customer you want to search: ";
getline(cin,searchTerm);
for (int i=0; i<index; i++){
string searchName=static_cast<RegularCustomer*>(c)[i].name;
for (int j=0; j<searchName.length(); j++){
searchName[j]=tolower(searchName[j]);
}
for (int j=0; j<searchTerm.length(); j++){
searchTerm[j]=tolower(searchTerm[j]);
}
if (searchTerm==searchName){
cout<<"Customer Details: "<<"\nID: "<<static_cast<RegularCustomer*>(c)[i].id<<"\nName: "<<static_cast<RegularCustomer*>(c)[i].name<<"\nAge: "<<static_cast<RegularCustomer*>(c)[i].age<<"\nContact Info: "<<static_cast<RegularCustomer*>(c)[i].contactInfo<<endl;
found=true;
break;
}
}
if (!found){
cout<<"No customer matching the name \""<<searchTerm<<"\" found. "<<endl;
}
}
bool RegularCustomer::saveToFile() {
ofstream fout;
try{
fout.open("regularCustomers.txt",ios::app);
if (fout.fail()){
throw runtime_error("Error opening regularCustomers.txt for writing");
}
}
catch(runtime_error &e){
cout<<e.what()<<endl;
return false;
}
fout<<id<<endl;
fout<<name<<endl;
fout<<age<<endl;
fout<<contactInfo<<endl;
fout.close();
cout << "Regular customer saved to file successfully." << endl;
return true;
}
void RegularCustomer::readFile(Customer *c, int &index) {
ifstream fin;
try{
fin.open("regularCustomers.txt");
if (fin.fail()){
throw runtime_error("Error opening regularCustomers.txt for reading");
}
}
catch(runtime_error &e){
cout<<e.what()<<endl;
return;
}
while (fin>>static_cast<RegularCustomer*>(c)[index].id ) {
fin.get();
getline(fin,static_cast<RegularCustomer*>(c)[index].name);
fin>>static_cast<RegularCustomer*>(c)[index].age;
fin.get();
getline(fin,static_cast<RegularCustomer*>(c)[index].contactInfo);
index++;
count++;
}
fin.close();
cout << "Regular customers read from file successfully." << endl;
}
ostream& operator<<(ostream& output, const RegularCustomer& customer) {
output<<"ID: "<<customer.id<<endl;
output <<"Name: " << customer.name << endl;
output <<"Age: " << customer.age << endl;
output <<"Contact Info: " << customer.contactInfo << endl;
return output;
}