-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_cattle.cpp
More file actions
233 lines (195 loc) · 7.37 KB
/
Copy pathinteractive_cattle.cpp
File metadata and controls
233 lines (195 loc) · 7.37 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
//NAME: Ignacio Jimenez
//RED ID: 826019175
//Interactive Cattle Management System
#include "cattle_tree.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
void displayMenu() {
cout << "\n=== CATTLE MANAGEMENT SYSTEM ===" << endl;
cout << "1. Add Cattle" << endl;
cout << "2. Query Cattle" << endl;
cout << "3. Count Cattle" << endl;
cout << "4. Delete Cattle" << endl;
cout << "5. Add Notes to Cattle" << endl;
cout << "6. View Cattle Notes" << endl;
cout << "7. Load from File" << endl;
cout << "8. Save to File" << endl;
cout << "9. Exit" << endl;
cout << "Choose an option (1-9): ";
}
void addCattle(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path (e.g., s_0_2_1_0_5): ";
cin >> cattlePath;
if (root.addCattle(cattlePath.c_str())) {
cout << "✓ Cattle added successfully!" << endl;
string annotation;
cout << "Add notes about this cattle (press Enter to skip): ";
cin.ignore();
getline(cin, annotation);
if (!annotation.empty()) {
root.setAnnotation(cattlePath.c_str(), annotation.c_str());
cout << "✓ Notes added successfully!" << endl;
}
} else {
cout << "✗ Failed to add cattle. Check path format." << endl;
}
}
void queryCattle(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path to query (e.g., s_0_2_1_0_5): ";
cin >> cattlePath;
int count = root.findCattle(cattlePath.c_str());
string annotation = root.getAnnotation(cattlePath.c_str());
cout << "\nQuery Result:" << endl;
cout << cattlePath << " " << count;
if (!annotation.empty()) {
cout << " [" << annotation << "]";
}
cout << endl;
}
void countCattle(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path to count (e.g., s_0 for all Yacumeño): ";
cin >> cattlePath;
int count = root.findCattle(cattlePath.c_str());
cout << "\nCount: " << count << " cattle" << endl;
}
void deleteCattle(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path to delete (e.g., s_0_2_1_0_5): ";
cin >> cattlePath;
if (root.removeCattle(cattlePath.c_str())) {
cout << "✓ Cattle deleted successfully!" << endl;
} else {
cout << "✗ Failed to delete cattle. (May not exist or has children)" << endl;
}
}
void addNotes(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path to add notes (e.g., s_0_2_1_0_5): ";
cin >> cattlePath;
string annotation;
cout << "Enter notes: ";
cin.ignore();
getline(cin, annotation);
if (root.setAnnotation(cattlePath.c_str(), annotation.c_str())) {
cout << "✓ Notes added successfully!" << endl;
} else {
cout << "✗ Failed to add notes. Cattle may not exist." << endl;
}
}
void viewNotes(CattleNode& root) {
string cattlePath;
cout << "\nEnter cattle path to view notes (e.g., s_0_2_1_0_5): ";
cin >> cattlePath;
string annotation = root.getAnnotation(cattlePath.c_str());
if (!annotation.empty()) {
cout << "\nNotes for " << cattlePath << ": " << annotation << endl;
} else {
cout << "\nNo notes found for " << cattlePath << endl;
}
}
void loadFromFile(CattleNode& root, vector<int>& maxCattlePerLevel) {
string filename;
cout << "\nEnter filename to load (e.g., skuchart.txt): ";
cin >> filename;
ifstream file(filename);
if (!file) {
cout << "✗ Unable to open " << filename << endl;
return;
}
// Read internal levels
string line;
getline(file, line);
int internalLevels = stoi(line.substr(line.find('=') + 1));
// Read max cattle per level
getline(file, line);
stringstream ss(line);
maxCattlePerLevel.resize(internalLevels);
for(int i = 0; i < internalLevels; i++){
ss >> maxCattlePerLevel[i];
}
// Read cattle paths and annotations
int count = 0;
while(getline(file, line) && !line.empty()){
// Check if line has annotation (contains [ and ])
size_t startBracket = line.find('[');
size_t endBracket = line.find(']');
string cattlePath = line;
string annotation = "";
if (startBracket != string::npos && endBracket != string::npos && endBracket > startBracket) {
// Extract cattle path (everything before [)
cattlePath = line.substr(0, startBracket);
// Remove trailing spaces
while (!cattlePath.empty() && cattlePath.back() == ' ') {
cattlePath.pop_back();
}
// Extract annotation (everything between [ and ])
annotation = line.substr(startBracket + 1, endBracket - startBracket - 1);
}
if (root.addCattle(cattlePath.c_str())) {
count++;
// Add annotation if present
if (!annotation.empty()) {
root.setAnnotation(cattlePath.c_str(), annotation.c_str());
}
}
}
file.close();
cout << "✓ Loaded " << count << " cattle from " << filename << endl;
}
void saveToFile(CattleNode& root, const vector<int>& maxCattlePerLevel) {
string filename;
cout << "\nEnter filename to save (e.g., my_cattle.txt): ";
cin >> filename;
ofstream file(filename);
if (!file) {
cout << "✗ Unable to create " << 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 is a simplified save - in a real system you'd need to traverse the tree
// and save all cattle paths with their annotations. For now, this creates a template file.
file << "# Add your cattle paths here, one per line" << endl;
file << "# Format: s_0_2_1_0_5 [Optional annotation]" << endl;
file << "# Example: s_0_2_1_0_5 [Pregnant - Due March 2025]" << endl;
file << "# Example: s_1_0_0_1_3 [Vaccinated - Last shot Jan 2025]" << endl;
file.close();
cout << "✓ Template saved to " << filename << endl;
cout << " (Note: You'll need to add cattle paths manually)" << endl;
}
int main() {
cout << "Welcome to the Interactive Cattle Management System!" << endl;
cout << "Initializing with default settings..." << endl;
// Initialize with default settings
vector<int> maxCattlePerLevel = {8, 6, 9, 5, 7}; // 8 breeds, 6 age groups, etc.
CattleNode root("s", 0, maxCattlePerLevel);
int choice;
do {
displayMenu();
cin >> choice;
switch(choice) {
case 1: addCattle(root); break;
case 2: queryCattle(root); break;
case 3: countCattle(root); break;
case 4: deleteCattle(root); break;
case 5: addNotes(root); break;
case 6: viewNotes(root); break;
case 7: loadFromFile(root, maxCattlePerLevel); break;
case 8: saveToFile(root, maxCattlePerLevel); break;
case 9: cout << "Goodbye!" << endl; break;
default: cout << "Invalid option. Please try again." << endl;
}
} while(choice != 9);
return 0;
}