-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcattle_manager.cpp
More file actions
426 lines (358 loc) · 13.1 KB
/
cattle_manager.cpp
File metadata and controls
426 lines (358 loc) · 13.1 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//NAME: Ignacio Jimenez
//RED ID: 826019175
//Simple Cattle Manager for Farmers
#include "cattle_tree.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
void showMenu() {
cout << "\n🐄 CATTLE MANAGER 🐄" << endl;
cout << "===================" << endl;
cout << "1. Add Cow" << endl;
cout << "2. Find Cow" << endl;
cout << "3. Count Cows" << endl;
cout << "4. View All Cattle" << endl;
cout << "5. Remove Cow" << endl;
cout << "6. Add Note" << endl;
cout << "7. See Note" << endl;
cout << "8. Load Data" << endl;
cout << "9. Save Data" << endl;
cout << "10. Exit" << endl;
cout << "\nWhat do you want to do? (1-10): ";
}
void addCow(CattleNode& root) {
cout << "\n🐮 ADD NEW COW" << endl;
cout << "==============" << endl;
cout << "Breed (0=Yacumeño, 1=Chaqueño, 2=Chusco, 3=Saavedreño, 4=Holstein, 5=Brahman, 6=Angus, 7=Hereford): ";
int breed;
cin >> breed;
cout << "Age (0=Calf, 1=Young, 2=Mature): ";
int age;
cin >> age;
cout << "Health (0=Excellent, 1=Good, 2=Fair, 3=Sick, 4=Quarantine, 5=Pregnant): ";
int health;
cin >> health;
cout << "Purpose (0=Breeding, 1=Dairy, 2=Meat, 3=Replacement, 4=Cull, 5=Show): ";
int purpose;
cin >> purpose;
cout << "Cow Number (0-9): ";
int cowNum;
cin >> cowNum;
// Build the cattle path
string cattlePath = "s_" + to_string(breed) + "_" + to_string(age) + "_" +
to_string(health) + "_" + to_string(purpose) + "_" + to_string(cowNum);
if (root.addCattle(cattlePath.c_str())) {
cout << "✅ Cow added successfully!" << endl;
cout << "Cow ID: " << cattlePath << endl;
cout << "\nAdd a note about this cow (or press Enter to skip): ";
cin.ignore();
string note;
getline(cin, note);
if (!note.empty()) {
root.setAnnotation(cattlePath.c_str(), note.c_str());
cout << "✅ Note added!" << endl;
}
} else {
cout << "❌ Failed to add cow. Please check your numbers." << endl;
}
}
void findCow(CattleNode& root) {
cout << "\n🔍 FIND COW" << endl;
cout << "===========" << endl;
cout << "Enter cow ID (like s_0_2_1_0_5): ";
string cowId;
cin >> cowId;
int count = root.findCattle(cowId.c_str());
string note = root.getAnnotation(cowId.c_str());
if (count > 0) {
cout << "✅ Found your cow!" << endl;
cout << "Cow: " << cowId << " (Count: " << count << ")" << endl;
if (!note.empty()) {
cout << "Note: " << note << endl;
}
} else {
cout << "❌ Cow not found. Check the ID." << endl;
}
}
void countCows(CattleNode& root) {
cout << "\n📊 COUNT COWS" << endl;
cout << "=============" << endl;
cout << "What do you want to count?" << endl;
cout << "1. All cows of a breed" << endl;
cout << "2. All cows of an age group" << endl;
cout << "3. All cows with same health" << endl;
cout << "4. All cows with same purpose" << endl;
cout << "5. Custom search" << endl;
cout << "Choose (1-5): ";
int choice;
cin >> choice;
string searchPath;
switch(choice) {
case 1: {
cout << "Breed (0-7): ";
int breed;
cin >> breed;
searchPath = "s_" + to_string(breed);
break;
}
case 2: {
cout << "Breed (0-7): ";
int breed;
cin >> breed;
cout << "Age (0-2): ";
int age;
cin >> age;
searchPath = "s_" + to_string(breed) + "_" + to_string(age);
break;
}
case 3: {
cout << "Breed (0-7): ";
int breed;
cin >> breed;
cout << "Age (0-2): ";
int age;
cin >> age;
cout << "Health (0-5): ";
int health;
cin >> health;
searchPath = "s_" + to_string(breed) + "_" + to_string(age) + "_" + to_string(health);
break;
}
case 4: {
cout << "Breed (0-7): ";
int breed;
cin >> breed;
cout << "Age (0-2): ";
int age;
cin >> age;
cout << "Health (0-5): ";
int health;
cin >> health;
cout << "Purpose (0-5): ";
int purpose;
cin >> purpose;
searchPath = "s_" + to_string(breed) + "_" + to_string(age) + "_" + to_string(health) + "_" + to_string(purpose);
break;
}
case 5: {
cout << "Enter search path (like s_0 for all Yacumeño): ";
cin >> searchPath;
break;
}
default:
cout << "Invalid choice." << endl;
return;
}
int count = root.findCattle(searchPath.c_str());
cout << "\n📈 RESULT: " << count << " cows found" << endl;
}
void removeCow(CattleNode& root) {
cout << "\n🗑️ REMOVE COW" << endl;
cout << "=============" << endl;
cout << "Enter cow ID to remove (like s_0_2_1_0_5): ";
string cowId;
cin >> cowId;
if (root.removeCattle(cowId.c_str())) {
cout << "✅ Cow removed successfully!" << endl;
} else {
cout << "❌ Could not remove cow. It might not exist or have children." << endl;
}
}
void addNote(CattleNode& root) {
cout << "\n📝 ADD NOTE" << endl;
cout << "===========" << endl;
cout << "Enter cow ID (like s_0_2_1_0_5): ";
string cowId;
cin >> cowId;
cout << "Enter your note: ";
cin.ignore();
string note;
getline(cin, note);
if (root.setAnnotation(cowId.c_str(), note.c_str())) {
cout << "✅ Note added successfully!" << endl;
} else {
cout << "❌ Could not add note. Cow might not exist." << endl;
}
}
void seeNote(CattleNode& root) {
cout << "\n👀 SEE NOTE" << endl;
cout << "===========" << endl;
cout << "Enter cow ID (like s_0_2_1_0_5): ";
string cowId;
cin >> cowId;
string note = root.getAnnotation(cowId.c_str());
if (!note.empty()) {
cout << "📄 Note for " << cowId << ": " << note << endl;
} else {
cout << "❌ No note found for this cow." << endl;
}
}
void viewAllCattle(CattleNode& root) {
cout << "\n📋 ALL YOUR CATTLE" << endl;
cout << "=================" << endl;
// Show counts by breed
cout << "🐄 CATTLE SUMMARY:" << endl;
cout << "==================" << endl;
string breeds[] = {"Yacumeño", "Chaqueño", "Chusco", "Saavedreño", "Holstein", "Brahman", "Angus", "Hereford"};
for(int i = 0; i < 8; i++) {
string breedPath = "s_" + to_string(i);
int count = root.findCattle(breedPath.c_str());
if(count > 0) {
cout << breeds[i] << ": " << count << " cows" << endl;
}
}
cout << "\n💡 TIP: Use option 2 (Find Cow) to see details about specific cows." << endl;
cout << " Use option 3 (Count Cows) for more detailed counts." << endl;
}
void loadData(CattleNode& root, vector<int>& maxCattlePerLevel) {
cout << "\n📂 LOAD DATA" << endl;
cout << "============" << endl;
cout << "Enter filename (like my_cattle.txt): ";
string filename;
cin >> filename;
ifstream file(filename);
if (!file) {
cout << "❌ Could not open file: " << filename << endl;
return;
}
// Read header
string line;
getline(file, line);
int internalLevels = stoi(line.substr(line.find('=') + 1));
getline(file, line);
stringstream ss(line);
maxCattlePerLevel.resize(internalLevels);
for(int i = 0; i < internalLevels; i++){
ss >> maxCattlePerLevel[i];
}
// Read cattle
int count = 0;
while(getline(file, line) && !line.empty()){
size_t startBracket = line.find('[');
size_t endBracket = line.find(']');
string cattlePath = line;
string annotation = "";
if (startBracket != string::npos && endBracket != string::npos && endBracket > startBracket) {
cattlePath = line.substr(0, startBracket);
while (!cattlePath.empty() && cattlePath.back() == ' ') {
cattlePath.pop_back();
}
annotation = line.substr(startBracket + 1, endBracket - startBracket - 1);
}
if (root.addCattle(cattlePath.c_str())) {
count++;
if (!annotation.empty()) {
root.setAnnotation(cattlePath.c_str(), annotation.c_str());
}
}
}
file.close();
cout << "✅ Loaded " << count << " cows from " << filename << endl;
// Automatically show the cattle summary
cout << "\n📋 YOUR CATTLE:" << endl;
cout << "===============" << endl;
string breeds[] = {"Yacumeño", "Chaqueño", "Chusco", "Saavedreño", "Holstein", "Brahman", "Angus", "Hereford"};
for(int i = 0; i < 8; i++) {
string breedPath = "s_" + to_string(i);
int breedCount = root.findCattle(breedPath.c_str());
if(breedCount > 0) {
cout << breeds[i] << ": " << breedCount << " cows" << endl;
}
}
}
void saveData(CattleNode& root, const vector<int>& maxCattlePerLevel) {
cout << "\n💾 SAVE DATA" << endl;
cout << "============" << endl;
cout << "Enter filename (like my_cattle.txt): ";
string filename;
cin >> filename;
ofstream file(filename);
if (!file) {
cout << "❌ Could not create file: " << filename << endl;
return;
}
// Write header
file << "internalLevels=" << maxCattlePerLevel.size() << endl;
for(int i = 0; i < maxCattlePerLevel.size(); i++){
file << maxCattlePerLevel[i];
if(i < maxCattlePerLevel.size() - 1) file << " ";
}
file << endl;
// Note: This creates a template - in a real system you'd traverse the tree
file << "# Add your cattle here, one per line" << endl;
file << "# Format: s_breed_age_health_purpose_number [note]" << endl;
file << "# Example: s_0_2_1_0_5 [Pregnant - Due March 2025]" << endl;
file.close();
cout << "✅ Template saved to " << filename << endl;
cout << " (Add your cattle data to this file)" << endl;
}
int main() {
cout << "🐄 Welcome to Cattle Manager! 🐄" << endl;
cout << "================================" << endl;
cout << "A simple way to track your cattle." << endl;
cout << "Just follow the menu and type numbers!" << endl;
// Initialize with default settings
vector<int> maxCattlePerLevel = {8, 6, 9, 5, 7};
CattleNode root("s", 0, maxCattlePerLevel);
// Try to load current cattle data automatically
cout << "\n🔄 Loading current cattle data..." << endl;
ifstream file("current_cattle.txt");
if (file) {
// Read header
string line;
getline(file, line);
int internalLevels = stoi(line.substr(line.find('=') + 1));
getline(file, line);
stringstream ss(line);
maxCattlePerLevel.resize(internalLevels);
for(int i = 0; i < internalLevels; i++){
ss >> maxCattlePerLevel[i];
}
// Read cattle
int count = 0;
while(getline(file, line) && !line.empty()){
size_t startBracket = line.find('[');
size_t endBracket = line.find(']');
string cattlePath = line;
string annotation = "";
if (startBracket != string::npos && endBracket != string::npos && endBracket > startBracket) {
cattlePath = line.substr(0, startBracket);
while (!cattlePath.empty() && cattlePath.back() == ' ') {
cattlePath.pop_back();
}
annotation = line.substr(startBracket + 1, endBracket - startBracket - 1);
}
if (root.addCattle(cattlePath.c_str())) {
count++;
if (!annotation.empty()) {
root.setAnnotation(cattlePath.c_str(), annotation.c_str());
}
}
}
file.close();
cout << "✅ Loaded " << count << " cows from current_cattle.txt" << endl;
} else {
cout << "ℹ️ No sample data found. Use option 8 to load your own cattle data." << endl;
}
int choice;
do {
showMenu();
cin >> choice;
switch(choice) {
case 1: addCow(root); break;
case 2: findCow(root); break;
case 3: countCows(root); break;
case 4: viewAllCattle(root); break;
case 5: removeCow(root); break;
case 6: addNote(root); break;
case 7: seeNote(root); break;
case 8: loadData(root, maxCattlePerLevel); break;
case 9: saveData(root, maxCattlePerLevel); break;
case 10: cout << "\n👋 Thanks for using Cattle Manager! Goodbye!" << endl; break;
default: cout << "❌ Please choose 1-10." << endl;
}
} while(choice != 10);
return 0;
}