In: Computer Science
Write a C++ program for storing information on on a series of balls collected by a person. The balls should have these characteristics:
1. Diameter in mm
2. Color
3. if the texture of the surface is smooth or rough
4. an identification id/number
The program should allow the person to enter the values for the balls' attributes as they are entered in a database. The program should then offer the choice to save all new data into a file.
Next, the program should offer these operations:
1. List all balls with diameters above 10mm (Show all attribute values)
2. List all balls with less or equal to 10mm (Show all attribute values)
3. Show all balls that are smooth (Show all attribute values as a list)
4. Show all balls that are rough (Show all attribute values as a list)
5. Find a specific ball based on its id (Show all attributes)
6. How many balls of a specific color are in the database? (Show a total)
Program requirements:
1. The program must be OOP
2. Use of an array as temporary buffer to store ball objects
3. Move content of array to file
4. Retrieve all objects in file to an array at run time
5. Use of menus to direct user on the various available options for the program.
6. Use of header files for the classes need for this program (Use a project setup rather than a single app).
Delivery requirements:
1. Source code
2. Sample files generated by the program
3. A word document that summarizes your work with screenshots of your program running and generating outputs based on your menu selections. All menu options must be tested!
4. A conclusion within the above document about your performance in completing this task. This is going to be important for me to read and assess what needs to be done at the beginning of this course to bring each one of you to a level where CS230 will be performed without struggles.
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
class Ball
{
private:
int diameter;
string color;
string surface;
int id;
public:
Ball()
{
this->diameter = 0;
this->color = "";
this->surface = "";
this->id = 0;
}
Ball(int diameter, string color, string surface, int id)
{
this->diameter = diameter;
this->color = color;
this->surface = surface;
this->id = id;
}
int getDiameter(){ return this->diameter; }
string getColor(){ return this->color; }
string getSurface(){ return this->surface; }
int getId(){ return this->id; }
string toString()
{
stringstream ss;
ss << "Id: " << this->id << ", Diameter: " << this->diameter << " mm, Color: " << this->color << ", Surface: " << this->surface;
return ss.str();
}
};
// function prototype
void printMenu();
int main()
{
vector<Ball> balls;
ifstream inFile("ballsInput.txt");
ofstream outFile("ballsOutput.txt");
// read data from file and populate the array "balls"
if(!inFile.is_open())
{
cout << "Error in opening file: ballsInput.txt" << endl;
exit(0);
}
if(!outFile.is_open())
{
cout << "Error in opening file: ballsOutput.txt" << endl;
exit(0);
}
string line;
while(getline(inFile, line))
{
stringstream ss (line);
string s;
vector<string> tokens;
while(getline(ss, s, ','))
{
tokens.push_back(s);
}
int diameter = stod(tokens[0]);
string color = tokens[1];
string surface = tokens[2];
int id = stod(tokens[3]);
Ball ball(diameter, color, surface, id);
balls.push_back(ball);
tokens.clear();
}
inFile.close();
int choice;
do
{
printMenu();
cin >> choice;
switch(choice)
{
case 1:
{
cout << endl << "Balls with diameter above 10mm:" << endl;
outFile << endl << "Balls with diameter above 10mm:" << endl;
for(Ball ball : balls)
{
if(ball.getDiameter() > 10)
{
cout << ball.toString() << endl;
outFile << ball.toString() << endl;
}
}
cout << endl;
outFile << endl;
break;
}
case 2:
{
cout << endl << "Balls with diameter less or equal to 10mm:" << endl;
outFile << endl << "Balls with diameter less or equal to 10mm:" << endl;
for(Ball ball : balls)
{
if(ball.getDiameter() <= 10){
cout << ball.toString() << endl;
outFile << ball.toString() << endl;
}
}
outFile << endl;
cout << endl;
break;
}
case 3:
{
cout << endl << "Balls with a smooth surface:" << endl;
outFile << endl << "Balls with a smooth surface:" << endl;
for(Ball ball : balls)
{
if(ball.getSurface().compare("Smooth") == 0){
cout << ball.toString() << endl;
outFile << ball.toString() << endl;
}
}
outFile << endl;
cout << endl;
break;
}
case 4:
{
cout << endl << "Balls with a rough surface:" << endl;
outFile << endl << "Balls with a rough surface:" << endl;
for(Ball ball : balls)
{
if(ball.getSurface().compare("Rough") == 0){
cout << ball.toString() << endl;
outFile << ball.toString() << endl;
}
}
outFile << endl;
cout << endl;
break;
}
case 5:
{
cout << endl << "Search ball with a specific ID:" << endl;
outFile << endl << "Search ball with a specific ID:" << endl;
int id;
cout << "Enter the id for the ball to search: ";
cin >> id;
bool found = false;
int index = 0;
for(int i = 0; i < balls.size(); i++)
{
if(balls[i].getId() == id)
{
found = true;
index = i;
break;
}
}
if(!found){
cout << "No such ball with id " << id << " was found!" << endl;
outFile << "No such ball with id " << id << " was found!" << endl;
}
else
{
cout << "Match found:\n" << balls[index].toString() << endl;
outFile << "Match found:\n" << balls[index].toString() << endl;
}
outFile << endl;
cout << endl;
break;
}
case 6:
{
cout << endl << "Search ball with a specific color:" << endl;
outFile << endl << "Search ball with a specific color:" << endl;
int count = 0;
string color;
cout << "Enter a color: ";
cin >> color;
for(int i = 0; i < balls.size(); i++)
{
if(balls[i].getColor().compare(color) == 0)
{
count++;
cout << balls[i].toString() << endl;
outFile << balls[i].toString() << endl;
}
}
cout << endl << "Total results: " << count << endl;
outFile << "Total results: " << count << endl;
outFile << endl;
cout << endl;
break;
}
case 0:
{
cout << "Thank you.\nGood Bye!" << endl << endl;
exit(0);
}
default:
cout << "\nInvalid choice\n\n";
}
}while(choice != 0);
}
void printMenu()
{
cout << "1. List all balls with diameters above 10mm\n2. List all balls with less or equal to 10mm\n3. Show all balls that are smooth\n4. Show all balls that are rough\n5. Find a specific ball based on its id\n6. How many balls of a specific color are in the database?\n0. Quit\nEnter choice: ";
}
******************************************************************** SCREENSHOT *******************************************************