-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
230 lines (203 loc) · 5.68 KB
/
main.cpp
File metadata and controls
230 lines (203 loc) · 5.68 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Template class for Responses
template <typename T1, typename T2>
class Response
{
private:
T1 keyword;
T2 response;
public:
Response(T1 k = "", T2 r = "") : keyword(k), response(r) {}
T1 getKeyword() const
{
return keyword;
}
T2 getResponse() const
{
return response;
}
void setKeyword(T1 k)
{
keyword = k;
}
void setResponse(T2 r)
{
response = r;
}
};
// Base class: Chatbot
class Chatbot
{
protected:
string name;
static const int maxSize = 100; // Maximum number of keyword-response pairs
Response<string, string> responses[maxSize]; // Composition: Using Response objects with templates
int currentSize;
string filename;
public:
// Helper method to convert string to lowercase without using cctype
string toLowerCase(string str)
{
for (int i = 0; i < str.length(); ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + ('a' - 'A');
}
}
return str;
}
Chatbot(string botName, string file) : name(botName), filename(file), currentSize(0)
{
loadResponses();
}
virtual void greet()
{ // Polymorphism: virtual function
cout << "Hi! I'm " << name << ". How can I help you today?" << endl;
}
void farewell()
{
cout << "Goodbye! Have a great day!" << endl;
}
void respond(string userInput)
{
string lowerInput = toLowerCase(userInput);
bool found = false;
for (int i = 0; i < currentSize; ++i)
{
if (lowerInput.find(responses[i].getKeyword()) != string::npos)
{
cout << responses[i].getResponse() << endl;
found = true;
break;
}
}
if (!found)
{
cout << "I don't understand. Can you teach me a response for this?" << endl;
string keyword, response;
cout << "Enter a keyword or phrase: ";
getline(cin, keyword);
cout << "Enter the response for this keyword or phrase: ";
getline(cin, response);
learn(keyword, response);
}
}
void learn(string keyword, string response)
{
if (currentSize < maxSize)
{
responses[currentSize].setKeyword(toLowerCase(keyword));
responses[currentSize].setResponse(response);
currentSize++;
saveResponseToFile(keyword, response);
cout << "I've learned a new response!" << endl;
}
else
{
cout << "Sorry, I can't learn any more responses. My memory is full." << endl;
}
}
void loadResponses()
{
ifstream infile(filename);
if (infile.is_open())
{
string keyword, response;
currentSize = 0;
while (getline(infile, keyword) && getline(infile, response))
{
if (currentSize < maxSize)
{
responses[currentSize].setKeyword(toLowerCase(keyword));
responses[currentSize].setResponse(response);
currentSize++;
}
else
{
break;
}
}
infile.close();
}
else
{
cout << "Unable to open the file." << endl;
}
}
void saveResponseToFile(string keyword, string response)
{
ofstream outfile(filename, ios::app);
if (outfile.is_open())
{
outfile << keyword << endl << response << endl;
outfile.close();
}
else
{
cout << "Unable to open the file." << endl;
}
}
};
// Derived class: Inheritance from Chatbot
class AutoMaintenanceChatbot : public Chatbot
{
public:
AutoMaintenanceChatbot(string botName, string file) : Chatbot(botName, file) {}
void customServiceInfo()
{
cout << "We provide a range of auto maintenance services. For more details, please ask about specific services." << endl;
}
void greet() override
{ // Polymorphism: Override base class method
cout << "Hello! Welcome to the Auto Maintenance Workshop. I'm " << name << ". How can I assist you today?" << endl;
}
};
// Separate class for Emergency Services: Demonstrating Composition
class EmergencyService
{
public:
void provideEmergencyInfo() {
cout << "In case of roadside emergencies, please contact +92 316 5119292 for immediate assistance." << endl;
}
};
// Another Derived class: Inheritance from AutoMaintenanceChatbot
class WorkshopChatbot : public AutoMaintenanceChatbot
{
private:
EmergencyService emergencyService; // Composition: Has an EmergencyService
public:
WorkshopChatbot(string botName, string file) : AutoMaintenanceChatbot(botName, file) {}
void provideEmergencyService()
{
emergencyService.provideEmergencyInfo();
}
};
int main()
{
WorkshopChatbot myBot("MJ's Bot", "MJ-details.txt");
myBot.greet();
string userInput;
while (true)
{
cout << "You: ";
getline(cin, userInput);
if (myBot.toLowerCase(userInput) == "bye" || myBot.toLowerCase(userInput) == "exit")
{
myBot.farewell();
break;
}
else if (myBot.toLowerCase(userInput) == "emergency")
{
myBot.provideEmergencyService();
}
else
{
myBot.respond(userInput);
}
}
return 0;
}