Thread: Book inventory program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Book inventory program

    Which STL container should I use for a book inventory program?
    I was thinking a vector. I plan to implement a query class and menu class. The query class will sort the list based on the given criteria for instance author name. I also want to allow the user to input some bool functions for instance if price is the search criteria the user could enter: >$10.00 and it would list books whose value is greater than $10.00.
    I have a book class
    Code:
    #ifndef BOOK_H
    #define BOOK_H
    
    #include <string>
    
    class Book{
        friend ostream &operator<<(ostream&,const Book &);
        
        public:
                Book();
                // constructor title,author,publisher,copy right,isbn,price
                // used for string literals in constructor
                Book(string,string="",string="",int=0,int=0,double=0.0);
                          
                string get_title() const {return title;);
                string get_author() const {return author;};
                string get_publisher() const {return publisher;};
                int get_ISBN() const {return isbn;};
                int get_datecopyright() const {return datecopyright;};
                double get_price() const {return price;};
                
                
                void set_title(string);
                void set_author(string);
                void set_publisher(string);
                void set_ISBN(int);
                void set_datecopyright(int);
                void set_price(double);
                                                          
        private:
                 string title;
                 string author;
                 // parsed from authors full name
                 string authorfirstname;
                 string authorlastname;
                              
                 string publisher;
                 int datecopyright;
                 int isbn;
                 int barcode;
                 double price;
                 
                 //split authors fullname into first, last
                 void parse_name(string,string &,string &);
                              
    };
    #endif
    I want to allow for multiple authors any suggestions? This is a complicated project for me so I will be asking many questions as I go.
    Last edited by curlious; 09-26-2003 at 07:59 PM.

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    A vector is ok if you want to use it like an array. I'd use either list or map myself. The semi-colons after the inline functions is not needed. Also I'd use string for ISBN barcode and datecopyrighted
    Last edited by velius; 09-26-2003 at 10:58 AM.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks. How should I implement multiple authors. Should I use a dynamic string array of authors? If I do this I will have to create a copy constructor and overload the ='s operator as well as create a destructor correct. Can anyone suggest how to do this?


    I have been giving the container some thought. Basicly I will have a list of books. A map with a key value pair would work if I used the title as the key and the whole book object as the value. Perhaps then I could create query "sets" based on the map. Instead of resorting the list of books based on the query I was thinking of creating new lists or sets or even maps from the query results. What do you think. lol I think I'm jumping ahead.
    Last edited by curlious; 09-26-2003 at 11:36 AM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    how about a master container using a map< string, Book> where the title of the book is the string and (that and) all the other information on that book is contained in Book. The map will be sorting by the title by default since the < operator is overloaded for the STL string class. Then you can create different views of that data by sorting on different fields of each Book. Each view can be based on a different type of container, say list or vector or whichever you feel more comfortable with, and then dealt with as you wish. Sounds pretty much like the second half of your post.

    Overloading the necessary operators usually isn't that difficult. There are a number of posts on the topic on this board and several reasonable tutorials on the web that cover this topic if you're not familiar with it.

    A dynamic container to hold the names of multiple authors is an acceptable technique. You can either create your own or use one from the Standard Template Library.
    Last edited by elad; 09-26-2003 at 12:27 PM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    doh, I have decided to use a map for multiple authors. I want to keep all the containers for the querys the same to make it easier to save to files.

    what will be interesting is parsing the query string and calling the appropriate overloaded function.

    I want to keep the interface (menu), query class and such seperate so that I can later update the menu to cgi forms.

    It seems the menu and the query class must communicate. Normally I would make all the query functions member functions of the Menu class but I think I'll create a query string and option and pass those to the query class. The menu will do nothing more than accept input and display an appropriate menu. Does this sound like a good technique.

    Also should I want to save the individual querys to disk so my query class will handle file i/o is this too much for one class.

    Thanks for all the input. Things are being planned as I go so I hope I am not boring you. Here is an update of my book class:
    Code:
    #ifndef BOOK_H
    #define BOOK_H
    
    #include <iostream>
    #include <string>
    #include <vector>
    
    class Book{
        friend ostream &operator<<(ostream&,const Book &);
        
        public:
                Book();
                // constructor title,author,publisher,copy right,isbn,price
                // used for string literals in constructor
                Book(string,string="",string="",string="",string="",double=0.0);
                Book(string,std::map<string,string,string>,string="",string="",double=0.0);          
                
                string get_title() const {return title}
                string get_authors() const {return &authors;}
                string get_publisher() const {return publisher;}
                string get_ISBN() const {return isbn;}
                string get_datecopyright() const {return datecopyright;}
                double get_price() const {return price;};
                
                
                void set_title(string);
                void set_authors(string);
                void set_authors(std::vector<string>);
                void set_publisher(string);
                void set_ISBN(int);
                void set_datecopyright(int);
                void set_price(double);
                                                          
        private:
                 string title;
                 // last,first,middle name
                 string std::map<string,string,string> authors;
                 
                                     
                 string publisher;
                 string datecopyright;
                 string isbn;
                 int barcode;
                 double price;
                 
                 //split authors fullname into first, last
                 void parse_name(string);
                              
    };
    #endif
    Last edited by curlious; 09-26-2003 at 02:50 PM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Classes can be as complicated as you like. Generally all the information should relate together somehow, but if you can do that, then go for it.

    to use the map the syntax would be

    map<key, value> mapObjectName;

    where key is the name of the class you are using for the key and value is the name of the class you are using for the value associated witht the key. you can't do this:

    map<string, string, string> mapObjectName;

    I wuld suggest a different container to hold authors. I still think a vector or list of authors sounds the most reasonable. If you want to sort by the authors last name, then you could create an Author class that has three strings, one each for the first, last, and middle names of a given author. Then you could have a list or vector of Authors in the book class and sort by any one of those three fields.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I updated my book class and created a author class. Here is the code
    Code:
    #ifndef BOOK_H
    #define BOOK_H
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "author.h"
    
    class Book{
        friend ostream &operator<<(ostream&,const Book &);
        friend istream & operator (istream & ,Book &);
        
        public:
                Book();
                // constructor title,author,publisher,copy right,isbn,price
                // used for string literals in constructor
                Book(string,string="",string="",string="",string="",double=0.0);
                
                string get_title() const {return title}
                string get_authors() const {return &authors;}
                string get_publisher() const {return publisher;}
                string get_ISBN() const {return isbn;}
                string get_datecopyright() const {return datecopyright;}
                double get_price() const {return price;};
                
                void edit_book(Book &);
                
                void set_title(string);
                void set_authors(string);
                void set_publisher(string);
                void set_ISBN(int);
                void set_datecopyright(int);
                void set_price(double);
                                                          
        private:
                 string title;
                 // last,first,middle name
                 string std::vector<Author> authors;
                                                 
                 string publisher;
                 string datecopyright;
                 string isbn;
                 int barcode;
                 double price;
                 
    };
    #endif
    And the author class
    Code:
    #include <iostream>
    #include <string>
    
    class Author{
        public:
            Author();
            //constructor that accepts full name as input
            Author(string);
            string get_lastname() const;
            string get_firstname() const;
            string get_middlename() const;
            
            void parse_name(string);
            void set_lastname();
            void set_firstname();
            void set_middlename();
    
        private:
            string last_name;
            string first_name;
            string middle_name;
    };
    #endif
    I have a question about overloading the istream operator>>
    to input a book should I access the private members directly ie:
    Code:
    istream &operator>>(istream & input, Book & tBook){
          input >> tbook.title;
          .
          . 
    }
    or should I uset the set functions
    Code:
    istream &operator>>(istream & input,Book & tBook){
          string ttitle,tauthors...;
          input >> ttitle;
          tBook.set_title(ttitle);
          .
          .
    }
    Last edited by curlious; 09-26-2003 at 07:48 PM.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    your most recent post has code for the Book.h twice but no Authur.h, not it matters that much, but just so you know.

    Code:
    in the following line drop the word string.
    
    string std::vector<Author> authors;
    
    you'll need to rework the following code, too
    
    string get_authors() const {return &authors;}
    because return the address of authors is not the same thing as returning a string. You will want to think about how you are trying to do here as there are several approachs.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I have taken a step back before implementing authors.
    What I am going to do is input a string containing the author(s)
    first middle last, first middle last and parse the string seperating it into a vector<string> authors.

    but looking at my code I need to slow down and am having some very basic problems with my book class here is my revised code before implementing author(s).
    Code:
    #ifndef BOOK_H
    #define BOOK_H
    
    #include <iostream>
    using namespace std;
    #include <string>
    #include <vector>
    
    
    class Book{
        friend ostream &operator<<(ostream &,const Book &);
        friend istream &operator>>(istream &,const Book &);
        
        public:
                Book();
                // constructor title,author,publisher,copy right,isbn,price
                // used for string literals in constructor
                Book(string,string="",string="",string="",string="",double=0.0);
                
                string get_title() const {return title;}
                string get_publisher() const {return publisher;}
                string get_ISBN() const {return ISBN;}
                string get_datecopyright() const {return datecopyright;}
                double get_price() const {return price;};
                
                void edit_book(Book &);
                
                void set_title(string);
                void set_authors(string);
                void set_publisher(string);
                void set_ISBN(string);
                void set_datecopyright(string);
                void set_price(double);
                                                          
        private:
                 string title;
                 // last,first,middle name
                 
                 string publisher;
                 string datecopyright;
                 string ISBN;
                 int barcode;
                 double price;
                 
    };
    #endif
    i am getting ambigous errors with my istream operator can anyone help
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "book.h"
    
    Book::Book(){
        set_title("");
        set_authors("");
        set_publisher("");
        set_ISBN("");
        set_datecopyright("");
        set_price(0.0);
    }
    
    // constructor title,author,publisher,ISBN,copy right,price
    Book::Book(string ttitle,string tauthors,string tpublisher,string tISBN,string tcopyright,double tprice){
        set_title(ttitle);
        set_authors(tauthors);
        set_publisher(tpublisher);
        set_ISBN(tISBN);
        set_datecopyright(tcopyright);
        set_price(tprice);
    }
    
    void Book::set_title(string ttitle){
        title=ttitle;
    }
    
    void Book::set_authors(string tauthors){
    }
    
    void Book::set_publisher(string tpublisher){
        publisher=tpublisher;
    }
    
    void Book::set_ISBN(string tISBN){
        ISBN=tISBN;
    }
    
    void Book::set_datecopyright(string tcopyright){
        datecopyright=tcopyright;
    }
    
    void Book::set_price(double tprice){
        price=tprice;
    }
    
    void edit_book(Book & tBook){
        cout << tBook;
        cin >> tBook;
        cout << "Revised Input:\n";
        cout << tBook;
    }
    
        
    
    istream & operator>>(istream &input,Book &tBook){
        string ttitle,tauthors,tpublisher,tISBN,tcopyright;
        double tprice;
        cout << "Enter Title (required):";
        input >> ttitle;
        tBook.set_title(ttitle);
        cout << "Enter Author(s)(in the form first middle(optional) last,next author):";           
        input >> tauthors;
        tBook.set_authors(tauthors);
        cout << "Enter Publisher:";
        input >> tpublisher;
        tBook.set_publisher(tpublisher);
        cout << "Enter ISBN:";
        input >> tISBN;
        tBook.set_ISBN(tISBN);
        cout << "Enter copy right year:";
        input >> tcopyright;
        tBook.set_datecopyright(tcopyright);
        cout << "Enter price of book:";
        input >> tprice;
        tBook.set_price(tprice);
        return input;
    }   
    
    ostream &operator<<(ostream & output,Book & tBook){
        output << "Title:"<<tBook.get_title()<<endl;
        output << "Publsher:"<<tBook.get_publisher()<<endl;
        output << "ISBN:"<<tBook.get_ISBN()<<endl;
        output << "Copy Right Date:"<<tBook.get_datecopyright()<<endl;
        output << "Price:"<<tBook.get_price()<<endl;
        return output;
    }
    here is a simple test main
    Code:
    include <iostream>
    #include <stdlib.h>
    #include "book.h"
    
    
    using std::cin;
    
    
    int main(int argc, char *argv[])
    {
      Book Entry();
      cin >> Entry;
      Entry.edit_book(Entry);
      //display main menu
      //get user input display appropriat menu
      //if query build queryset
       
      system("PAUSE");	
      return 0;
    }
    Ooops I edited the post to include the author class instead of the book class again sorry.

    BTW thanks for the help ignore this post as I started from scratch and solved the problem with the istream/ostream operator though I don't know what I did wrong originally. I'll post further questions in a new thread.
    Last edited by curlious; 09-27-2003 at 09:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Inventory program
    By Kayoss in forum C++ Programming
    Replies: 14
    Last Post: 12-17-2005, 04:24 AM
  3. parsing author name for book inventory prog
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2003, 12:54 PM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM