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.
// ball.h
#ifndef BALL_H_
#define BALL_H_
#include <iostream>
using namespace std;
class Ball
{
private:
double diameter;
string color;
string texture;
int id;
public:
Ball();
Ball(double diameter, string color, string texture, int id);
void setDiameter(double diameter);
double getDiameter();
void setColor(string color);
string getColor();
void setTexture(string texture);
string getTexture();
void setId(int id);
int getId();
};
#endif /* BALL_H_ */
//end of ball.h
// ball.cpp
#include "ball.h"
Ball::Ball()
{
diameter=0;
color="";
texture="";
id=0;
}
Ball::Ball(double diameter, string color, string texture, int id)
{
this->diameter = diameter;
this->color = color;
this->texture = texture;
this->id = id;
}
void Ball::setDiameter(double diameter)
{
this->diameter = diameter;
}
double Ball::getDiameter()
{
return diameter;
}
void Ball::setColor(string color)
{
this->color = color;
}
string Ball::getColor()
{
return color;
}
void Ball::setTexture(string texture)
{
this->texture = texture;
}
string Ball::getTexture()
{
return texture;
}
void Ball::setId(int id)
{
this->id = id;
}
int Ball::getId()
{
return id;
}
//end of ball.cpp
// main.cpp : Driver program to implement the main program
#include <iostream>
#include <fstream>
#include "ball.h"
using namespace std;
int main() {
int n;
double diameter;
string color, texture;
int id, texture_choice;
// input of number of ball objects to create
cout<<"Enter the number of balls you want to enter : ";
cin>>n;
Ball balls[n]; // create an array of ball objects
// loop to input details of ball objects
for(int i=0;i<n;i++)
{
cout<<"\nEnter details for ball-"<<(i+1)<<" : "<<endl;
cout<<"Diameter (in mm) : ";
cin>>diameter;
cout<<"Color : ";
cin>>color;
cout<<"Texture of the surface( 1. rough , 2. smooth ) : ";
cin>>texture_choice;
cout<<"Identification number(ID) : ";
cin>>id;
balls[i].setDiameter(diameter);
balls[i].setColor(color);
balls[i].setId(id);
if(texture_choice == 1)
balls[i].setTexture("rough");
else
balls[i].setTexture("smooth");
}
// open an output file
ofstream fout("balls.txt");
// loop to write the data of balls in output file (each field is separated by a single space)
for(int i=0;i<n-1;i++)
{
fout<<balls[i].getId()<<" "<<balls[i].getDiameter()<<" "<<balls[i].getColor()<<" "<<balls[i].getTexture()<<endl;
}
fout<<balls[n-1].getId()<<" "<<balls[n-1].getDiameter()<<" "<<balls[n-1].getColor()<<" "<<balls[n-1].getTexture();
fout.close(); //close the file
ifstream fin("balls.txt"); // open the file as input file
n=0;
// read till the end of file
while(!fin.eof())
{
// load the data into the array
fin>>id>>diameter>>color>>texture;
balls[n].setDiameter(diameter);
balls[n].setId(id);
balls[n].setColor(color);
balls[n].setTexture(texture);
n++;
}
fin.close(); //close the file
int choice;
// loop that continues till the user wants
do
{
cout<<endl;
cout<<"1. List all balls with diameters above 10mm"<<endl;
cout<<"2. List all balls with less or equal to 10mm"<<endl;
cout<<"3. Show all balls that are smooth"<<endl;
cout<<"4. Show all balls that are rough "<<endl;
cout<<"5. Find a specific ball based on its id"<<endl;
cout<<"6. How many balls of a specific color are in the database?"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter a choice(1-7) : ";
cin>>choice;
switch(choice)
{
case 1: // list all balls with diameter > 10
for(int i=0;i<n;i++)
{
if(balls[i].getDiameter() > 10)
cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;
}
break;
case 2: // list all balls with diameter <= 10
for(int i=0;i<n;i++)
{
if(balls[i].getDiameter() <= 10)
cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;
}
break;
case 3: //list all smooth balls
for(int i=0;i<n;i++)
{
if(balls[i].getTexture() == "smooth")
cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;
}
break;
case 4: // list all rough balls
for(int i=0;i<n;i++)
{
if(balls[i].getTexture() == "rough")
cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;
}
break;
case 5: // find and display the details of the ball whose id is given
{
bool found=false;
cout<<"Enter the id of the ball : ";
cin>>id;
// loop to find the ball with the given id
for(int i=0;i<n;i++)
{
if(balls[i].getId() == id)
{
cout<<"ID : "<<balls[i].getId()<<" Diameter : "<<balls[i].getDiameter()<<"mm Color : "<<balls[i].getColor()<<" Texture : "<<balls[i].getTexture()<<endl;
found = true;
break;
}
}
if(!found) //check if ball was found
cout<<"No ball with id "<<id<<" found"<<endl;
break;
}
case 6: // count the number of balls of a specific color
{
int count = 0;
cout<<"Enter the color of the ball : ";
cin>>color;
for(int i=0;i<n;i++)
{
if(balls[i].getColor() == color)
count++;
}
cout<<"Number of balls of color "<<color<<" : "<<count<<endl;
break;
}
case 7: // exit the loop
break;
default: // wring choice
cout<<"Invalid choice"<<endl;
}
}while(choice != 7);
return 0;
}
//end of program
Output:
File: