In: Computer Science
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.
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;
}