Question

In: Computer Science

C++ Question Design and implement a class that stores a list of students. Each student has...

C++ Question

Design and implement a class that stores a list of students. Each student has the list of courses he/she registered for the semester. You will need a container of containers to hold this list of students. Choose the optimal container for this exercise, noting that no duplicates should be permitted. Use STL classes with iterators to navigate the list. Develop a test program, which allows options to add, remove, and list students and their associated course lists. Also include a function that displays the first course name in each list (the function must use iterators) Please use namespace std for this.

Solutions

Expert Solution

ANSWER :

I made 2 classes PersonalGiftList and GiftListCollection which contains a list of PersonalGiftList.

//Add new class name PersonalGiftList

//PersonalGiftList.h

#include <string>

#include <list>

using namespace std;

#ifndef PERSONALGIFTLIST_H

#define PERSONALGIFTLIST_H

class PersonalGiftList

{

private:

string name;

list<string> giftList;

public:

PersonalGiftList();

PersonalGiftList(const string &name);

~PersonalGiftList();

void addGift(const string &giftName);

void removeGift(const string &giftName);

void printGiftList()const;

string getName() const { return name; }

};

#endif

//PersonalGiftList.cpp

#include "PersonalGiftList.h"

#include <iostream>

#include <algorithm>

using namespace std;

//---------------------------------------------------------

PersonalGiftList::PersonalGiftList()

{

name = "noname";

giftList.clear();

}

//---------------------------------------------------------

PersonalGiftList::PersonalGiftList(const string &name)

{

this->name = name;

giftList.clear();

}

//---------------------------------------------------------

PersonalGiftList::~PersonalGiftList()

{

}

//---------------------------------------------------------

void PersonalGiftList::addGift(const string &giftName)

{

giftList.push_back(giftName);

}

//---------------------------------------------------------

void PersonalGiftList::removeGift(const string &giftName)

{

giftList.remove(giftName);

}

//---------------------------------------------------------

void PersonalGiftList::printGiftList()const

{

cout << name << "'s gift list:\n";

list<string>::const_iterator i;

for (i = giftList.begin(); i != giftList.end(); ++i)

{

cout << *i << endl;

}

}

//Add new class name GiftListCollection

//GiftListCollection.h

#include <list>

#include "PersonalGiftList.h"

using namespace std;

#ifndef GIFTLISTCOLLECTION_H

#define GIFTLISTCOLLECTION_H

class GiftListCollection

{

private:

list<PersonalGiftList> collection;

public:

GiftListCollection();

~GiftListCollection();

void addNewList(const string &name);

void addNewList(const PersonalGiftList &rhs);

bool findPerson(const string &name);

PersonalGiftList& operator()(const string &name);

void removePerson(const string& name);

void printCollection()const;

};

#endif

//GiftListCollection.cpp

#include "GiftListCollection.h"

#include <iostream>

#include <algorithm>

#include <string>

using namespace std;

//---------------------------------------------------------

GiftListCollection::GiftListCollection()

{

collection.clear();

}

//---------------------------------------------------------

GiftListCollection::~GiftListCollection()

{

}

//---------------------------------------------------------

void GiftListCollection::addNewList(const string &name)

{

PersonalGiftList temp(name);

collection.push_back(temp);

}

//---------------------------------------------------------

void GiftListCollection::addNewList(const PersonalGiftList &rhs)

{

collection.push_back(rhs);

}

//---------------------------------------------------------

void GiftListCollection::removePerson(const string &name)

{

if (findPerson(name))

{

list<PersonalGiftList>::iterator i;

for (i = collection.begin(); i != collection.end(); ++i)

{

if (i->getName() == name)

break;

}

collection.erase(i);

}

}

//---------------------------------------------------------

bool GiftListCollection::findPerson(const string &name)

{

list<PersonalGiftList>::iterator i;

for (i = collection.begin(); i != collection.end(); ++i)

{

if (i->getName() == name)

return true;

}

return false;

}

//---------------------------------------------------------

PersonalGiftList& GiftListCollection::operator()(const string &name)

{

list<PersonalGiftList>::iterator i;

for (i = collection.begin(); i != collection.end(); ++i)

{

if (i->getName() == name)

return *i;

}

return *i;

}

//---------------------------------------------------------

void GiftListCollection::printCollection()const

{

list<PersonalGiftList>::const_iterator i;

for (i = collection.begin(); i != collection.end(); ++i)

{

cout << i->getName() << endl;

}

}

//main.cpp

#include <iostream>

#include <string>

#include "PersonalGiftList.h"

#include "GiftListCollection.h"

using namespace std;

int main()

{

//LOCAL DECLARATIONS

const int NAME_SIZE = 40;

GiftListCollection list;

int choice = 0;

char input[NAME_SIZE];

string name;

string gift;

//PROCEDURES

cout << "\tMy gift list book\n\n";

do

{

cout << "Please choose what you want to do:\n";

cout << "1 - See all people you have in your list\n";

cout << "2 - See a specific person's gift list\n";

cout << "3 - Add a person to your list\n";

cout << "4 - Add a gift to a person's gift list\n";

cout << "5 - Remove a person from your list\n";

cout << "6 - Remove a gift from a person's gift list\n";

cout << "7 - Exit - close the list book\n";

cin >> choice;

cin.ignore();

cout << endl;

if (choice == 1)

{

cout << "Here they are:\n";

list.printCollection();

cout << "(End of list)\n";

}

else if (choice == 2)

{

cout << "Enter that person's name: ";

cin.getline(input, NAME_SIZE);

name = input;

if (list.findPerson(name))

{

list(name).printGiftList();

cout << "(End of " << name << "'s gift list)\n";

}

else

cout << "Not found " << name << " in the list\n";

}

else if (choice == 3)

{

cout << "Enter that person's name: ";

cin.getline(input, NAME_SIZE);

name = input;

list.addNewList(name);

cout << "Sucessfully added " << name << " to the list\n";

}

else if (choice == 4)

{

cout << "Enter that person's name: ";

cin.getline(input, NAME_SIZE);

name = input;

if (list.findPerson(name))

{

cout << "Enter the gift's name: ";

cin.getline(input, NAME_SIZE);

gift = input;

list(name).addGift(gift);

cout << "Sucessfully added " << gift << " to " << name << "'s gift list\n";

}

else

cout << "Not found " << name << " in the list\n";

}

else if (choice == 5)

{

cout << "Enter that person's name: ";

cin.getline(input, NAME_SIZE);

name = input;

if (list.findPerson(name))

{

list.removePerson(name);

cout << "Successfully remove " << name << " from the list\n";

}

else

cout << "Not found " << name << " in the list\n";

}

else if (choice == 6)

{

cout << "Enter that person's name: ";

cin.getline(input, NAME_SIZE);

name = input;

if (list.findPerson(name))

{

cout << "* You should look at his/her gift list first to make sure to type correctly the gift's name or it won't be removed form the list! *\n";

cout << "Enter the gift's name: ";

cin.getline(input, NAME_SIZE);

gift = input;

list(name).removeGift(gift);

cout << "Successfully remove " << gift << " from " << name << "'s list\n";

}

else

cout << "Not found " << name << " in the list\n";

}

else {

}

cout << endl;

} while (choice != 7);

cin.get();

cout << endl;

return 0;

}


Related Solutions

C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application. a) Draw the UML diagram b) Write...
Create a C structure which stores information about a student. Each student should be represented by...
Create a C structure which stores information about a student. Each student should be represented by a student ID (integer), first name, last name, day, month and year of birth, and program code (string). Write a C program which creates an array of 100 of these structures, then prompts the user to enter data from the keyboard to fill the array. If an ID of 0 is entered, data entry should finish, and the list of students should be printed...
COIN TOSSES In a large class of introductory Statistics students, the professor has each student toss...
COIN TOSSES In a large class of introductory Statistics students, the professor has each student toss a coin 16 times and calculate the proportion of his or her tosses that were heads. The students then report their results, and the professor plots a histogram of these several proportions. What shape would you expect this histogram to be? Why? Where do you expect the histogram to be centred? How much variability would you expect among these proportions? Explain why a Normal...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
Design a complete system to allow manipulation of Students (via a designed and developed Student class)....
Design a complete system to allow manipulation of Students (via a designed and developed Student class). in JAVA It Includes: 1) A Student Class which stores info about the student: name and a generated ID#. 2) User interface (via Scanner) that prompts user for the following: a) New Student -- user enters the name of the student, the system (your student class will generate the ID#) b) View Student via Name - the user enters name (not case sens., and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT