-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarmlist.cpp
More file actions
177 lines (165 loc) · 6.65 KB
/
farmlist.cpp
File metadata and controls
177 lines (165 loc) · 6.65 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 "farmlist.h"
// reads the farm to add from the user and then calls insertFarm to add it to the dataset
void FarmList::addFarm() {
cout << "COUNT BEFORE ADD FARM: " << count << endl;
Farm farm;
farm.readToFarm(); // read the farm's attributes from the user
// insert the newly created farm to the array
insertFarm(farm);
}
// this function inserts a farm into the array of farms in the correct position
void FarmList::insertFarm(Farm tempFarm) {
int insertIndex = 0;
// compare by farm name to alphabetize and find the correct insert index
Node* curr = head; // start at the head of the linked list
Node* prev = nullptr; // pointer to the previous node, initially null
// traverse the linked list to find the correct insert index
while (curr && isLessThan(curr->farm.getName(), tempFarm.getName())) {
prev = curr; // keep track of the previous node
curr = curr->next; // move to the next farm in the linked list
insertIndex++; // increment the insert index
}
// now curr points to the first farm that is greater than or equal to tempFarm, or nullptr if we reached the end of the list
if (prev == nullptr) { // if we are inserting at the head
head = new Node;
head->farm = tempFarm; // set the farm to the new farm
head->next = curr; // link to the next farm in the linked list
}
else { // if we are inserting in the middle or end of the linked list
Node* newNode = new Node;
newNode->farm = tempFarm; // set the farm to the new farm
newNode->next = curr; // link to the next farm in the linked list
prev->next = newNode; // link previous farm to new farm
// print the farm that was just added for confirmation
}
// increment the count of farms
count++;
}
// this function lists the farms in the array with indices
void FarmList::listFarms() {
if (count == 0) { // database empty
cout << "Database empty! Please add a farm" << endl;
}
else { // database not empty
cout << left << setw(6) << "Index" << setw(25) << "Farm Name" << setw(15) << "Location" << setw(9) << "Rating" << setw(13) << "Num Products" << "Products" << endl;
cout << setfill('=') << setw(120) << "=" << setfill(' ') << endl;
Node* curr = head; // start at the head of the linked list
int i = 0;
while(curr) {
cout << setw(6) << i;
curr->farm.printFarm();
curr = curr->next; // move to the next farm in the linked list
i++;
}
}
}
// this function lists the farms in the array with indices
void FarmList::searchLocation() {
if (count == 0) { // database empty
cout << "Database empty! Please add a farm" << endl;
}
else { // database not empty
char locationKeyword[MAX_CHAR];
cout << endl << "Enter the locaiton/keyword(case sensitive): ";
cin.getline(locationKeyword, MAX_CHAR, '\n');
cout << "Farms that matched your search:" << endl;
cout << left << setw(25) << "Farm Name" << setw(15) << "Location" << setw(9) << "Rating" << setw(13) << "Num Products" << "Products" << endl;
cout << setfill('=') << setw(100) << "=" << endl << setfill(' ');
// go through list of farms, printing the ones that match the location keyword
// strstr returns a pointer to the first occurrence of locationKeyword in the farm's location
Node* curr = head; // start at the head of the linked list
while(curr) {
if (strstr(curr->farm.getLocation(), locationKeyword) != nullptr) {
curr->farm.printFarm();
}
curr = curr->next; // move to the next farm in the linked list
}
cout << endl;
}
}
// this function reads a farm's attributes from the user and inserts it into the farm array if not already full
void FarmList::removeByIndex() {
if (count == 0) { // database empty
cout << "Database empty! Please add a farm" << endl;
}
else { // database not empty
listFarms(); // includes indices so user can see what to remove
int removeIndex;
cout << "Enter the index of the farm to remove: ";
cin >> removeIndex;
while (!cin || removeIndex < 0 || removeIndex >= count) { // validate removeIndex
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!! Please enter a valid index: ";
cin >> removeIndex;
}
// remove the farm at removeIndex
Node* curr = head; // start at the head of the linked list
for (int i = 0; i < removeIndex; i++) {
curr = curr->next; // traverse the linked list to the given index
}
// now curr points to the farm to remove
if (curr == head) { // if the farm to remove is the head
head = curr->next; // move head to the next farm
}
else { // if the farm to remove is not the head
Node* prev = head; // start at the head of the linked list
while (prev->next != curr) { // find the previous farm
prev = prev->next;
}
prev->next = curr->next; // link previous farm to next farm
}
delete curr; // delete the farm to remove
count--;
}
}
// searches the farms in the array by inputted product code
void FarmList::searchByProduct() {
head->farm.printProduceCodes();
int code;
cout << "Enter the product code to search for: ";
cin >> code;
while (!cin || code <= 0 || code > 12) { // validate code
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!! Enter a valid product code 1-12 " << endl;
cout << "Enter the product code to search for: ";
cin >> code;
}
// cout << count;
cin.ignore(100, '\n'); // ignore excess buffer
// go through list of farms, printing the ones that have the product
cout << left << setw(25) << "Farm Name" << setw(15) << "Location" << setw(9) << "Rating" << setw(13) << "Num Products" << "Products" << endl;
cout << setfill('=') << setw(100) << "=" << endl << setfill(' ');
Node* curr = head; // start at the head of the linked list
while(curr != nullptr) {
for (int j = 0; j < curr->farm.getNumProducts(); j++) {
if (curr->farm.getProductAt(j) == code) {
curr->farm.printFarm(); // print current farm if it has the product
j = curr->farm.getNumProducts(); // break out of the inner loop
}
}
curr = curr->next; // move to the next farm in the linked list
}
cout << endl;
}
// this function is used in insertFarm to compare the names of the farms for alphabetizing
bool FarmList::isLessThan(char const name1[], char const name2[]) {
if (strcmp(name1, name2) < 0) {
return true;
}
else {
return false;
}
}
Farm FarmList::getFarmAt(int index) {
Node* curr = head;
for (int i = 0; i < index && curr; i++) {
curr = curr->next; // traverse the linked list to the given index
}
if (!curr) {
cout << "Index out of bounds!" << endl;
return Farm(); // return an empty farm if index is out of bounds
}
return curr->farm; // return the farm at the given index in the array
}