Question

In: Computer Science

Write a C++ program for storing information on on a series of balls collected by a...

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.

Solutions

Expert Solution

#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 *******************************************************


Related Solutions

Write a C++ program for storing information on on a series of balls collected by a...
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....
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT