-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarm.cpp
More file actions
125 lines (118 loc) · 3.85 KB
/
farm.cpp
File metadata and controls
125 lines (118 loc) · 3.85 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
#include "farm.h"
// constructor
Farm::Farm() {
// initialize the farm's attributes
name = new char[1];
strcpy(name, ""); // initialize to empty string
location = new char[1];
strcpy(location, ""); // initialize to empty string
rating = -1;
numProducts = 0;
for (int i = 0; i < 12; i++) {
products[i] = -1; // initialize to -1 to indicate no product codes
}
}
// destructor
Farm::~Farm() {
name = nullptr; // deallocate name
location = nullptr; // deallocate location
}
// assignment operator
Farm& Farm::operator=(const Farm& farmToCopy) {
if (this != &farmToCopy) { // check for self-assignment
setName(farmToCopy.name); // set the name
setLocation(farmToCopy.location); // set the location
rating = farmToCopy.rating;
numProducts = farmToCopy.numProducts;
for (int i = 0; i < numProducts; i++) {
products[i] = farmToCopy.products[i];
}
}
// print the farm's attributes after assignment for confirmation
return *this; // return the current object
} // assignment operator
void Farm::setLocation(char const loc[]) {
if (location) {
// if location already exists, delete it to prevent memory leak
delete[] location;
}
location = new char[strlen(loc) + 1];
strcpy(location, loc);
} // sets the farm's location
void Farm::setName(char const farmName[]) {
if (name) {
// if name already exists, delete it to prevent memory leak
delete[] name;
}
name = new char[strlen(farmName) + 1];
strcpy(name, farmName);
} // sets the farm's name
// this function prints a farm's attributes seperated by \t tab
void Farm::printFarm() {
cout << left << setw(25) << name << setw(15) << location << setw(9) << rating;
cout << left << setw(13) << numProducts;
for (int i = 0; i < numProducts; i++) {
if (i != 0) {
cout << ", ";
}
cout << productNames[products[i] - 1];
}
cout << endl;
}
// this function prints the produce names that correlate with the product codes.
void Farm::printProduceCodes() {
cout << "Produce Codes:" << endl;
for (int i = 0; i < 12; i++) {
cout << "\t" << i + 1 << ") " << productNames[i] << endl;
}
}
// this function reads a farm's attributes from the user, called by addFarm
void Farm::readToFarm() {
// create input variables
char nameInput[MAX_CHAR];
char locationInput[MAX_CHAR];
// deallocate old name and location to prevent memory leak
delete[] name;
delete[] location;
// farm name input
cout << "Enter Farm Name: ";
cin.getline(nameInput, MAX_CHAR, '\n');
name = new char[strlen(nameInput) + 1]; // allocate memory for name
strcpy(name, nameInput); // copy the input to the farm's name
//location input
cout << "Enter Location: ";
cin.getline(locationInput, MAX_CHAR, '\n');
location = new char[strlen(locationInput) + 1]; // allocate memory for location
strcpy(location, locationInput); // copy the input to the farm's location
//rating
cout << "Enter the rating of the farm (1-5): ";
cin >> rating;
while (!cin || rating < 1 || rating > 5) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!! Please enter a valid rating 1-5: ";
cin >> rating;
}
//numProducts
cout << "Enter Number of Products(1-12): ";
cin >> numProducts;
while (!cin || numProducts < 1 || numProducts > 12) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!! Please enter a valid number of products (1-12): ";
cin >> numProducts;
}
printProduceCodes();
// Product codes for the products that the farm makes
for (int i = 0; i < numProducts; i++) {
cout << "Enter code for product" << i + 1 << ": ";
cin >> products[i];
while (!cin || products[i] < 1 || products[i] > 12) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!! Please enter a valid product code 1-12" << endl;
cout << "Enter code for product " << i + 1 << ": ";
cin >> products[i];
}
}
}