Question

In: Computer Science

C++ only Large Program Create a magical creature (or monster) zoo inventory program that will allow...

C++ only

Large Program

Create a magical creature (or monster) zoo inventory program that will allow a zookeeper to add magical creatures (either manually or from a file), delete a creature, and display all creatures in the zoo.

The zoo creatures will be organized in a linked list.

----------------------------------------------------------------------------

  • Zoo.cpp – this file will contain your main function that will allow the user to enter, delete, and print creatures. The main function will create the linked list of creatures.

  • Creature.h – class specification for the Creature class

  • Creature.cpp – class implementation for the Creature class

  • LinkedList.h – a Singly-Linked List template class that you create (DO NOT USE THE LIST CLASS PROVIDED IN THE STANDARD TEMPLATE LIBRARY). All the member functions should be defined in this file as well (either inline or below the class declaration).

  • creatureFile.txt – this is the text file that contains creature data in it. You should submit a file with at least five creatures/monsters.

----------------------------------------------------------------------------

linkedlist.h

Create a template class (LinkedList.h) that declares a singly-linked list. This template class should be able to create a linked list of any type of object.

LinkedList class should have the following attributes:

  • A structure (struct) ADT named ListNode, which defines a node with a value & a pointer to the next ListNode.

  • A pointer to a ListNode named head

  • A pointer to a ListNode named tail

  • An integer named numNodes

LinkedList class should have the following member functions:

  • Constructor – should initialize head & tail to point to nothing and initialize numNodes to zero

  • Destructor – like a “RemoveAll” function – should deallocate memory for all existing nodes

  • getLength – should return the number of nodes in the list

  • getNodeValue – this function accepts a position (integer) as a parameter and then returns the value (object) inside the node at that position

  • appendNode – this function accepts a value (object) as a parameter. It creates a new node at the end of the list with the sent object inside of it and increments the number of nodes. This function does not return anything.

  • deleteNode – this function accepts a position (integer) as a parameter, which indicates the position of the node to be deleted. It updates the links of the nodes on either side of this node to be deleted. Then it deletes the node. Then, it decrements the number of nodes.

----------------------------------------------------------------------------

Creature.cpp and

Creature.h

Create a class specification (Creature.h) & class implementation (Creature.cpp) file for a class named Creature. The purpose of the Creature class is to allow for creation of a Creature object.

Creature should have the following attributes:

  • The creature’s name (string)

  • A description of the creature (string)

  • The cost (float) of the creature (per month) to care for it (food, grooming, destruction, etc)

  • A boolean representing whether the creature is dangerous or not (1 means true – it is dangerous and 0 means false, it is not dangerous)

Creatures should have the following member functions:

  • Constructor – default constructor (doesn’t have to have anything inside it)

  • Constructor with parameters – string, string, bool, & float – should initialize the creature’s attributes

  • Accessor functions

    • getName

    • getDescription

    • getDangerous

    • getCost

  • Mutator functions

    • setName

    • setDescription

    • setDangerous

    • setCost

  • printCreature – a function to print a single creature’s information to the screen in a nice, easy to read format

  • printCreatureToFile – a function to print a single creature’s information to the file – unformatted – one piece of information per line. This is so the program would be able to read the creature’s information back later.

----------------------------------------------------------------------------

zoo.cpp

This is the driver. This file should have a main function and several other functions to organize or help the main function as described below:

  • main – This function should create a creature linked list, based on the LinkedList template class you create. Then it should display a menu of four options until the user chooses option 4, which is to end the program. Look at sample output to see the flow of the program.

    • If user chooses option 1, then a sub-menu should be shown to ask user if they want to enter creatures manually or from file. If manually, then the enterMagicalCreature function should be called, sending the address of the creatureList to this function. If from a file, then the enterMagicalCreatureFromFile function should be called, sending the address of the creature list to this function.

    • If user chooses option 2, then the deleteCreature function should be called, sending the address of the creature list to this function.

    • If user choses option 3, then the printCreatures function should be called, sending the address of the creature list to this function.

    • If user chooses option 4, then the program should ask the user if they want to save their creature list to a file. If user chooses yes, then call the saveCreaturesToFile function, sending the creature list to this function. Print “GOODBYE!” before the program ends.

  • enterMagicalCreature – this function should ask the user for the creature’s name, description, if it is dangerous, and the cost per month to care fo rhte creature. Then it should create a new creature object with this data. Then, it should append the creature object to the linked creature list. Then it should print a confirmation that the creature (print creature’s name) has been added to the zoo. The function should then ask the user if they want to add more creatures. If they answer yes, then repeat this process. Otherwise, the function should end and it doesn’t return any data.

  • enterMagicalCreatureFromFile – this function should ask the user for the name of the file they want to read from. Then, if the file is able to be opened (print a message if can’t be opened) then read the creature data from the file (one at a time) with a loop and after reading in one creature, create a new creature object with this data, then append the creature to the creature linked list. After reading all the creatures from the file & adding them to the linked list, print how many creatures FROM THE FILE were added to the zoo. This may or may not be the current number of creatures in the linked list! This function does not return any data.

  • deleteCreature – this function should first print a numbered list of the names of all the creatures in the linked list. Then ask the user which creature number they want to delete. Then the creature should be removed from the linked list. A confirmation should be printed out that the creature was removed. This function does not return anything.

  • printCreatures – this function should print “THERE ARE NO CREATURES AT YOUR ZOO!” if there are no creatures in the linked list. If there are creatures in the linked list then it should print each creature’s detail in the list. This function does not return anything.

  • saveCreaturesToFile – this function should either print “THERE ARE NO CREATURES AT YOUR ZOO!” if there are no creatures in the linked list. If there are creatures in the linked list then it should ask the user for the filename that they wish to use to save the creatures and then use a loop to get a creature from each node, and then call this creature’s printCreatureToFile function, sending the filename to the function.

--------------------------------------------------------------------------

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LinkedList.h

#include <iostream>
#include "Creature.cpp"

template <class ItemType>
struct ListNode
{
    ItemType node;
    ListNode *next;
    ListNode(ItemType T)
    {
        node = T;
        next = NULL;
    }
};

template <class ItemType>
class LinkedList
{
private:
    ListNode<ItemType> *head, *tail;
    int numNodes;

public:
    LinkedList()
    { // Constructor
        head = tail = NULL;
        numNodes = 0;
    }

    ~LinkedList()
    { // Destructor
        while (getLength() > 0)
        {
            deleteNode(1);
        }
    }

    int getLength();
    ItemType getNodeValue(int id);
    void appendNode(ListNode<ItemType> *newNode);
    void deleteNode(int id);
};

template <class ItemType>
int LinkedList<ItemType>::getLength()
{
    return numNodes;
}

template <class ItemType>
ItemType LinkedList<ItemType>::getNodeValue(int id)
{
    ListNode<ItemType> *copy = head;
    int i = 1;
    while (copy)
    {
        if (i == id)
            return copy->node;
        copy = copy->next;
        i++;
    }
    return ItemType();
}

template <class ItemType>
void LinkedList<ItemType>::appendNode(ListNode<ItemType> *newNode)
{
    if (!head && !tail)
    { // if list is empty
        head = newNode;
        tail = newNode;
        numNodes = 1;
        return;
    }

    tail->next = newNode; // else
    tail = newNode;
    numNodes++;
}

template <class ItemType>
void LinkedList<ItemType>::deleteNode(int id)
{
    if (id > numNodes)
        return;

    ListNode<ItemType> *copy = head, *save, *prev;
    int i = 1;

    if (id == 1)
    { // if first node is to be deleted
        if (head)
        {
            save = head;
            head = head->next;
            save->next = NULL;
            delete save;
            if (!head)
                tail = NULL;
            numNodes--;
        }
    }
    else if (id == numNodes)
    { // if last node is to be deleted
        while (copy && i <= numNodes - 2)
        {
            copy = copy->next;
            i++;
        }
        save = copy->next;
        copy->next = NULL;
        delete save;
        numNodes--;
        return;
    }
    else
    {
        while (copy)
        { // otherwise

            if (i == id)
            {
                save = copy;
                prev->next = copy->next;
                save->next = NULL;
                delete save;
                numNodes--;
                break;
            }
            prev = copy;
            copy = copy->next;
            i++;
        }
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Zoo.cpp

#include <bits/stdc++.h>
#include "LinkedList.h"
#include <fstream>
using namespace std;

void enterMagicalCreature(LinkedList<Creature> *lis)
{
    char ch = 'n';
    do
    {
        string name, description;
        char isDangerous;
        float cost;

        cout << "Enter name - ";

        cin.clear();
        cin.sync();
        getline(cin, name);

        cout << "Enter description - ";

        cin.clear();
        cin.sync();
        getline(cin, description);

        cout << "Is it dangerous? (y/n) ";
        cin >> isDangerous;

        cout << "Enter its maintenance cost - ";
        cin >> cost;

        Creature creature = Creature(name, description, isDangerous == 'y', cost);
        ListNode<Creature> *newPtr = new ListNode<Creature>(creature);
        lis->appendNode(newPtr);

        cout << name << " has been added to the Zoo\n\nWanna add more? (y/n) ";
        cin >> ch;

    } while (ch == 'y' || ch == 'Y');
}

void enterMagicalCreatureFromFile(LinkedList<Creature> *lis, string fileName)
{

    ifstream fin;
    fin.open(fileName);

    if (fin.fail())
    {
        cout << "No file exists! Try adding some creatures to the ZOO first & save it to the file\n\n";
        return;
    }

    string record, entry = "", des = "";
    vector<string> res;

    if (lis->getLength() > 0)
    { // Delete the current list
        while (lis->getLength() > 0)
        {
            lis->deleteNode(1);
        }
    }

    int i = 0;
    while (1)
    { // Reading the file line by line
        i++;
        getline(fin, record);

        if (fin.eof())
            break;

        for (int i = 0; i < record.size(); ++i)
        {
            while (i < record.size() && record[i] == ' ')
                i++;

            if (res.size() == 1)
            {
                while (i < record.size() && !isdigit(record[i]))
                {
                    entry += record[i];
                    i++;
                }
                i--;
                entry.pop_back();
            }
            else
            {
                while (i < record.size() && record[i] != ' ')
                {
                    entry += record[i];
                    i++;
                }
            }

            if (entry != "")
            {
                res.push_back(entry); // pushing the creature's attributes into the vector
                entry = "";
            }
        }

        Creature creature = Creature(res[0], res[1], stoi(res[2]), stof(res[3])); // creating a creature object using recently formed vector
        ListNode<Creature> *newPtr = new ListNode<Creature>(creature);            // creating a listnode of the object
        lis->appendNode(newPtr);                                                  // appending the listnode to the list
        res.clear();
    }
    if (i)
        cout << i - 1 << " creature(s) added from file\n\n";
    else
        cout << "File is empty\n\n";

    fin.close();
}

void deleteCreature(LinkedList<Creature> *lis, int pos)
{
    if (pos < 1 || pos > lis->getLength())
        return;
    lis->deleteNode(pos);
    cout << "Deleted successfully\n";
}

void saveCreaturesToFile(LinkedList<Creature> *lis, string fileName)
{

    int i = 1;
    ofstream f;
    f.open(fileName, ios::out); // this will create a new file or erase the contents of the existing file
    f.close();

    if (lis->getLength() == 0)
    { // if list is empty
        cout << "THERE ARE NO CREATURES AT YOUR ZOO!\n";
        return;
    }

    while (i <= lis->getLength())
    { // if list isn't empty, call the respective function for appending creatures to the file
        printCreatureToFile(lis->getNodeValue(i), fileName);
        i++;
    }
}

void printCreatures(LinkedList<Creature> *lis)
{
    if (lis->getLength() == 0)
    {
        cout << "THERE ARE NO CREATURES AT YOUR ZOO!\n";
        return;
    }

    int i = 1;
    while (i <= lis->getLength())
    {
        printCreature(lis->getNodeValue(i)); // this is the printing function
        i++;
    }
}

int main()
{

    int ch1, pos;
    char ch2;
    string fileName;
    LinkedList<Creature> *lis = new LinkedList<Creature>(); // creating a new linked list

    do
    {
        cout << "MENU:\n\n";
        cout << "1. Enter creature\n2. Delete creature\n3. Print creature\n4. Save creature to file\n\n";
        cout << "Enter your choice - ";
        cin >> ch1;
        cout << "\n";

        switch (ch1)
        {
        case 1:
            cout << "a) Enter manually\nb) Enter from file\n\n";
            cout << "Enter your choice - ";
            cin >> ch2;

            cout << "\n";
            if (ch2 == 'a')
            {
                enterMagicalCreature(lis);
            }
            else if (ch2 == 'b')
            {
                cout << "Enter File Name - ";
                cin >> fileName;
                enterMagicalCreatureFromFile(lis, fileName);
            }
            break;

        case 2:
            printCreatures(lis);
            cout << "Enter the position of the node you wanna delete - ";
            cin >> pos;
            cout << "\n";

            deleteCreature(lis, pos);
            break;

        case 3:
            printCreatures(lis);
            break;

        case 4:
            cout << "Enter File Name - ";
            cin >> fileName;
            saveCreaturesToFile(lis, fileName);
            cout << "GOOD BYE!\n\n";
            break;

        default:
            break;
        }
    } while (ch1 != 4);
    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Creature.h

#include <string>
class Creature
{
private:
    std::string name, description;
    float cost;
    bool isDangerous;

public:
    Creature()
    { // Default Constructor
    }

    Creature(std::string name, std::string description, bool isDangerous, float cost)
    { // Parameterised Constructor
        this->name = name;
        this->description = description;
        this->isDangerous = isDangerous;
        this->cost = cost;
    }

    std::string getName();
    std::string getDescription();
    bool getDangerous();
    float getCost();

    void setName(std::string name);
    void setDescription(std::string description);
    void setDangerous(bool isDangerous);
    void setCost(float cost);
};

std::string Creature::getName()
{
    return name;
}

std::string Creature::getDescription()
{
    return description;
}

bool Creature::getDangerous()
{
    return isDangerous;
}

float Creature::getCost()
{
    return cost;
}

void Creature::setName(std::string name)
{
    this->name = name;
}

void Creature::setDescription(std::string description)
{
    this->description = description;
}

void Creature::setDangerous(bool isDangerous)
{
    this->isDangerous = isDangerous;
}

void Creature::setCost(float cost)
{
    this->cost = cost;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Creature.cpp

#include <iostream>
#include <fstream>
#include "Creature.h"
using namespace std;

void printCreature(Creature creature)
{ // Will print the creature details to the console
    cout << "Creature details:\n";
    cout << "Name - " << creature.getName() << "\n";
    cout << "Description - " << creature.getDescription() << "\n";
    cout << "Is Dangerous - " << (creature.getDangerous() ? "Yes" : "No") << "\n";
    cout << "Cost - " << creature.getCost() << "\n\n";
}

void printCreatureToFile(Creature creature, string fileName)
{ // will print the creature details to the given file
    ofstream f;
    f.open(fileName, ios::app);
    f << creature.getName() << " " << creature.getDescription() << " " << creature.getDangerous() << " " << creature.getCost() << "\n";
    f.close();
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

creatureFile.txt

kdjh askdj 1 14.3
DHGFBAW lildwh 0 16451
sdkj zskdj 1 14
dshzg sbhj dg 0 164
dfihgha sdhf 1 6456

Related Solutions

C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your...
IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your name while running your program as you did with hello2.c (using argc and argv) name has to be 2 words max (e.g. Jose Lopez, Maria Rodrigues) . Make sure to check properly. Print your name 10 times using a loop. 10 is a fixed number. Count how many characters are in your first name and last name. If the number of characters of your...
ASSEMBLY LANGUAGE ONLY OR I WILL DOWNVOTE write a program that will allow a person to...
ASSEMBLY LANGUAGE ONLY OR I WILL DOWNVOTE write a program that will allow a person to play a Guess a C♠rd Game. This section specifies the required functionality of the program. The interface must be a BlueJ Terminal Window, otherwise zero marks will be awarded. Only a simple screen presentation of the game is required; however, more marks will be gained for a game that is easy to follow with clear informative messages to the player. The aim of the...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
Create design document for a program that will allow the user: Convert a number to binary....
Create design document for a program that will allow the user: Convert a number to binary. Convert from binary to number. Display the hexadecimal representation of a binary number. Given an hexadecimal display its binary representation. Convert a number to IEEE single precision and vice versa. More to come. PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING USE FUNCTIONS IF POSSIBLE
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT