Professional Documents
Culture Documents
PROJECT FILE
LIBRARY MANAGEMENT
Leo Joseph
XII S3
Session: 2018-2019
DAV Public School, Nerul
TABLE OF CONTENTS
Acknowledgement
Objective
Source Code
Output
Bibliography
Acknowledgement
#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
// Rohit -
class Book{
int id;
char name[40];
char author[20];
float rating;
float price;
public:
void getdata();
void display();
void modify();
char * getAuthor() {return author;}
int getID() {return id;}
float getRating() {return rating;}
float getPrice() {return price;}
};
void Book::getdata()
{
cout << "\n\t\t Enter Book Details";
cout << "\n\n\n Enter ID\t\t\t-\t";
cin >> id;
cout << "\n\n Enter name\t\t\t-\t";
gets(name);
cout << "\n\n Enter author's name\t\t-\t";
gets(author);
while(1){
cout << "\n\n Enter rating (Scale 0-10)\t-\t";
cin >> rating;
if(rating >= 0 && rating <= 10)
break;
else{
cout << "\n\n Error. Rating must be between 0
and 10.";
clrscr();
}
}
cout << "\n\n Enter price\t\t\t-\t";
cin >> price;
}
void Book::display()
{
cout << "\n\t\t Book Details"
<< "\n\n ID\t\t-\t\t" << id
<< "\n Name\t\t-\t\t" << name
<< "\n Author \t-\t\t" << author
<< "\n Rating\t\t-\t\t" << rating << " / 10"
<< "\n Price \t\t-\t\tRs." << price;
}
void Book::modify()
{
char str[40];
float num;
display();
getch();
cout << "\n\n\n Enter new name (Enter '.' to retain current
name) -\n ";
gets(str);
if(strcmp(str, ".") != 0)
strcpy(name, str);
cout << "\n\n\n Enter author's name (Enter '.' to retain
current name) -\n ";
gets(str);
if(strcmp(str,".") != 0)
strcpy(author, str);
while(1){
cout << "\n\n\n Enter new rating (Enter '-1' to retain
current rating) -\n ";
cin >> num;
if(num >= 0 && num <= 10 || num == -1)
break;
else{
cout << "\n\n Error. Rating must be between 0
and 10.";
clrscr();
}
}
if(num != -1)
rating = num;
cout << "\n\n\n Enter new price (Enter '-1' to retain current
price) -\n ";
cin >> num;
if(num != -1)
price = num;
}
// Abhinav -
class Genre{
int genreID;
char genreName[20];
Book books[21];
int total;
int authors;
char authorList[20][40];
public:
int getID() {return genreID;}
void addBook();
void removeBook();
void modifyBook();
void displayAll();
void searchByID();
void dispHighestRated();
void dispHighestPrice();
char * listAuthors();
void booksByAuthor();
void removeAuthor(char *);
void setName(char * name){strcpy(genreName, name);}
char * getName(){return genreName;}
Genre(int ID)
{
genreID = ID;
total = 0;
authors = 0;
}
};
void Genre::addBook()
{
if(total == 21)
cout << "\n\n Error. Shelf is full.";
else{
books[total].getdata();
int authorInList = 0;
char * author = books[total].getAuthor();
for(int i = 0; i < authors; i++){
if(strcmp(authorList[i],author) == 0)
authorInList = 1;
}
total++;
if(!authorInList)
strcpy(authorList[authors++],author);
}
}
void Genre::removeBook()
{
char id, flag = 0;
char ch;
cout << "\n\n Enter book ID - ";
cin >> id;
for(int i = 0; i < total; i++){
if(books[i].getID() == id){
flag = 1;
books[i].display();
cout << "\n\n Press 'y' to confirm deletion.\n ";
ch = getch();
if(tolower(ch) == 'y'){
for(int j = i+1; j < total; j++){
removeAuthor(books[j].getAuthor());
books[j-1] = books[j];
}
total--;
}
}
}
if(!flag)
cout << "\n\n Error. No book with ID - " << id;
else{
cout << "\n\n Record deleted.\n ";
}
getch();
}
void Genre::modifyBook()
{
int id, flag = 0;
cout << "\n\n Enter book ID - ";
cin >> id;
for(int i = 0; i < total; i++){
if(books[i].getID() == id){
books[i].modify();
flag = 1;
}
}
if(!flag)
cout << "\n\n Error. No book with ID - " << id;
}
void Genre::displayAll()
{
for(int i = 0; i < total; i++){
books[i].display();
getch();
}
}
void Genre::searchByID()
{
int flag = 0, id;
cout << "\n\n Enter book ID - ";
cin >> id;
for(int i = 0; i < total; i++){
if(books[i].getID() == id){
books[i].display();
flag = 1;
}
}
if(!flag)
cout << "\n\n Error. No book with ID - " << id;
}
void Genre::dispHighestRated()
{
int highID = 0;
float maxRating = books[0].getRating();
for(int i = 1; i < total; i++){
if(books[i].getRating() > maxRating){
highID = i;
maxRating = books[i].getRating();
}
}
cout << "\n\n\t\tHIGHEST RATED BOOK";
books[highID].display();
}
void Genre::dispHighestPrice()
{
int highID = 0;
float maxPrice = books[0].getPrice();
for(int i = 1; i < total; i++){
if(books[i].getPrice() > maxPrice){
highID = i;
maxPrice = books[i].getPrice();
}
}
cout << "\n\n\t\tMOST EXPENSIVE BOOK";
books[highID].display();
}
char * Genre::listAuthors()
{
int num;
cout << "\n\n Select Author - \n\n";
for(int i = 0; i < authors; i++){
cout << "\n " << i+1 << " - " << authorList[i];
}
cout << "\n\n Enter choice : ";
while(1)
{
cin >> num;
if(num > 0 && num < authors+1)
break;
cout << "\n Please enter a valid choice: ";
}
return authorList[num-1];
}
void Genre::booksByAuthor()
{
char name[40];
strcpy(name, listAuthors());
clrscr();
for(int i = 0; i < total; i++){
if(strcmp(books[i].getAuthor(), name)==0){
books[i].display();
getch();
}
}
}
// Leo -
void mainMenu();
void bookMenu();
void genreMenu();
void addGenre();
void removeGenre();
void writeToFile(Genre * g);
int getID();
Genre * getGenre(int);
int filesize(const char * filename);
int main()
{
do{
mainMenu();
}while(1);
return 0;
}
void mainMenu()
{
int ch;
clrscr();
cout << "\n\n\t MAIN MENU"
<< "\n\n\n 1. Genre"
<< "\n\n 2. Book"
<< "\n\n 3. Exit\n\n ";
cin >> ch;
clrscr();
switch(ch){
case 1: genreMenu();
break;
case 2: bookMenu();
break;
case 3: exit(0);
default: cout << "\n\n Error. Invalid choice.";
getch();
}
}
int getID()
{
int id;
Genre * g = new Genre(-1);
int ch;
cout << "\n\n\t\t Genre list\n\n";
ifstream fin("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT",
ios::binary);
if(!fin){
cout << "\n Error. File not found.";
getch();
exit(0);
}
int i = 0;
while(1){
i++;
fin.read((char *) g, sizeof(* g));
if(fin.eof())
break;
cout << g->getID() << " - " << g->getName() << "\n";
}
cout << "\n\n Enter choice : ";
cin >> id;
fin.close();
delete g;
return id;
}
void bookMenu()
{
if(filesize("C:/TURBOC3/BIN/CSPROJ/BOOKS.DAT")
== 0){
cout << "\n\n Error. No books found.";
getch();
return;
}
int id = getID();
Genre * ge = getGenre(id);
if(ge->getID() == -1){
cout << "\n\n Error. Please select an existing genre.";
getch();
return;
}
int ch;
do{
clrscr();
cout << "\n\n\t\t BOOK MENU"
<< "\n\n\n 1. Add book"
<< "\n\n 2. Modify book"
<< "\n\n 3. Remove book"
<< "\n\n 4. Display best rated book"
<< "\n\n 5. Display most expensive"
<< "\n\n 6. List books by a specific author"
<< "\n\n 7. List all books"
<< "\n\n 8. Go back\n\n ";
cin >> ch;
clrscr();
switch(ch){
case 1: ge->addBook();
writeToFile(ge);
break;
case 2: ge->modifyBook();
writeToFile(ge);
break;
case 3: ge->removeBook();
writeToFile(ge);
break;
case 4: ge->dispHighestRated();
break;
case 5: ge->dispHighestPrice();
break;
case 6: ge->booksByAuthor();
break;
case 7: ge->displayAll();
break;
case 8: delete ge;
return;
default: cout << "\n\n Error. Invalid choice.";
}
getch();
}while(1);
}
void genreMenu()
{
int ch;
do{
clrscr();
cout << "\n\n\t GENRE MENU"
<< "\n\n\n 1. Add genre"
<< "\n\n 2. Remove genre"
<< "\n\n 3. Go back\n\n ";
cin >> ch;
clrscr();
switch(ch){
case 1: addGenre();
break;
case 2: removeGenre();
break;
case 3: return;
default: cout << "\n\n Error. Invalid choice";
getch();
}
} while(1);
}
void addGenre()
{
ofstream
fout("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT", ios::binary |
ios::app);
unsigned int a;
do{
cout << "\n\n Enter ID of genre to be created.\n ";
cin >> a;
if(getGenre(a)->getID() != -1){
cout << "\n\n Error. Genre with id " << a << "
already exists.";
getch();
clrscr();
}
else
break;
}while(1);
clrscr();
Genre * g = new Genre(a);
char name[20];
cout << "\n\n Enter genre name - ";
gets(name);
g->setName(name);
fout.write((char *) g, sizeof(* g));
clrscr();
cout << "\n\n Genre created with ID - " << g->getID();
getch();
fout.close();
}
void removeGenre()
{
if(filesize("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT")
== 0){
cout << "\n\n Error. No genres exist.";
getch();
return;
}
int id = getID();
Genre * ge = getGenre(id);
Genre * g = new Genre(-1);
ifstream fin("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT",
ios::binary);
ofstream fout("C:/TURBOC3/BIN/CSPROJ/TEMP.DAT",
ios::binary);
while(1){
fin.read((char *) g, sizeof(* g));
if(fin.eof())
break;
if(g->getID() != ge->getID()){
fout.write((char *) g, sizeof(* g));
}
}
delete ge;
delete g;
fin.close();
fout.close();
remove("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT");
rename("C:/TURBOC3/BIN/CSPROJ/TEMP.DAT",
"C:/TURBOC3/BIN/CSPROJ/GENRES.DAT");
}
void writeToFile(Genre * g)
{
int id = g->getID();
Genre * ge = new Genre(-1);
fstream f("C:/TURBOC3/BIN/CSPROJ/GENRES.DAT",
ios::binary | ios::in | ios::out);
if(!f){
cout << "\n\n Error. File not found.";
getch();
exit(0);
}
int size = sizeof(* g);
while(1){
f.read((char *) ge, sizeof(*ge));
if(f.eof())
break;
if(ge->getID() == id){
f.seekg(-1 * size, ios::cur);
f.write((char *) g, size);
break;
}
}
delete ge;
f.close();
}
www.cbseportal.com
OUTPUT
Main menu -
Genre menu -
Select genre-
Book menu-
Add book-
Modify book-
WWW.TUTORIALSPOINT.COM
WWW.CPLUSPLUS.COM
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com
www.cbseportal.com