#### **CSCI 1300 CS1: Starting Computing** (Solution)

$ 20.99
Category:

Description

#### **Homework 9**
<br>

# Table of contents

1. [Homework Summary](#summary)
1. [Objectives](#objectives)
2. [Background](#background)
3. [Testing Functions](#tests)
* [Void functions with printed output](#void)
* [Non-void functions returning bool or int](#non-void-bool-int)
* [Non-void functions returning double](#non-void-double)
* [Functions manipulating arrays](#test-function-array)
* [Testing class objects](#test-objects)
4. [Questions](#questions) * [Question 0](#q0)
* [Question 1](#q1)
* [Question 2](#q2)
* [Question 3](#q3)
* [Question 4](#q4)
* [Question 5](#q5)
* [Question 6](#q6)
* [Question 7](#q7)
* [Checklist](#check) * [Grading rubric](#rubric)

# Homework Summary <a name=”summary”></a>

For this assignment, we will be creating an aquarium planner to assist aquarists (people who own aquariums) in managing their fish. Question 0 will give you a brief overview of the class you will be creating, and the remaining questions will walk you through implementing each method of the **Aquarium** class.

# Objectives <a name=”objectives”></a>
1. Learn how to work with structs, objects, vectors, and file I/O.
2. Get some extra credit!

# Background <a name=”background”></a>
## **Structures**

In C++, we can define a **structure** using the keyword `struct` like so:
“`cpp struct State
{
string name; int area;
}; // <– semicolon after struct definition
“`

This defines a new type, `State`, that you can use for declaring variables, e.g.
“`cpp
//create a State variable with no name or area
State empty_state;

//create a State variable with a name and area
State colorado{“Colorado”, 104094};
“`
The variables `empty_state` and `colorado` both have two named attributes, called **members** – `name` and `area`. We can access each member using dot notation, e.g.

“`cpp
//set members for empty State empty_state.name = “Texas”; empty_state.area = 268596;

//get members for colorado
cout << colorado.name << ” has an area of” << colorado.area << ” square miles.” << endl;
“`

Expected output:
“`
Colorado has an area of 104094 square miles.
“`

If we want to compare two structs, we cannot do so directly. Instead, we must compare each data member individually to see if they match, e.g.

“`cpp
//check each data member by one
if(colorado.name == emptyState.name && colorado.area == emptyState.area)
{
cout << “These are the same state!” << endl;
} else { cout << “These are not the same state!” << endl;
}
“`

Expected output:
“`
These are not the same state!
“`

## **Structs and Classes**

A class can have a `struct` as a data member, much like how a class could have any other type of data member. It’s important to make sure that the class header file (the .h file) can see the definition of the struct. This can be accomplished by defining the struct inside of the .h file, like below:
“`cpp
// filename: example.h struct State
{ string name; int area;
}; // don’t forget the semicolon

class Example
{ private:
State my_state_; //other data members
public:
//setter accepts a State parameter void setState(State new_state);

//getter returns the State member variable
State getState();

//other member methods
};
“`

Any file that includes the .h file shown above will be able to use instances of the `State` struct, so you will not need to define the `State` struct anywhere else.

## **Vectors of Structs**

Much like how we can have vectors of objects, we can also have vectors of structs. We would define a vector of structs like so:
“`cpp struct State
{ string name; int area;
};

//create a vector of States vector<State> my_states;

//add a state to the vector
State new_state{“Random State”, 123}; my_states.push_back(new_state);
“`

## **When to Use Structs Versus Classes**

# Testing functions <a name=”tests”></a>

Every C++ program you write should include a main function that tests the function you wrote.
There will be different types of test cases you will write depending on the return type of the function.
Listed below is how we expect you to test different types of functions. The process will be different for testing a `void` function, functions that return an `int` or `bool`, and functions that return a `double`.

#### 1. Void Functions with printed output <a name=”void”></a>

For **void** functions that have printed output (i.e. functions that use
`cout` to print to the terminal), call the testing function in the main function. Your tests should include the expected output in comments.

See the sample code below:
“`c++
void checkDiscount(double discount) { if (discount >= 50) {
cout << “Great discount!” << endl;
} else {
cout << “Not a great discount.” << endl;
} return;
}
int main() {
// test 1 for checkDiscount
// expected output: “Great discount!” checkDiscount(82.7);

// test 2 for checkDiscount
// expected output: “Not a great discount.” checkDiscount(22);
return 0;
}
“`
_* For the purpose of this test code snippet, algorithm comments were not included, but they are still expected in your C++ files._

<br>

#### 2. Non-Void Functions returning bool or int <a name=”non-void-boolint”></a>

For functions that return a **bool, char or int**, use **assert statements** from the **cassert** library (`#include <cassert>`) with a conditional expression.
Assert statements contain a conditional expression which will evaluate to
`true` if the function’s actual output matches what’s expected. If the conditional expression evaluates to `false`, then your program will stop running after printing an error message.

For the purpose of this project, functions that return a `bool`, `char` or `int` can be compared to a specific value using the equality operator `==`.

Your test will look something like this:

`assert(<function call> == <value to compare to>);`

* `<function call>` is where you will call the function you want to test with its function parameters.
* `<value to compare to>` is the value you expect the function to return. * `==` is the equality operator, and it compares the equality of both sides of itself.

See the sample code below:
“`c++
#include <iostream>
#include <cassert> using namespace std;

int addInts(int num1, int num2)
{
// add num1 and num2 before returning return num1 + num2;
}

// isDrivingAge() returns true if the given age passed as a parameter
// is greater than or equal to 16, otherwise it returns false. bool isDrivingAge(int age)
{
return age >= 16;
} int main()
{
// test 1 for addInts assert(addInts(5, 6) == 11);

// test 2 for addInts assert(addInts(10, 10) == 20);

// test 3 for drivingAge assert(isDrivingAge(17) == true);

// test 4 for drivingAge assert(isDrivingAge(14) == false);
}
“`
_* For the purpose of this test code snippet, algorithm comments were not included, but they are still expected in your C++ files._
<br>

#### 3. Non-Void Functions returning double <a name=”non-void-double”></a>
For functions that return a **double**, you should use an **assert statement** from the **cassert** header (`#include <cassert>`) with a conditional expression like above. The difference is that you will also need to include the following function in your program:
“`c++
/**
* doublesEqual will test if two doubles are equal to each other within two decimal places.
*/
bool doublesEqual(double a, double b, const double epsilon = 1e-2)
{
double c = a – b;
return c < epsilon && -c < epsilon;
}
“`
Because the `double` type holds so much precision, it will be hard to compare the equality of a function that returns a
double with another double value. To overcome this challenge, we can compare
`double` values within a certain range
of precision or decimal places. The function above compares the equality of two variables `a` and `b` up to two decimal places, and returns `true` if the values of `a` and `b` are equal with each other up to two decimal places.

You will be expected to use this function in conjunction with `assert` statements to test functions that return the type double.

Your test will look something like this:

`assert(doubles_equal(<function call>, <value to compare to>));`

* `<function call>` is where you will call the function you want to test with its function parameters
* `<value to compare to>` is the `double` value you expect the function to return.

See the sample code below:
“`c++
#include <iostream>
#include <cassert> using namespace std;
/**
* doublesEqual will test if two doubles are equal to each other within two decimal places.
*/
bool doublesEqual(double a, double b, const double epsilon = 1e-2)
{
double c = a – b;
return c < epsilon && -c < epsilon;
}
/**
* reciprocal returns the value of 1 divided by the number passed into the function. */
double reciprocal(int num)
{
return 1.0 / num;
} int main()
{
// test 1 for reciprocal
assert(doublesEqual(reciprocal(6), 0.16));
// test 2 for reciprocal
assert(doublesEqual(reciprocal(12), 0.083));
}
“`
For test 1, `reciprocal(6)` is being called, and we expect the function to return the value `0.16`.
The return value of `reciprocal(6)` and `0.16` is passed in as parameters to the `doublesEquals` function,
which will then return `true` if these two values are equal or `false` if they are not.

_* For the purpose of this test code snippet, algorithm comments were not included, but they are still expected in your C++ files._
<br>

#### 4. Functions manipulating arrays <a name=”test-function-array”></a>
Functions which manipulate arrays passed as arguments can be tested by asserting on individual elements of the array after calling the function.
This works even for `void` functions because arrays are passed by reference; even if the function doesn’t return anything, the array in the function is the same array in memory as the one outside the scope of your function.

Note that if the array is an array of `float` or `double`, the
`doubles_equal` function described [here](#non-void-double) should be used to assert on individual array elements.

See the sample code below:
“`cpp
#include<iostream>
#include<cassert> using namespace std;

/**
* Add one to each element of the array.
*/
void addOneToArrayElements(int arr[], int size)
{ for (int i = 0; i < size; i++)
{
arr[i] = arr[i] + 1;
}
}

/**
* doublesEqual will test if two doubles are equal to each other within two decimal places.
*/
bool doublesEqual(double a, double b, const double epsilon = 1e-2)
{
double c = a – b;
return c < epsilon && -c < epsilon;
}

/**
* Add one to each element of the array.
*/
void addOneToArrayElementsDouble(double arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = arr[i] + 1.0;
}
}
int main()
{
// test 1 for addOneToArrayElements int test1Arr[3] = {1, 2, 3}; addOneToArrayElements(test1Arr, 3); assert(test1Arr[0] == 2); assert(test1Arr[1] == 3); assert(test1Arr[2] == 4);

// test 2 for addOneToArrayElements int test2Arr[3] = {8, 10}; addOneToArrayElements(test2Arr, 2); assert(test2Arr[0] == 9); assert(test2Arr[1] == 11);

// test 1 for addOneToArrayElementsDouble double test3Arr[4] = {1.5, 4.6, 9.7, 16.8}; addOneToArrayElementsDouble(testArr3, 4); assert(doublesEqual(testArr3[0], 2.5)); assert(doublesEqual(testArr3[1], 5.6)); assert(doublesEqual(testArr3[2], 10.7)); assert(doublesEqual(testArr3[3], 17.8));
}
“`

#### 5. Testing class objects <a name=”test-objects”></a>
When creating an instance of a class, or an object, the attributes of that object can be tested by asserting on the individual attributes or data members.
Note that if the data attribute is a float or double, the doubles_equal function described [here](#non-void-double) should be used to assert on the value.

Additionally, note that if the data attribute is an array, follow the [“Functions manipulating arrays” section](#test-function-array) on how to assert on individual array elements.
See the sample code below:
“`c++ class Animal { private:
string sound_; string name_; int age_; public: Animal();
Animal(string, string, int);
string getSound(); string getName(); int getAge();
};

Animal::Animal() { sound_ = “”; name_ = “”; age_ = 0;
}

Animal::Animal(string sound, string name, int age)
{ sound_ = sound; name_ = name; age_ = age;
}
string Animal::getSound()
{
return sound_;
}
string Animal::getName()
{
return name_;
}
int Animal::getAge()
{ return age_;
} int main()
{
Animal default_dog; assert(default_dog.getSound() == “”); assert(default_dog.getAge() == 0); assert(default_dog.getName() == “”);

Animal actual_dog(“Bark bark!”, “Pluto”, 2); assert(actual_dog.getSound() == “Bark bark!”); assert(actual_dog.getAge() == 2); assert(actual_dog.getName() == “Pluto”);
}
“`
_* For the purpose of this test code snippet, algorithm comments were not included, but they are still expected in your C++ files._

# Questions <a name=”questions”></a>
## **Question 0 (0 points): The <code>Fish</code> Struct and
<code>Aquarium</code> Class** <a name=”q0″></a>

First, create a **Fish** struct with the members described below. This struct will be used in your **Aquarium** class.

**Fish Members:**
| Attribute | Description |
| ——— | ———– |
| `string`: name | The name of a fish, e.g. Minnow |
| `int`: gallons_required | The number of gallons of water required for this fish |

**Hint:** You should include the definition of the `Fish` struct in your `Aquarium.h` file. See the [background](#background) section on Structs and Classes for more information.

<br>

The **Aquarium** class is described below:

**Data Members (private)**
| Data Members | Description |
| ———— | ———– |
| `string` aquarist_name | The name of the aquarium owner. |
| `static const int` tank_size_gallons | The maximum number of gallons of water in the aquarium. Set to 12. |
| `int` gallons_used | The total number of gallons of water ‘used’ by fish; we’ll use this to determine if there is enough space for another fish in the aquarium. |
| `vector<Fish>` available_fish | A vector of Fish available to add to aquarium. |
| `vector<Fish>` selected_fish | A vector of Fish that are currently in the aquarium. |

<br>

**Member Functions (public)**

This table provides an overview of each function and what it does. Questions 1 through 7 provide more details about return types and handling edge cases.

| Member Functions | Description |
| —————- | ———– |
| Parameterized Constructor | Set `aquarist_name` to the `string` parameter and `gallons_used` to 0. |
| `getAquaristName()` | Returns the value of `aquarist_name` member variable. |
| `getGallonsUsed()` | Returns the value of `gallons_used` member variable. |
| `loadFish(string)` | Takes a `string` (the name of the file to be read) and populates the `available_fish` vector with Fish. Returns `true` if the Fish were loaded successfully and `false` otherwise. |
| `displayAvailableFish()` | Displays the Fish listed in the `available_fish` vector, one per line. Returns `void`. |
| `addFish(string)` | Takes a `string` (the name of a Fish) and adds the
Fish to the `selected_fish` vector. Returns a an `int` depending on the success of adding the fish.|
| `removeFish(string)` | Takes a `string` (the name of a Fish) and removes the Fish from the list of `selected_fish`. Returns a `bool`- true if removed successfully, false otherwise.|
| `writeAquariumContents(string)` | Takes a `string` (the path to the file) and writes a list of all the fish in the aquarium to a file. Returns `false` if the file cannot be opened, and `true` otherwise. |

**Note:** Question 0 serves to describe the Fish struct and Aquarium class. There is no CodeRunner or other deliverable for this question.

## **Question 1 (3 points): The <code>Aquarium</code> Constructor,
<code>getAquaristName</code> and <code>getGallonsUsed</code>** <a name=”q1″></a>

For this question, you will create three methods for the `Aquarium` class. The function specifications are listed below:

**The <code>Aquarium</code> constructor**

This constructor will accept one `string` parameter for the aquarist’s name, and set `gallons_used` to 0.

* Accept one parameter:
* `string`: The name of the aquarist. The data member `aquarist_name` should be set to this parameter.
* Set the `gallons_used` data member to 0.

**The <code>getAquaristName</code> method**
This method should accept no parameters and returns the `aquarist_name` data member.

* Accept no parameters.
* This function returns the `aquarist_name` data member as a `string`.

**The <code>getGallonsUsed</code> method**

This method accepts no parameters and returns the `gallons_used` data member.

* Accept no parameters.
* This function returns the `gallons_used` data member as an `int`.

**Example Usage**
“`cpp
//create an aquarium object Aquarium billys_aquarium(“Billy”); //test getAquaristName method
cout << billys_aquarium.getAquaristName() << endl;

//test getGallonsUsed method
cout << billys_aquarium.getGallonsUsed() << endl;
“`

Expected output:
“`
Billy
0
“`

**Note:** Although you haven’t finished implementing all of the member methods, you should still include them all in your class definition (.h file including the Fish struct), and paste the **entire** class definition (.cpp file) into the CodeRunner for each question.

When you are done implementing these methods, head on over to Coderunner and paste **all** of your .h file contents and the implementation for **only the three methods you just created** into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the CodeRunner – that’s already been included for you. There is nothing to submit to Canvas for this question; you will submit your `Aquarium.h` and `Aquarium.cpp` files to Canvas once you have finished implementing all of the member methods.

## **Question 2 (6 points): The <code>loadFish</code> Method** <a name=”q2″></a>

For this question you will implement the `loadFish` member method. This method should:

* Accept one parameter:
* `string`: The filepath to be read from
* Use `ifstream` and `getline` to read data from the file, creating a new
`Fish` struct for each line, and put that struct into the `available_fish` member vector.
* Empty lines should not be added to the vector.
* Assume that the txt files provided do **not** have duplicate fish listed. * You should use the `stoi()` method to convert the number of gallons of water required for each Fish from a `string` to an `int`.
* **Hint**: You can use the `split()` function from Homework 5 with a comma
(`,`) as the delimiter. * This function should return:
* `true` if the file was opened successfully
* `false` otherwise

We have provided an example [fish_example.txt](data_files/fish_example.txt) file for you to use while developing your solution. A preview of this file is displayed below:
“`
Lyretail Guppy,1
Red Panda Guppy,2 Elephant Ear Betta,3

Platinum Ogon Koi,5
Doitsu Koi,4
Red Oranda Goldfish,4
Black Koi,5

Jack Dempsey Cichlid,3
Blue Peacock Cichlid,3
“`

**Example Usage**
“`cpp
//create an aquarium object Aquarium billys_aquarium(“Billy”);

//test return value for a file that doesn’t exit cout << billys_aquarium.loadFish(“fake_file.txt”) << endl;

//test return value for a file that does exit
cout << billys_aquarium.loadFish(“fish_example.txt”) << endl;
“`

Expected output:
“`
0
1
“`

When you are done implementing this method, head on over to Coderunner and paste **all** of your .h file contents and the implementation for all of **the member methods from questions 1 and 2** into the answer box. Be sure to also include your `split` function in your answer if you need to use it. Do not paste your version of displayAvailableFish(). We will be using our own implementation of this function to test your code. There is nothing to submit to Canvas for this question; you will submit your `Aquariuam.h` and `Aquariuam.cpp` files to Canvas once you have finished implementing all of the member methods.

## **Question 3 (3 points): The <code>displayAvailableFish</code> Method** <a name=”q3″></a>

For this question you will implement the `displayAvailableFish` member method. This method should:

* Accept no parameters.
* This function will return `void`.
* The function should print the following:
* If there are no fish available, your method should print `There are no fish available.`
* If there are fish available, your method should print `Fish available to add to aquarium:`, followed by the contents of the `available_fish` member vector, with each entry on a new line (see example output below).
**Example 1:** There are no fish stored in the `available_fish` vector.
“`cpp
//create aquarium object
Aquarium billys_aquarium(“Billy”);

//call displayAvailableFish
billys_aquarium.displayAvailableFish();
“`

Expected output:
“`
There are no fish available.
“`

**Example 2:** There are fish stored in the `available_fish` vector.
“`cpp
//create aquarium object Aquarium sams_aquarium(“Sam”);

//load fish
sams_aquarium.loadFish(“fish_15.txt”);

//call displayAvailableFish sams_aquarium.displayAvailableFish();
“`

Expected output:
“`
Fish available to add to aquarium:
Minnow – 1
Fancy Guppy – 1
Blue Neon Guppy – 1
Elephant Ear Guppy – 2
Yellow Guppy – 1
Lyretail Guppy – 1
Red Pand Guppy – 2
Elephany Ear Betta – 3
Rose Petal Betta – 3
Halfmoon Betta – 4
Paradise Betta – 3
Blue Crowntail Betta – 4
Neon Tetra – 2
Electric Green Longfin Tetra – 2 Sunburst Orange Tetra – 2
“`
When you are done implementing this method, head on over to Coderunner and paste all of your .h file contents and the implementation for **all of the member methods from questions 1, 2 and 3** into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the CodeRunner – that’s already been included for you. There is nothing to submit to Canvas for this question; you will submit your `Aquarium.h` and `Aquarium.cpp` files to Canvas once you have finished implementing all of the member methods.

## **Question 4 (6 points): The <code>addFish</code> Method** <a name=”q4″></a>
For this question you will implement the `addFish` member method. This method should:

* Accept one parameter:
* `string`: The name of the Fish to be added to the aquarium.
* The function should look through the `available_fish` vector to ensure that this Fish is available to add to the aquarium
* **Note:** The Fish name should be case **insensitive**, e.g. both
“Elephant Ear Guppy” and “elePHant eaR guPPY” refer to the same Fish.
* If the Fish is available to add, the method should add the Fish to the
`selected_fish` data member.
* Be sure to update the `gallons_used` data member appropriately! * If the Fish has been added to the Aquarium successfully, the Fish should be removed from the `available_fish` member vector.
* **Note:** The number of gallons used by the fish cannot exceed the maximum tank size, which is stored in the constant data member `tank_size_gallons`. If adding the Fish to the Aquarium would result in too many `gallons_used`, then this function should **not** add the Fish to the aquarium. * The function should return one of the following `int` values:
* -1 if the Fish is not available to add to the Aquarium
* 0 if the Fish is available but there isn’t enough space the Aquarium
(ie, `gallons_used` would be more than `tank_size_gallons`) * 1 if the Fish was successfully added to the Aquarium.

**Example Usage**
“`cpp
//create an aquarium object Aquarium billys_aquarium(“Billy”);

//load fish from file
billys_aquarium.loadFish(“fish_example.txt”);

//try adding a Fish that doesn’t exist + print result cout << billys_aquarium.addFish(“not a real fish”) << endl;

//add a fish that does exist + print result
cout << billys_aquarium.addFish(“platinum ogon koi”) << endl;

// add two more fish, for total of 11 gallons of water used
// not printing the results of these billys_aquarium.addFish(“eLePhAnt EaR bEtTa”); billys_aquarium.addFish(“Blue Peacock Cichlid”);

//try adding another fish that would result in 13 gallons used
// print result of adding fish
cout << billys_aquarium.addFish(“red PANDA guppy”) << endl;
“`

Expected output:
“`
-1
1
0
“`

When you are done implementing this method, head on over to Coderunner and paste all of your .h file contents and the implementation for **all of the member methods from questions 1, 2, 3 and 4** into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the
CodeRunner – that’s already been included for you. There is nothing to submit to Canvas for this question; you will submit your `Aquarium.h` and
`Aquarium.cpp` files to Canvas once you have finished implementing all of the member methods.

## **Question 5 (6 points): The <code>removeFish</code> Method** <a name=”q5″></a>

For this question you will implement the `removeFish` method. This method should:

* Accept one parameter:
* `string`: The fish to be removed from the aquarium.
* This function should search through the `selected_fish` data member to find the appropriate fish to remove.
* **Note:** The fish name should be case **insensitive**, e.g. both
“minnow” and “Minnow” refer to the same fish.
* If the fish is currently selected, it should be removed from the
`selected_fish` data member.
* Be sure to update the `gallons_used` data member appropriately!
* Once the Fish has been removed, it should be put back into the
* The function should return one of the following `bool` values:
* `true` if the fish was removed successfully
* `false` otherwise. This includes the case where the fish does not exist at all.

**Example Usage**
“`cpp
//create an aquarium object
Aquarium marias_fish(“maria”);

//load fish
marias_fish.loadFish(“fish_15.txt”);

//add a fish
marias_fish.addFish(“Cardinal Tetra”);

//remove fish that has not been selected
cout << marias_fish.removeFish(“Butterfly Koi”) << endl;

// remove fish that has been selected
cout << marias_fish.removeFish(“Cardinal Tetra”) << endl;
“`

Expected output:
“`
0
1
“`

When you are done implementing this method, head on over to Coderunner and paste all of your .h file contents and the implementation for **all of the member methods from questions 1, 2, 3, 4 and 5** into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the
CodeRunner – that’s already been included for you. There is nothing to submit to Canvas for this question; you will submit your `Aquarium.h` and
`Aquarium.cpp` files to Canvas once you have finished implementing all of the member methods.

## **Question 6 (5 points): The <code>writeAquariumContents</code> Method**
<a name=”q6″></a>

For this question you will implement the `writeAquariumContents` member method. This method should:

* Accept one parameter:
* `string`: The filepath for the output to be saved.
* This function should write a list of the Fish currently in the Aquarium to the file specified by the filepath.
* This method should output the following to the specified txt file: * If there are no Fish in the Aquarium, the function should write `There are no fish in <aquarist’s name>’s Aquarium.`.
* Otherwise, the function should write `<aquarist’s name>’s Aquarium
(<gallons_used> of <tank_size_gallons> gallons of water used):` on the first line, followed by the Fish name and gallons required, one per line.
* This function should return one of the following `bool` values:
* `true` if the aquarium’s contents were written to the file successfully
(regardless of whether or not there are any fish in the aquarium).
* `false` if the file could not be opened.

**Example 1:** Multiple Fish in the Aquarium.
“`cpp
//create an aquarium object Aquarium billys_aquarium(“Billy”);

//load fish from file
billys_aquarium.loadFish(“fish_example.txt”);

//add some fish
billys_aquarium.addFish(“platinum ogon koi”); billys_aquarium.addFish(“eLePhAnt EaR bEtTa”); billys_aquarium.addFish(“Blue Peacock Cichlid”);

billys_aquarium.writeAquariumContents(“billysAquarium.txt”);
“`

Expected contents of `billysAquarium.txt`:
“`
Billy’s Aquarium (11 of 12 gallons of water used):
Platinum Ogon Koi – 5
Elephant Ear Betta – 3
Blue Peacock Cichlid – 3
“`

**Example 2:** The Aquarium is empty (no fish). “`cpp
//create aquarium object
Aquarium billys_aquarium(“Billy”);

//save aquarium contents
billys_aquarium.writeAquariumContents(“billysAquarium.txt”);
“`

Expected contents of `billysAquarium.txt`:
“`
There are no fish in Billy’s Aquarium.
“`

When you are done implementing this method, head on over to Coderunner and paste **the entirety** of your header (Aquarium.h) and implementation
(Aquarium.cpp) files into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the CodeRunner – that’s already been included for you. You will submit your (now completed) Aquarium.h and Aquarium.cpp files to Canvas for this question, along with a
aquariumDriver.cpp file that tests out your new Aquarium class (See the [testing class objects](#test-objects) section above for more details about how to test your new class).

## **Question 7 (6 points): An Aquarium Menu** <a name=”q7″></a>

For this question, you will create a file called **aquariumMenu.cpp** that will provide a menu for an aquarist to manage their aquarium. In this file, you should create a main function that begins by prompting the user for their name:
“`
Welcome to the aquarium manager. Please enter the aquarist’s name:
“`
Once the user has entered their name, your program should create an `Aquarium` object with the attribute `aquarist_name` set to the name entered by the user. Then, your program should display the following menu:
“`
–Aquarium Manager– 1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
“`
This menu should run on a loop, continually displaying the options above until the user chooses to exit. You should make use of the functions you wrote previously, call them with appropriate input, and process the values they return. The behavior of each menu optioon is explained below:
**Option 1: loadFish**

* Prompt the user for a file path:
“`
Please enter a filepath:
“`
* Pass the file path to your `loadFish` member function.
* If `loadFish` returns `true`, print:
“`
Fish loaded successfully. “`
* If `loadFish` returns `false`, print:
“`
Could not open file. No fish loaded.
“`

**Option 2: Display available fish**

* Call your `displayAvailableFish` member function.

**Option 3: Add a fish**

* Prompt the user to enter the fish they want to add:
“`
Which fish would you like to add?
“`
* Use your `addFish` member function to add the fish to the aquarium.
* If `addFish` returns -1, print
“`
Could not find fish: <fish name>.
“`
* If `addFish` returns 0, print
“`
The aquarium is already at <gallons_used> gallons and this fish cannot be added.
“`
* If `addFish` returns 1, print
“`
Fish added successfully.
“`

**Option 4: Remove Fish**

* Prompt the user to enter the fish they would like to remove:
“`
Which fish would you like to remove?
“`
* Use your `removeFish` member function to remove the Fish from the aquarium.
* If `removeFish` returns `false`, print
“`
<fish name> not found.
“`
* If `removeFish` returns `true`, print
“`
Fish removed successfully.
“`

**Option 5: Save aquarium contents**

* Prompt the user for a file path
“`
Where would you like to save your aquarium?
“`
* Pass the file path to your `writeAquariumContents` member function.
* If `writeAquariumContents returns `false`, print
“`
Could not open file. Aquarium was not saved.
“`
* If `writeAquariumContents` returns `true`, print
“`
Aquarium saved successfully.
“`

**Option 6: Exit**

* Display a parting message
“` Goodbye!
“`
* End the program.

**Invalid input**
If the user inputs a value that is not one of the menu options, print
“`
Invalid input. Please enter a number between 1 and 6.
“`
And then display the menu again. Assume menu input will always be numeric.

Below is a **sample run** of the `aquariumMenu.cpp` program. User input is in
**bold**.
<pre><code>Welcome to the aquarium manager. Please enter the aquarist’s name:
<b>Emily Voigt</b>
–Aquarium Manager– 1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>1</b>
Please enter a filepath:
<b>fileDoesntExist.txt</b>
Could not open file. No fish loaded.
–Aquarium Manager– 1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>1</b>
Please enter a filepath:
<b>fish_15.txt</b>
Fish loaded successfully.
–Aquarium Manager– 1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>2</b>
Fish available to add to aquarium:
Minnow – 1
Fancy Guppy – 1
Blue Neon Guppy – 1
Elephant Ear Guppy – 2
Yellow Guppy – 1
Lyretail Guppy – 1
Red Pand Guppy – 2
Elephany Ear Betta – 3
Rose Petal Betta – 3
Halfmoon Betta – 4
Paradise Betta – 3
Blue Crowntail Betta – 4
Neon Tetra – 2
Electric Green Longfin Tetra – 2
Sunburst Orange Tetra – 2
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>0</b>
Invalid input. Please enter a number between 1 and 6.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>3</b>
Which fish would you like to add?
<b>Halfmoon Betta</b> Fish added successfully.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>3</b>
Which fish would you like to add?
<b>Asian Arowana</b> Asian Arowana not found.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>3</b>
Which fish would you like to add?
<b>Blue Crowntail Betta</b> Fish added successfully.
–Aquarium Manager– 1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>3</b>
Which fish would you like to add?
<b>Neon Tetra</b> Fish added successfully.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>3</b>
Which fish would you like to add?
<b>Paradise Betta</b>
Aquarium is already at 10 gallons and this fish cannot be added
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>2</b>
Fish available to add to aquarium:
Minnow – 1
Fancy Guppy – 1
Blue Neon Guppy – 1
Elephant Ear Guppy – 2
Yellow Guppy – 1
Lyretail Guppy – 1
Red Pand Guppy – 2
Elephany Ear Betta – 3
Rose Petal Betta – 3
Paradise Betta – 3
Electric Green Longfin Tetra – 2
Sunburst Orange Tetra – 2
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>4</b>
Which fish would you like to remove?
<b>Rose Petal Betta</b> Rose Petal Betta not found.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>4</b>
Which fish would you like to remove?
<b>Neon Tetra</b>
Neon Tetra removed successfully.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>2</b>
Fish available to add to aquarium:
Minnow – 1
Fancy Guppy – 1
Blue Neon Guppy – 1
Elephant Ear Guppy – 2
Yellow Guppy – 1
Lyretail Guppy – 1
Red Pand Guppy – 2
Elephany Ear Betta – 3
Rose Petal Betta – 3
Paradise Betta – 3
Electric Green Longfin Tetra – 2
Sunburst Orange Tetra – 2
Neon Tetra – 2
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>5</b>
Where would you like to save your aquarium?
<b>aquariumSchedule.txt</b> Aquarium saved successfully.
–Aquarium Manager–
1. Load fish
2. Display available fish
3. Add a fish
4. Remove a fish
5. Save aquarium contents
6. Exit
<b>6</b>
Goodbye!</pre></code>

**Expected contents of <code>aquariumSchedule.txt</code>**:
“`
Emily Voigt’s Aquarium (8 of 12 gallons of water used):
Halfmoon Betta – 4
Blue Crowntail Betta – 4
“`

When you are done implementing this method, head on over to Coderunner and paste **the entirety** of your header (Aquarium.h) and implementation (Aquarium.cpp) files along with your newly created driver file (aquariumMenu.cpp) into the answer box. Do not paste in any `#include` directives or `using namespace std;` into the CodeRunner – that’s already been included for you. You will submit your aquariumMenu.cpp file to Canvas for this question.

## Checklist <a name=”check”></a>

Here is a checklist for submitting the assignment:

2. **C++ files**. When you are finished with all the questions, zip all of your .cpp and .h files together. Submit the single zip file under the assignment **”Homework 9 (optional extra-credit)”** on Canvas. * All files should be named as specified in each question, and they should compile and run on Coderunner (pass all test cases) and in VSCode to earn full points.
* Our TAs will be grading the style of your code and comments. Please see the style guide on Canvas for more details.
* At the top of each file, write your name in the format shown below. * Your aquariumDriver.cpp file should contain a main that tests the class member methods you created you wrote, like below. **You should include at least 3 test cases for each member method.** In general, we recommend you try to include as many test cases as you believe are necessary to ensure that your code works properly. Deciding on how many test cases to include is an important skill to learn, since the number of necessary test cases can change between programs. See [testing functions](#tests) for examples demonstrating testing.
* Please be sure to also include function headers that contain the algorithm implemented within the function, expressed in pseudocode. You will need to include these headers above every member function except for the constructor, `getAquaristName`, and `getGallonsUsed`. You can refer to the example below for more details.

* **Style Example:**
![style example](https://github.com/CSCI1300-StartingComputing/CSCI1300Fall2022/blob/main/project/project2/images/style.jpeg)

* The zip file should be named, **hmwk9_lastname.zip**. It should have the following 4 files:
* Aquarium.cpp
* Aquarium.h
* aquariumDriver.cpp
* aquariumMenu.cpp

## Homework 9 points summary <a name=”rubric”></a>

|Criteria|Points|
|–|–|
| Question 0 | 0 |
| Question 1 | 3 |
| Question 2 | 6 |
| Question 3 | 3 |
| Question 4 | 6 |
| Question 5 | 6 |
| Question 6 | 5 |
| Question 7 | 6 |
|C++ File submission (Style, Comments, Tests and Driver files)|10| |Total|45|

Reviews

There are no reviews yet.

Be the first to review “#### **CSCI 1300 CS1: Starting Computing** (Solution)”

Your email address will not be published. Required fields are marked *