creating a "Library" class.

Hi, I am creating a library class, but i am a little confused on how to do these things:

-shows all library cards, and all library books, from an output file.
The library should initialize itself from 2 files (1 for library card data,
1 for book data) on startup, and save these config files on shutdown.

how would i go about doing this? create an object of library and assign it to ifstream file??? and what about the saving part?


-able to check in a book, check out a book.

For loop here, removing books from the array of books i've created???

-and able to add new books and and new cards.

dynamically allocate 'new' books and cards???

Any help/suggestions, would be greatly appreciated, Thank you.

Three classes i've made, Card, Book and Library.
below is the class Library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include "Card.h"
#include "Book.h"
using namespace std;

#ifndef LIBRARY_H
#define LIBRARY_H

class Library
{
private:
	int numCards;
	int numBooks;

	Card numCardss[500];
	Book numBookss[500];

	//Book book;
	//Card card;
public:
	Library();
	Library(int numCards, int numBooks);

	void addBook()
	{
	     //allocate new books here???
	}
	void addCard()
	{
            //allocate new cards here???
	}

	void showMenu()
	{
		cout << "Press 1 to view library cards: " << endl;
		cout << "Press 2 to view current books: " << endl;
		cout << "Press 3 to checkin a book: " << endl;
		cout << "Press 4 to checkout a book: " << endl;
		//cout << "Press 0 to exit the program. " << endl;
	}

	int doCommand(int command)
	{
		switch(command)
		{
		case 1:
			cout << numCards << endl;
			break;
		case 2:
			cout << numBooks << endl;
			break;
		case 3:
			cout << addBook << endl;
		case 4:
			cout << addCard << endl;
		default:
			cout << "Invalid input, please try again." << endl;
		}

	}

};
#endif 


and main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

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


void main()
{
	ifstream infile;					//input file
	ofstream outfile;					//output file
	infile.open("cards.txt");
	infile.open("books.txt");

	/*if (!infile)						//input file check
	{
		cerr << "One or more File(s) could not be opened!" << endl;
		exit(1);
	}*/

	Library lib;
	int command;

	bool do_exit = false;
	do
	{
		lib.showMenu();
		cin >> command;
		do_exit = lib.doCommand( command );
	} 
	while ( do_exit == false );

	infile.close();
	outfile.close();

	//system("pause");
}









You don't know how to finish your homework :D

1) You need to create Card and Book classes.
2) If I understood good you need to open and close cards.txt and books.txt files in their respective classes.
try not to use void main, and to be honest, i do not like void functions in general.

more information on what is in the book and card library would be nice. making an array of 500 is a good amount of memory allocation (not in a single instance, but if there are 1000 books, that is 1000*500 variables), try vectors or recreating the arrays with pointers to the needed length, both would be more memory efficient and allow more memory to be allocated if the need presents itself.
That is true, just need to put things together now, a little stuck.

I have created Card and Book classes, but didn't post them, didn't think it was necessary.

Right now i am working on reading/opening and saving the files in the Main and not in their respective classes?

and what about creating new books and cards?
Here are Book and Card classes:

CARD:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include "Card.h"
#include <iostream>
using namespace std;

Card::Card()
{
	name = " ";
	phonenum = 0;
	cardnum = 0;
	booksout = 0;
}
Card::Card(string Name, int Phonenum, int Cardnum, int Booksout)
{
	name = Name;
	phonenum = Phonenum;
	cardnum = Cardnum;
	booksout = Booksout;
}
Card::~Card()
{

}

void Card::setbooksout(int Booksout)
{
	booksout = Booksout;
}
int Card::getbooksout()
{
	return booksout;
}


BOOK:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "Book.h"
#include <string>
using namespace std;

Book::Book()
{
	title = " ";
	author = " "; 
	ISBN = 0; 
	status = 0;
	holderID = 0;
}
Book::Book(string Title, string Author, int isbn, int Status, int Holder)
{
	title = Title;
	author = Author; 
	ISBN = isbn; 
	status = Status;
	holderID = Holder;
}

Book::~Book()
{
	
}

void Book::setStatus(int Status)
{
	status = Status;

	/*if(status = -1)
	{
		
	}
	else if (status = 0)
	{

	}
	else if (status = 1)
	{

	}*/
	
}

void Book::setholder(int Holder)
{
	holderID = Holder;
}

int Book::getholder()
{
	return holderID;
}

int Book::getStatus()
{
	return status;
}

int Book::getISBN()
{
	return ISBN;
}

/*void Book::print()
{
	cout << "Title: " << title << endl;
	cout << "Author: " << author << endl;
	cout << "ISBN-10: " << ISBN << endl;
	cout << "Status: " << status << endl;
	cout << "Holder's ID number: " << holderID << endl;

}*/


a little tweaking still needed, i believe.
umm, im still not sure why u do not just include all 3 libraries in the main program and just setup friendships. this site has a decent explanation of friendship/inheritance. otherwise you will need to write functions that create the object such as

(object name) createnewobject(//whatever u want){
(object name) (your object name here); /*i am not sure if this works with your compiler, otherwise you need to add the objects into the private section of your library class and have an array/vector of those which you can expand on. */
Last edited on
I am not allowed to use friend functions, don't know why.

Yes, I am creating objects in my main, but only an object of class Library which has components of class card and book.

Right now I am figuring out to read from file and assigning books: title, author, ISBN, etc to its respecting place in the Book class. same thing with Card.

then writing to text files when ever I add a new book or a new card.

any suggestions would reaally help. :)
Other than me playing around with the actual code. Ill tell you to stream information directly into your file. If you do not need to use the information at that time. Other you need to plug everything into an array and then at the end of the program save the info.
Thank you for your reply.

"Stream information directly into your file", that would be the writing back to the file after reading from a file; to add new books/cards, correct?

right now, programs reading from one of the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Library::showCardss()
{
	ifstream inCard ("cards.txt", ios::binary); 					//input file
	char name[80];
	int phonenum, cardnum;
	int booksout;
	inCard >> name >> phonenum >> cardnum >> booksout;

	card.setname(name);
	card.setphonenum(phonenum);
	card.setcardnum(cardnum);
	card.setbooksout(booksout);
	card.printcards();
	
	inCard.close();
}


I do have an array pointer for Card and Book, I wasn't too sure how to "plug everything into an array and then at the end of the program save the info. "
the streaming method is just when u get information you upload it directly to the file, advantages: simpler. disadvantages: can not access the stored data unless you reopen as a ifstream OR you could just open an (io)fstream file. the array method is when you gt the information you put it into an array and then upload the information using a for loop and a class function which returns the value you want. for example
1
2
3
4
5
   } 
    for ( int i = 0; i < size; i++){
        std::cout << library[i].returnname() << " has " << library[i].returnpage() << " number of pages\n"; 
    }
    

instead of std::cout you would use the fie name. this requires more RAM but allows you to access the data within the program and then upload it into the file using a for loop at the end of your program.
my declaration of library
1
2
const int size = 2; 
    book library[size]; 
Topic archived. No new replies allowed.