-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.cpp
240 lines (195 loc) · 6.87 KB
/
shapes.cpp
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
//-----------------------------------------------------------
// File: shapes.cpp
// Class: COP 3003, Fall 2022
// Devl: Katarya Johnson-Williams
// Desc: Final Project to Calculate Shape Values
//-----------------------------------------------------------
#include <iostream> // console I/O
#include <vector> // vectors (lists)
#include <memory> // dynamic memory management library (unique pointers)
#include <limits> // validate user input
#include "Point.h"
#include "Shape.h"
#include "Line.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Triangle.h"
#include "Logger.h"
// Function Prototypes (Declarations)
//-----------------------------------------------------------
float validateInput(float input);
Point getPoint();
std::unique_ptr<Line> createLine();
std::unique_ptr<Rectangle> createRectangle();
std::unique_ptr<Circle> createCircle();
std::unique_ptr<Triangle> createTriangle();
void printShapes(std::vector<std::unique_ptr<Shape>>& shapesVector);
int main() {
// create shape collection vector
std::vector<std::unique_ptr<Shape>> shapesVector;
// begin program loop
bool addShapes = true;
int shapeMode = DEFAULT;
do {
std::cout << "\nEnter a Number: " << std::endl;
std::cout << "1. Line" << std::endl;
std::cout << "2. Rectangle" << std::endl;
std::cout << "3. Circle" << std::endl;
std::cout << "4. Right Triangle" << std::endl;
std::cout << "5. Print Shapes" << std::endl;
std::cout << "6. End Program" << std::endl;
std::cout << "> ";
std::cin >> shapeMode;
bool validMode = false;
while (!validMode) {
if (std::cin.fail()) {
Logger::log(ERROR, "shapeMode input was invalid");
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "You have entered an invalid mode. Please retry: ";
std::cin >> shapeMode;
} else {
validMode = true;
}
}
switch(shapeMode) {
case LINE:
shapesVector.push_back(std::move(createLine()));
break; // end line
case RECTANGLE:
shapesVector.push_back(std::move(createRectangle()));
break; // end rectangle
case CIRCLE:
shapesVector.push_back(std::move(createCircle()));
break; // end circle
case TRIANGLE:
shapesVector.push_back(std::move(createTriangle()));
break; // end triangle
case PRINT:
printShapes(shapesVector);
break; // end print
case EXIT:
addShapes = false;
// end exit
}
} while (addShapes); // see if shapes are still being added
return 0;
} // end main
// Function Definitions
//-----------------------------------------------------------
/**
* validate float input
* @param input - from the user
* @return valid float
*/
float validateInput(float input) {
float userInput = input;
bool validInput = false;
// loop until valid input is provided
while (!validInput) {
if (std::cin.fail()) { // if cin fails
Logger::log(ERROR, "validateInput() input was invalid");
std::cin.clear(); // clear the error state of the buffer
// ignore the rest of the line after the first instance of the error
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "You have entered an invalid input. Please retry: ";
std::cin >> userInput; // prompt for new input
} else { // cin was successful
validInput = true;
}
}
return userInput;
}
/**
* create a point
* @return the created point
*/
Point getPoint() {
Point newPoint;
float coordX, coordY;
std::cout << "Please enter the X coordinate for a point: ";
std::cin >> coordX;
coordX = validateInput(coordX);
std::cout << "Please enter the Y coordinate for a point: ";
std::cin >> coordY;
coordY = validateInput(coordY);
newPoint.setCoordinateX(coordX);
newPoint.setCoordinateY(coordY);
return newPoint;
}
/**
* create a line to add to the collection
* @return the created line
*/
std::unique_ptr<Line> createLine() {
std::unique_ptr<Line> newLine(new Line());
std::cout << "Enter coordinates for two points of the line: " << std::endl;
newLine->setPointOne(getPoint());
newLine->setPointTwo(getPoint());
return newLine;
}
/**
* create a rectangle to add to the collection
* @return the created rectangle
*/
std::unique_ptr<Rectangle> createRectangle() {
std::unique_ptr<Rectangle> newRectangle(new Rectangle());
std::cout << "Enter bottom left coordinate and values for rectangle: " << std::endl;
newRectangle->setBottomLeftPoint(getPoint());
float newWidth;
std::cout << "Please enter the width of the rectangle: ";
std::cin >> newWidth;
newWidth = validateInput(newWidth);
newRectangle->setWidth(newWidth);
float newHeight;
std::cout << "Please enter the height of the rectangle: ";
std::cin >> newHeight;
newHeight = validateInput(newHeight);
newRectangle->setHeight(newHeight);
return newRectangle;
}
/**
* create a circle to add to the collection
* @return the created circle
*/
std::unique_ptr<Circle> createCircle() {
std::unique_ptr<Circle> newCircle(new Circle());
std::cout << "Enter point for the center of the circle: " << std::endl;
newCircle->setCenterPoint(getPoint());
float radius;
std::cout << "Please enter the radius of the circle: ";
std::cin >> radius;
radius = validateInput(radius);
newCircle->setRadius(radius);
return newCircle;
}
/**
* create a right triangle to add to the collection
* @return the created triangle
*/
std::unique_ptr<Triangle> createTriangle() {
std::unique_ptr<Triangle> newTriangle(new Triangle());
std::cout << "Enter the coordinates for right angle of the triangle: " << std::endl;
newTriangle->setRightAnglePoint(getPoint());
float height;
std::cout << "Please enter the height of the triangle: ";
std::cin >> height;
height = validateInput(height);
newTriangle->setHeight(height);
float base;
std::cout << "Please enter the base of the triangle: ";
std::cin >> base;
base = validateInput(base);
newTriangle->setBase(base);
return newTriangle;
}
/**
* Print out all the shapes created by the user
* @param shapesVector - vector collection of all the created shapes
* @param shapeCount - number of shapes in the vector collection
*/
void printShapes(std::vector<std::unique_ptr<Shape>>& shapesVector) {
for (int i = 0; i < shapesVector.size(); i++) {
std::cout << std::to_string(i + 1) << ". " << shapesVector[i]->shapeProperties() << std::endl;
}
}