
Patrick B. answered 07/20/21
Math and computer tutor/teacher
//book.h
#ifndef _BOOK
#define _BOOK
typedef
class Book
{
private:
char * title;
char * author;
int year;
public:
Book();
Book(char * bookTitle, char * bookAuthor, int yr);
Book(const Book &);
~Book();
int GetBookTitleLength();
int GetBookAuthorLength();
void GetBookTitle(char * bookTitleBuff);
void GetBookAuthor(char * bookAuthorBuff);
int GetBookYear();
Book(char * bookBuffCSV);
void SerializeToCSV(char * bookBuffCSV);
protected:
void SetBookTitle(char * bookTitleBuff);
void SetBookAuthor(char * bookAuthorBuff);
} * TBook;
#define BOOK_SIZE (sizeof(Book))
//book.cpp
#ifndef _BOOK
#define _BOOK
typedef
class Book
{
private:
char * title;
char * author;
int year;
public:
Book();
Book(char * bookTitle, char * bookAuthor, int yr);
Book(const Book &);
~Book();
int GetBookTitleLength();
int GetBookAuthorLength();
void GetBookTitle(char * bookTitleBuff);
void GetBookAuthor(char * bookAuthorBuff);
int GetBookYear();
Book(char * bookBuffCSV);
void SerializeToCSV(char * bookBuffCSV);
protected:
void SetBookTitle(char * bookTitleBuff);
void SetBookAuthor(char * bookAuthorBuff);
} * TBook;
#define BOOK_SIZE (sizeof(Book))
//bookdb.h
#ifndef _BOOK_DB
#define _BOOK_DB
#define MAX_BOOKS (1000)
#ifndef _BOOK
#include "book.h"
#endif
typedef
class BookDB
{
private:
Book books[MAX_BOOKS];
int count;
public:
BookDB() { count=0; }
int GetCount() { return(count); }
void RemoveDelete(int iIndexPos);
void InsertAddNew(TBook book);
int LinearSearchFind(TBook book);
int IndexerGetAtIndex(int iIndexPos, TBook book);
int Read(char * filename);
int Write(char* filename);
} * TBookDB;
#define BOOK_DB_SIZE (sizeof(BookDB))
#endif
//bookdb.cpp
#ifndef _BOOK_DB
#include "BookDB.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void BookDB::RemoveDelete(int iIndexPos)
{
if (iIndexPos>=0 && iIndexPos<count)
{
TBook bookPtr = &books[count-1]; //points at last book record
memcpy(&books[iIndexPos],bookPtr,BOOK_SIZE); //overwrites target book record with last book record
memset(bookPtr,0,BOOK_SIZE); //invalidates last book record
count--; //updates counter
Write((char*)"E:\\books.dat");
}
}
void BookDB::InsertAddNew(TBook book)
{
books[count++]= Book(*book);
Write((char*)"E:\\books.dat");
}
int BookDB::LinearSearchFind(TBook book)
{
}
int BookDB::IndexerGetAtIndex(int iIndexPos, TBook book)
{
int iReturn=0;
if (iIndexPos>=0 && iIndexPos<count)
{
memcpy(book,&books[iIndexPos],BOOK_SIZE);
}
else
{
iReturn=-1;
}
return(iReturn);
}
int BookDB::Read(char * filename)
{
int iReturn=0;
char inbuff[1024];
char outbuff[1024];
FILE *fptr = fopen(filename,"r");
if (fptr!=NULL)
{
fscanf(fptr,"%d",&count);
fgets(inbuff,1024,fptr);
for (int iLoop=0; iLoop<count; iLoop++)
{
fgets(inbuff,1024,fptr);
// printf(" read :>%s< \n",inbuff);
books[iLoop] = Book(inbuff);
/*books[iLoop].SerializeToCSV(outbuff);
printf("%s\n",outbuff);*/
}
fclose(fptr);
}
else
{
iReturn = -1;
}
return(iReturn);
}
int BookDB::Write(char* filename)
{
FILE * fptr = fopen(filename,"w");
char outbuff[1024];
fprintf(fptr,"%d \n",count);
for (int iLoop=0; iLoop<count; iLoop++)
{
books[iLoop].SerializeToCSV(outbuff);
fprintf(fptr,"%s\n",outbuff);
}
fclose(fptr);
}
using namespace std;
#include <iostream>
#include <string.h>
#ifndef _BOOK_DB
#include "BookDB.h"
#endif
int Menu()
{
int x;
cout << "*********************************" << endl;
cout << "<1> Add book to collection " << endl;
cout << "<2> remove book from collection " << endl;
cout << "<3> display collection " << endl;
cout << "**********************************" << endl;
cout << "Input selection or ZER0 to quit :> ";
cin >> x;
return(x);
}
Go()
{
BookDB bookDB;
bookDB.Read((char*)"E:\\books.dat");
int x=-1;
while (x!=0)
{
x = Menu();
switch(x)
{
case 1:
{
char title[1024];
char author[1024];
int year;
cout << "title ???:>"; cin.ignore();
cin.getline(title,1024,'\n');
cout << "author : ()lastName:firstName) ??? :>";
cin.getline(author,1024,'\n');
cout << "year :>";
cin >> year;
Book newBook(title,author,year);
bookDB.InsertAddNew(&newBook);
break;
}
case 2:
{
int n;
cout <<" book # :>";
cin >> n;
bookDB.RemoveDelete(n);
break;
}
case 3:
{
int N = bookDB.GetCount();
cout << "Title Author Year" << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
Book curBook;
char outbuff[1024];
bookDB.IndexerGetAtIndex(iLoop,&curBook);
curBook.GetBookTitle(outbuff);
cout << outbuff <<" ";
curBook.GetBookAuthor(outbuff);
cout << outbuff << " ";
cout << curBook.GetBookYear() << endl;
}
break;
}
}
} //while
}
int main(int argc, char** argv)
{
Go();
return 0;
}