tags:

views:

177

answers:

1

I have a media program where from main() i am adding info about book, cd, dvd. every entry for cd,dvd,book has a keyword. Where i am having a problem is adding a keyword to each item. currently i have everything added but keyword.

required out put

    -Book-
    author:    Mark Haddon
    pages:     240
    title:     The Curious Incident of the Dog in the Night-Time
    keywords:  Asperger's Syndrome, autism

    -MusicCD-
    band:      Grateful Dead
    musicians: "Bill Kreutzman" "Jerry Garcia" "Keith Godcheaux"
    songs:     12
    title:     Europe In '72
    keywords:  acid rock, jam bands, sixties

I need help with the addKeywordForItem() function

Main()

cout << ">>> adding items to library:\n\n";
item = library->addBook("The Curious Incident of the Dog in the Night-Time", "Mark Haddon", 240);
if (item != NULL) {
    library->addKeywordForItem(item, "autism");
    library->addKeywordForItem(item, "Asperger's Syndrome");
        library->printItem(cout, item);
    }

here is the library function()

#include "Library.h"
#include "book.h"
#include "cd.h"
#include "dvd.h"

#include <iostream>
// general functions


ItemSet allBooks;
ItemSet allCDS;
ItemSet allDVDs;


ItemSetMap allBooksByAuthor;

ItemSetMap allmoviesByDirector;
ItemSetMap allmoviesByActor;

ItemSetMap allMusicByBand;
ItemSetMap allMusicByMusician;


void Library::addKeywordForItem(const Item* item, const string& keyword)
{

     ItemSet* obj = new ItemSet();


     //key.insert(keyword);


}

const ItemSet* Library::itemsForKeyword(const string& keyword) const
{
    ItemSet* obj = new ItemSet();


    return NULL;

}

void Library::printItem(ostream& out, const Item* const item) const
{

    out << item;

}


const Item* Library::addBook(const string& title, const string& author, const int nPages)
{

    ItemSet* obj = new ItemSet();

    Book* item = new Book(title,author,nPages);
    allBooks.insert(item); // add to set of all books
    obj->insert(item);
    allBooksByAuthor[author] = obj;

     return item;
}

const ItemSet* Library::booksByAuthor(const string& author) const
{
    return allBooksByAuthor[author];

}

 const Item* Library::addMusicCD(const string& title, const string& band, const int nSongs)
{

ItemSet* obj = new ItemSet();
CD* item = new CD(title,band,nSongs);

allCDS.insert(item);
obj->insert(item);
allMusicByBand[band] = obj;
 return item;

}

here is library.h

#pragma once

#include <ostream>
#include <map>
#include <set>
#include <string>
#include "Item.h"

using namespace std;

typedef set<Item*>              ItemSet;
typedef map<string,Item*>       ItemMap;
typedef map<string,ItemSet*>    ItemSetMap;

class Library
{

public:
    // general functions

    void addKeywordForItem(const Item* const item, const string& keyword);
    const ItemSet* itemsForKeyword(const string& keyword) const;
    void printItem(ostream& out, const Item* const item) const;

    // book-related functions

    const Item* addBook(const string& title, const string& author, int const nPages);
    const ItemSet* booksByAuthor(const string& author) const;
    const ItemSet* books() const;

    // music-related functions

    const Item* addMusicCD(const string& title, const string& band, const int nSongs);
    void addBandMember(const Item* const musicCD, const string& member);
    const ItemSet* musicByBand(const string& band) const;
    const ItemSet* musicByMusician(const string& musician) const;
    const ItemSet* musicCDs() const;

    // movie-related functions

    const Item* addMovieDVD(const string& title, const string& director, const int nScenes);
    void addCastMember(const Item* const movie, const string& member);
    const ItemSet* moviesByDirector(const string& director) const;
    const ItemSet* moviesByActor(const string& actor) const;
    const ItemSet* movies() const;
};

here is item.h where i am using a set to store the Keywords - which i do not want to change..

#pragma once

#include <ostream>
#include <set>
#include <string>

using namespace std;

typedef set<string> StringSet;

class Item
{
protected:

    Item(const string& title);

public:

    void addKeyword(const string& keyword);
    const string getTitle() const;
    const StringSet* getKeywords() const;
    virtual void print(ostream& out) const;
    string printKeywords(const StringSet* keywords) const;
    ~Item();

private:

    string Title;
    StringSet* Keys;


};

ostream& operator<<(ostream& out, const Item* const item);

here is item.cpp

#include "Item.h"

Item::Item(const string& title): Title(title),Keys(new StringSet)
{


}

Item::~Item()
{

delete []Keys;

}

void Item::addKeyword(const string& keyword) 
{

Keys->insert(keyword);


}

const string Item::getTitle() const
{
    return Title;
}

const StringSet* Item::getKeywords() const
{
    return Keys;
}


string Item::printKeywords(const StringSet* keywords) const
{

 return NULL;


}


ostream& operator<<(ostream& out, const Item*  item)
{
    item->print(out);
    return out;
}



void Item::print(ostream &out) const
{


    out << "title: " << this->getTitle() << endl;
    out << endl;

}

Can you offer up some help where i can add the keys to the items? the set i am using for the keywords is in item.h and i need help with addKeywordForItem() function.

Thank you for any help you can give me.. in Item.h am i declaring keyword right in the private section? are my functions for keywords in item.cpp right?

Keep in mind i cannot change the CONST in the function!!

A: 

Looks like what you need it

void Library::addKeywordForItem(const Item* item, const string& keyword)
{
    Item->addKeyword(keyword);
}

but you'll have to either (most sensibly, it seems to me) give up on the idea that you can possibly add a keyword to a constant Item [[so change addKeywordForItem's signature by dropping the first const), or (dubiously) declare that the addKeyword method of Item is a const one.

Alex Martelli