I have created a Library Management system. Can anyone please look at if see if there's any bad practices or if anything could be done better?
Main.cpp
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include<stdio.h>
#include <string.h>
#include "Book.h"
#include "DVD.h"
#include "Student.h"
using namespace std;
int main() {
Student student4;
Book book2;
DVD dvd2;
while(1)
{
char mainSelect;
char studentOption;
char dvdOption;
char bookOption;
char name[30];
bool processed = false;
while(!processed)
{
// User Menu
// Read user selection
cin.getline( name, 80);
mainSelect = name[0];
// Switch statement to select between the options
switch (mainSelect)
{
case '1':
processed = true;
break;
case '2':
processed = true;
break;
case '3':
processed = true;
break;
case '4':
processed = true;
exit(0);
break;
case '5':
default:
cout<<"Incorrect selection. Please select from the given options." <<endl;
processed = false;
break;
}
};
if (mainSelect == '1')
// User Menu
bool processed = false;
while(!processed)
{
cin.getline( name, 80);
dvdOption = name[0];
switch(dvdOption)
{
case '1':
dvd2.issueDVD();
processed = true;
break;
case '2':
dvd2.returnDVD();
processed = true;
break;
case '3':
dvd2.insertDVD();
processed = true;
break;
case '4':
dvd2.updateDVD();
processed = true;
break;
case '5':
dvd2.deleteDVD();
processed = true;
break;
case '6':
char barcode[6];
cout<<"Enter The DVD barcode: " <<endl;
cin>>barcode;
dvd2.searchDVD(barcode);
processed = true;
break;
case '7':
dvd2.showallDVDs();
processed = true;
break;
case '8':
processed = true;
break;
case '9':
exit(0);
break;
case '10':
default:
cout<<"Incorrect selection. Please select from the given options." <<endl;
processed = false;
break;
}
}
}
else if (mainSelect == '2')
{
// User Menu
cin.getline(name, 80);
bookOption = name[0];
switch(bookOption)
{
case '1':
book2.issueBook();
break;
case '2':
book2.returnBook();
break;
case '3':
book2.insertBook();
break;
case '4':
book2.updateBook();
break;
case '5':
book2.deleteBook();
break;
case '6':
char barcode[6];
cout<<"Enter The book barcode: " <<endl;
cin>>barcode;
book2.searchBook(barcode);
break;
case '7':
book2.showallBooks();
break;
case '8':
break;
case '9':
exit(0);
break;
}
}
else if (mainSelect=='3')
{
// User Menu
cin.getline( name, 80);
studentOption = name[0];
switch(studentOption){
case '1':
student4.insertStudent();
break;
case '2':
student4.showallStudents();
break;
case '3':
char regno[6];
cout<<"Enter the registration no. of the student you want to search: "<<endl;
cin>>regno;
student4.searchStudent(regno);
break;
case '4':
student4.updateStudent();
break;
case '5':
student4.deleteStudent();
break;
case '6':
exit(0);
break;
case '7':
cout << "Invalid selection!" << endl;
break;
}
}
return 0;
};
Media.h
#ifndef MEDIA_H_
#define MEDIA_H_
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
class Media
{
public:
char barcode[6];
char name[50];
char num[6];
char* retBarcode()
{
return barcode;
}
};
#endif /* MEDIA_H_ */
Book.h
#ifndef BOOK_H_
#define BOOK_H_
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "Media.h"
using namespace std;
class Book : public Media
{
char author[20];
public:
void insertBook();
void searchBook(char barcode[]);
void updateBook();
void deleteBook();
void showallBooks();
void issueBook();
void returnBook();
void newBook()
{
cout<<"Enter the Book Name: " <<endl;
fgets(name, sizeof(name), stdin);
cout<<"Enter the Name of Author: " <<endl;
fgets(author, sizeof(author), stdin);
cout<<"Enter the book barcode: " <<endl;
cin>>barcode;
cout<<"Book Added to the library" <<endl;
}
void displayBook()
{
cout<<"Enter the Book Name: " <<endl;
puts(name);
cout<<"Enter the Name of Author: ";
puts(author);
cout<<"Enter the Book barcode: " <<endl;
cin>>barcode;
}
void editBook()
{
cout<<"Enter the Book Name you want to Update: " <<endl;
cin>>barcode;
cout<<"Enter New Book Name: " <<endl;
fgets(name, sizeof(name), stdin);
cout<<"Enter New Name of Author: " <<endl;
fgets(author, sizeof(author), stdin);
}
void report()
{
cout<<"______________"<<" __________________"<<" _________________"<<endl;
cout<<"|Name of Book| "<<"|Director of Book| "<<"|Barcode of Book| " <<endl;
cout<<" "<<name<<"------------"<<author<<"-----------------"<<barcode<<endl;
}
}; //class ends here
#endif /* BOOK_H_ */
Book.cpp
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <iostream>
#include<iomanip>
#include "Book.h"
#include "Student.h"
using namespace std;
Student student2;
Book book1;
void Book::insertBook()
{
fstream file;
file.open("book.dat",ios::out|ios::app);
newBook();
file.write((char*)&book1,sizeof(Book));
file.close();
}
void Book::searchBook(char barcode[6])
{
fstream file;
int sys=0;
file.open("book.dat",ios::in);
while(file.read((char*)&book1,sizeof(Book)))
{
if(strcmp(retBarcode(),barcode)==0)
{
displayBook();
sys=1;
}
}
file.close();
if(sys==0)
cout<<"Error: No such Book found in System." <<endl;
}
void Book::updateBook()
{
fstream file;
int found=0;
char barcode[6];
cout<<"Enter barcode of the Book you want to update:" <<endl;
cin>>barcode;
file.open("book.dat",ios::in|ios::out);
while(file.read((char*)&book1,sizeof(Book)) && found==0)
{
if(strcmp(retBarcode(),barcode)==0)
{
displayBook();
editBook();
long pos=-1*sizeof(book1);
file.seekp(pos,ios::cur);
file.write((char*)&book1,sizeof(Book));
cout<<"Book updated"<<endl;
found=1;
}
}
file.close();
if(found==0)
cout<<"Error:Book Not Found. " <<endl;
}
void Book::deleteBook()
{
fstream file, file2;
char barcode[6];
cout<<"Enter barcode of the Book you want to delete: " <<endl;
cin>>barcode;
file.open("book.dat",ios::in|ios::out);
file2.open("Temp.dat",ios::out);
file.seekg(0,ios::beg);
while(file.read((char*)&book1,sizeof(Book)))
{
if(strcmp(retBarcode(),barcode)!=0)
{
file2.write((char*)&book1,sizeof(Book));
}
}
file2.close();
file.close();
remove("book.dat");
rename("Temp.dat","book.dat");
cout<<"Book deleted." <<endl;
}
void Book::showallBooks()
{
fstream file;
file.open("book.dat",ios::in);
if(!file)
{
cout<<"Error: File could not be opened. " <<endl;
return;
}
while(file.read((char*)&book1,sizeof(Book)))
{
report();
break;
}
file.close();
}
void Book::issueBook()
{
fstream file,file1;
char studentno[6],bookno[6];
int found=0,sys=0;
cout<<"Enter the Registration no. of the Student: "<<endl;
cin>>studentno;
file.open("student.dat",ios::in|ios::out);
file1.open("book.dat",ios::in|ios::out);
while(file.read((char*)&student2,sizeof(Student)) && found==0)
{
if(strcmp(student2.retregistrationNo(),studentno)==0)
{
found=1;
cout<<"Enter barcode of the Book you want to issue: "<<endl;
cin>>bookno;
while(file1.read((char*)&book1,sizeof(Book))&& sys==0)
{
if(strcmp(retBarcode(),bookno)==0)
{
displayBook();
sys=1;
student2.getstudentbookBar(retBarcode());
long pos=-1*sizeof(student2);
file.seekp(pos,ios::cur);
file.write((char*)&student2,sizeof(Student));
cout<<"Book issued";
}
}
if(sys==0)
cout<<"Error: Book barcode does not exist. "<<endl;
}
}
if(found==0)
cout<<"Error: Student does not exist in the system. "<<endl;
file.close();
file1.close();
}
void Book::returnBook()
{
fstream file, file1;
char studentno[6];
int found=0,sys=0;
cout<<"Enter the Registration no. of the Student: "<<endl;
cin>>studentno;
file.open("student.dat",ios::in|ios::out);
file1.open("book.dat",ios::in|ios::out);
while(file.read((char*)&student2,sizeof(Student)) && found==0)
{
if(strcmp(student2.retregistrationNo(),studentno)==0)
{
found=1;
while(file1.read((char*)&book1,sizeof(Book))&& sys==0)
{
if(strcmp(retBarcode(),student2.retstudentbookBar())==0)
{
displayBook();
sys=1;
long pos=-1*sizeof(student2);
file.seekp(pos,ios::cur);
file.write((char*)&student2,sizeof(Student));
cout<<"Book returned. "<<endl;
}
}
if(sys==0)
cout<<"Error: Book barcode does not exist. "<<endl;
}
if(found==0)
cout<<"Error: Student not found in the system. "<<endl;
file.close();
file1.close();
}
};
DVD.h
#ifndef DVD_H_
#define DVD_H_
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "Media.h"
using namespace std;
class DVD : public Media{
char director[20];
public:
void insertDVD();
void searchDVD(char barcode[]);
void updateDVD();
void deleteDVD();
void showallDVDs();
void issueDVD();
void returnDVD();
void newDVD()
{
cout<<"Enter The DVD Name: " <<endl;
fgets(name, sizeof(name), stdin);
cout<<"Enter the Name of Director: " <<endl;
fgets(director, sizeof(director), stdin);
cout<<"Enter the DVD barcode: " <<endl;
cin>>barcode;
cout<<"DVD added to the library" <<endl;
}
void displayDVD()
{
cout<<"Enter the DVD Name: " <<endl;
puts(name);
cout<<"Enter the Name of Director: " <<endl;
puts(director);
cout<<"Enter the DVD barcode: " <<endl;
cin>>barcode;
}
void editDVD()
{
cout<<"Enter the DVD Name you want to Update: " <<endl;
cin>>barcode;
cout<<"Enter New DVD Name: " <<endl;
fgets(name, sizeof(name), stdin);
cout<<"Enter New Name of Director: " <<endl;
fgets(director, sizeof(director), stdin);
}
void report()
{
cout<<"_____________"<<" _________________"<<" ________________"<<endl;
cout<<"|Name of DVD| "<<"|Director of DVD| "<<"|Barcode of DVD| " <<endl;
cout<<" "<<name<<"-----------"<<director<<"---------------"<<barcode<<endl;
}
}; //class ends here
#endif /* DVD_H_ */
DVD.cpp
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "DVD.h"
#include "Student.h"
using namespace std;
Student student3;
DVD dvd1;
void DVD::insertDVD()
{
fstream file;
file.open("dvd.bin",ios::out|ios::app);
newDVD();
file.write((char*)&dvd1,sizeof(DVD));
file.close();
}
void DVD::searchDVD(char barcode[6])
{
fstream file;
int sys=0;
file.open("dvd.bin",ios::in);
while(file.read((char*)&dvd1,sizeof(DVD)))
{
if(strcmp(retBarcode(),barcode)==0)
{
displayDVD();
sys=1;
}
}
file.close();
if(sys==0)
cout<<"Error: No such DVD in System." <<endl;
}
void DVD::updateDVD()
{
fstream file;
char barcode[6];
int found=0;
cout<<"Enter barcode of the DVD you want to update: " <<endl;
cin>>barcode;
file.open("dvd.bin",ios::in|ios::out);
while(file.read((char*)&dvd1,sizeof(DVD)) && found==0)
{
if(strcmp(retBarcode(),barcode)==0)
{
displayDVD();
cout<<"Enter The New Details of DVD"<<endl;
editDVD();
long pos=-1*sizeof(dvd1);
file.seekp(pos,ios::cur);
file.write((char*)&dvd1,sizeof(DVD));
cout<<"DVD Updated" <<endl;
found=1;
}
}
file.close();
if(found==0)
cout<<"Error: No such DVD in System. " <<endl;
}
void DVD::deleteDVD()
{
fstream file,file2;
char barcode[6];
cout<<"Enter Barcode of the DVD you want to delete : " <<endl;
cin>>barcode;
file.open("dvd.bin",ios::in|ios::out);
file2.open("Temp.bin",ios::out);
file.seekg(0,ios::beg);
while(file.read((char*)&dvd1,sizeof(DVD)))
{
if(strcmp(retBarcode(),barcode)!=0)
{
file2.write((char*)&dvd1,sizeof(DVD));
}
}
file2.close();
file.close();
remove("dvd.bin");
rename("Temp.bin","dvd.bin");
cout<<"DVD deleted from the System. " <<endl;
}
void DVD::showallDVDs()
{
fstream file;
file.open("dvd.bin",ios::in);
if(!file)
{
cout<<"Error: File could not be opened. " <<endl;
return;
}
while(file.read((char*)&dvd1,sizeof(DVD)))
{
report();
break;
}
file.close();
}
void DVD::issueDVD()
{
fstream file,file1;
char studentno[6],bookno[6];
int found=0,sys=0;
cout<<"Enter the Registration no. of the Student: "<<endl;
cin>>studentno;
file.open("student.bin",ios::in|ios::out);
file1.open("book.bin",ios::in|ios::out);
while(file.read((char*)&student3,sizeof(Student)) && found==0)
{
if(strcmp(student3.retregistrationNo(),studentno)==0)
{
found=1;
cout<<"Enter barcode of the Book you want to issue: "<<endl;
cin>>bookno;
while(file1.read((char*)&dvd1,sizeof(DVD))&& sys==0)
{
if(strcmp(retBarcode(),bookno)==0)
{
displayDVD();
sys=1;
student3.getstudentbookBar(retBarcode());
long pos=-1*sizeof(student3);
file.seekp(pos,ios::cur);
file.write((char*)&student3,sizeof(Student));
cout<<"Book issued";
}
}
if(sys==0)
cout<<"Error: Book barcode does not exist. "<<endl;
}
}
if(found==0)
cout<<"Error: Student does not exist in the system. "<<endl;
file.close();
file1.close();
}
void DVD::returnDVD()
{
fstream file,file1;
long found=0,sys=0;
char studentno[6];
cout<<"Enter the Registration no. of Student:" <<endl;
cin>>studentno;
file.open("student.bin",ios::in|ios::out);
file1.open("book.bin",ios::in|ios::out);
while(file.read((char*)&student3,sizeof(Student)) && found==0)
{
if(strcmp(student3.retregistrationNo(),studentno)==0)
{
found=1;
while(file1.read((char*)&dvd1,sizeof(DVD))&& sys==0)
{
if(strcmp(retBarcode(),student3.retstudentdvdBar())==0)
{
displayDVD();
sys=1;
long pos=-1*sizeof(student3);
file.seekp(pos,ios::cur);
file.write((char*)&student3,sizeof(Student));
cout<<"Book returned" <<endl;
}
}
if(sys==0)
cout<<"Error: No DVD found with the entered barcode." <<endl;
}
}
if(found==0)
cout<<"Error: Student not found in the system. " <<endl;
file.close();
file1.close();
}
Student.h
#ifndef STUDENT_H_
#define STUDENT_H_
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;
class Student
{
char registrationno[6];
char name[20];
char stbookbar[6];
char stdvdbar[6];
public:
void insertStudent();
void searchStudent(char regno[]);
void updateStudent();
void deleteStudent();
void showallStudents();
char* retregistrationNo()
{
return registrationno;
}
char* retstudentbookBar()
{
return stbookbar;
}
char* retstudentdvdBar()
{
return stdvdbar;
}
void getstudentbookBar(char t[])
{
strcpy(stbookbar,t);
}
void getstudentdvdBar(char t[])
{
strcpy(stdvdbar,t);
}
void report()
{
cout<<"_________________"<<" _________________"<<endl;
cout<<"|Name of Student| "<<"|Registration No|" <<endl;
cout<<" "<<name<<"-----------"<<registrationno<<endl;
}
void newStudent()
{
cout<<"Enter the registration no. " <<endl;
cin>>registrationno;
cin.ignore();
cout<<"Enter the name of the student " <<endl;
fgets(name, sizeof(name), stdin);
stbookbar[0]='/0';
cout<<"Student added to system." <<endl;
}
void displayStudent()
{
cout<<"Enter the registration no. : " <<endl;
cin>>registrationno;
cin.ignore();
cout<<"Enter the name of the student: " <<endl;
puts(name);
}
void editStudent()
{
cout<<"registration no. : " <<endl;
cin>>registrationno;
cout<<"Modify Student Name : " <<endl;;
fgets(name, sizeof(name), stdin);
}
}; //class ends here
#endif /* STUDENT_H_ */
Student.cpp
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "Student.h"
using namespace std;
Student student1;
void Student::insertStudent()
{
fstream file;
file.open("student.dat",ios::out|ios::app);
newStudent();
file.write((char*)&student1,sizeof(Student));
file.close();
}
void Student::searchStudent(char regno[])
{
fstream file;
int flag=0;
file.open("student.dat",ios::in);
while(file.read((char*)&student1,sizeof(Student)))
{
if((strcmp(retregistrationNo(),regno)==0))
{
displayStudent();
flag=1;
}
}
file.close();
if(flag==0)
cout<<"Error: Student not found in the system. "<<endl;
}
void Student::updateStudent()
{
fstream file;
char regno[6];
int found=0;
cout<<"Enter Student Registration no. " <<endl;
cin>>regno;
file.open("student.dat",ios::in|ios::out);
while(file.read((char*)&student1,sizeof(Student)) && found==0)
{
if(strcmp(retregistrationNo(),regno)==0)
{
displayStudent();
cout<<"Enter student's new details"<<endl;
editStudent();
long pos=-1*sizeof(student1);
file.seekp(pos,ios::cur);
file.write((char*)&student1,sizeof(Student));
cout<<"Student's details Updated"<<endl;
found=1;
}
}
file.close();
if(found==0)
cout<<"Error: Student not found in the system. "<<endl;
}
void Student::deleteStudent()
{
fstream file, file2;
char n[6];
int flag=0;
cout<<"Enter The registration no. of the Student You Want To Delete : "<<endl;
cin>>n;
file.open("student.dat",ios::in|ios::out);
file2.open("Temp.dat",ios::out);
file.seekg(0,ios::beg);
while(file.read((char*)&student1,sizeof(Student)))
{
if(strcmp(retregistrationNo(),n)!=0)
file2.write((char*)&student1,sizeof(Student));
else
flag=1;
}
file2.close();
file.close();
remove("student.dat");
rename("Temp.dat","student.dat");
if(flag==1)
cout<<"Student deleted from the system. " <<endl;
else
cout<<"Error: Student not found in the system. " <<endl;
}
void Student::showallStudents()
{
fstream file;
file.open("student.dat",ios::in);
if(!file)
{
cout<<"Error: File could not be opened. "<<endl;
return;
}
while(file.read((char*)&student1,sizeof(Student)))
{
report();
break;
}
file.close();
}