Hi Guys
The thing is that i was asked to make a libaray database managment software for my semester project which i did but now my teacher is asking to give that same project in OOP and i am really bad in that.Can anyone change my program into Object orientied programming ?I have no time left
Hamza_9 0 Newbie Poster
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
#include <iostream>
#include <string>
#include<iomanip>
#include<fstream>
#include <sstream>
#include<time.h>
using namespace std;
void add_book(void);
void add_student(void);
void search_book(string [][3],int );
void search_student(string [][3],int);
void issue(string [][3],string[][3],int,int,int [],string,string, int []);
void retrn(string [][3],string[][3],int,int,int [],string,string, int []);
string due_date(void);
string date_format(void);
void check_due_date(string[],int,string[][3],int,string [][3],string [][5]);
int main ()
{
system ("COLOR 81");
string pass;
//DEFINING LEVEL TOP
top:
system ("cls");
for(int star = 0 ; star < 80 ; star++)
cout << "_";
cout << "" << setw(50) << "Welcome to The Library :" << setw(29) << "";
for(int star = 0 ; star < 80 ; star++)
cout << "_";
cout << endl;
cout << "Please Enter the Password to Gain Access to This Program :";
cin >> pass;
if(pass == "project")
{
cin.ignore();
string date;
date = date_format(); //Calling function for date
//CLERAING SCREEN
system ("cls");
//DEFINING LEVEL TOP
top2:
int choice_section; //Choice of section inside the library (admin/library)
int choice_database; //Choice of database in the admin section(seeing lists, adding books etc)
int choice_lib; //Choice of database in the library section
int choice_issue; //Choice for searching or entering code of the book while issuing it
int count_book[100]; //number of books each user has issued
int user_issue[100][5]; //student code and the code of the book that the user has issued
int user_count[100]={0}; //number of books issued by user
int avail_book[100]; //book availibilty(1 when available and 0 when not available)
// Setting The Book Availbility of all books to 1 (available) and Book Count of Every Student to 1
for (int i = 0 ; i < 100 ; i++)
{
avail_book[i] = 1;
count_book[i] = 1;
}
string pass; //password for entering in the library
string pass_admin; //password for entering in the admin section of the library
string book[100][3]; //array for storing a book's code with its name, author and year of publication
string student[100][3]; //array for storing a student's code with his name,batch and username
string username; //username of student
string date_book[100]; //date on which the book was issued
string date_student[100][5]; //date on which a particular student issued a book
string due_date[100]; //due date of book (date on which the book is to be returned)
string due_date_student[100][5]; //date on which a particular student has to return a book
int book_code = 1; //a unique book code for every book
int student_code = 1; //a unique student code for every student
int book_code_enter; //book code entered by the user while issuing/returning it
int student_book_issue[100] = {0}; //code of the student who has issued the book
//OPENING book.txt TO READ DATA OF BOOKS IN LIBRARY
ifstream InBook;
InBook.open ("books.txt");
do
{
//getting the date from the text file . Storing Book name in subscript [0]
getline (InBook,book[book_code][0],'#');
//storing author name in subscript [1]
getline (InBook,book[book_code][1],'$');
//storing year in subscript [2]
getline (InBook,book[book_code][2]);
book_code++;
}while (InBook.eof() == false);
//getting the final value of book code
book_code--;
InBook.close();
//OPENING student.txt TO READ DATA OF STUDENTS IN LIBRARY
ifstream InStudent;
InStudent.open ("students.txt");
do
{
//Name of Student is stored in Subscript [0]
getline (InStudent,student[student_code][0],'#');
//Batch is stored in subscript [1]
getline (InStudent,student[student_code][1],'$');
//user name is stored in subscript [2]
getline (InStudent,student[student_code][2]);
cout << endl;
student_code++;
} while (InStudent.eof() == false);
for (int i = 1 ; i < book_code ; i++)
{
//using ostringstream to open a txt file by giving a variable name os
//opening the individual txt files of all books to read the data of individual books
ostringstream os;
os << book[i][0] << ".txt";
ifstream InBookAvail ( os.str().c_str() );
do
{
// here i represents book code
// storing 0 if book is not available and 1 if it is from file
InBookAvail >> avail_book[i];
// ignoring $ sign
InBookAvail.ignore();
// storing code of the student that has issued the book
InBookAvail >> student_book_issue[i];
//ignoring & sign
InBookAvail.ignore();
//storing the date when the book was issued
getline (InBookAvail, date_book[i],'*');
// storing the date when the book is to be returned (due date)
getline (InBookAvail, due_date[i]);
} while (!InBookAvail.eof());
InBookAvail.close();
}
for (int i = 1 ; i < student_code-1 ; i++)
{
int j = 0;
//opening txt files of individual students to read data
ostringstream zs;
zs << student[i][0] << ".txt";
ifstream InStudentStatus ( zs.str().c_str() );
//storing code of the book issued by user
// here i represents the users code and j represents the code of the book issued
InStudentStatus >> user_issue[i][j];
//ingoring $ sign
InStudentStatus.ignore();
do
{
//storing date when the book was issued
getline (InStudentStatus, date_student[i][j],'*');
//storing date when book is due
getline (InStudentStatus,due_date_student[i][j]);
j++;
InStudentStatus >> user_issue[i][j];
InStudentStatus.ignore();
//increasing the count (number of book issued by user) by 1
user_count[i]++;
} while (!InStudentStatus.eof());
InStudentStatus.close();
}
//CLEARING SCREEN
system ("cls");
//main options
for (int star = 0 ; star < 49 ; star++)
cout<<"_";
cout << endl;
cout << "\tTo Access the Admin Section Press 1\t\n\tTo Access The Library Section Press 2 \t\n\tTo Quit Program Press 3 \t\t" << endl;
for (int star = 0 ; star < 49 ; star++)
cout << "_";
cout<<endl;
cout << "Enter your choice(1, 2 or 3): ";
cin >> choice_section;
cin.ignore();
if (choice_section == 3)
goto end;
if (choice_section == 1)
{
cout << "Enter the Admin Password ";
cin >> pass_admin;
cin.ignore();
if (pass_admin == "admin")
{
system("cls");
for (int star = 0 ; star < 65 ; star++)
cout << "_";
cout << endl;
cout << "" << setw(35) << "Welcome" << setw(29) << "" << endl;
cout << "\tTo Enter New Books in Database press 1\t\t\t\n\tTo Enter New Student in Student Database Press 2\t\n\tTo See The List of All Students press 3\t\t\t\n\tTo See The List of All Books Press 4\t\t\t\n\tTo Go Back To The Previous Menu Press 5:\t\t" << endl;
for (int star = 0 ; star < 65 ; star++)
cout << "_";
cout << endl;
cout << "Enter your choice(1-5): ";
cin >> choice_database;
cin.ignore();
system ("cls");
if (choice_database == 1)
{
add_book(); //CALLING FUNCTION ADD BOOK
}
else if (choice_database == 2)
{
add_student(); //CALLING FUNCTION ADD STUDENT
}
else if (choice_database == 3)
{
// displaying names of all students
cout << "Student Name" << "\t\t\t" << "Batch" << "\t\t\t" << "UserName" << "\t" << endl;
for(int i = 0 ; i < student_code ; i++)
{
//here i represents respective student code
cout << student[i][0] << "\t\t\t" << student[i][1] << "\t\t\t" << student[i][2] << "\t" << endl;
//the program will display 15 names and then wait for user to press any key before it continues
if (i == 15)
system("pause");
}
system ("pause" );
}
else if (choice_database == 4)
{
//showing all the books
cout << "Book Name" << "\t\t" << " Author Name" << "\t" << "Year Published" << "\t\t" << "code" << endl;
for(int i = 1 ; i < book_code ; i++)
{
//here i represents book code
cout << book[i][0] << "\t\t" << book[i][1] << "\t\t" << book[i][2] << "\t\t" << i << "\t" << endl;
//program will display 10 books and then wait for user to press any key before it continues
if(i == 10)
system ("pause");
}
system ("pause");
}
else if (choice_database == 5)
goto top2;
else
cout << "Wrong Choice "<< endl;
system ("cls");
goto top2;
}
else
{
cout << "Wrong Password Entered ";
system ("pause");
system ("cls");
goto top2;
}
}
else if(choice_section == 2)
{
system ("cls");
for (int star = 0 ; star < 57 ; star++)
cout << "_";
cout << endl;
cout << "\tTo Search For a Books Code Press 1\t\t\n\tTo Search For A Student Press 2\t\t\t\n\tTo Issue A Book Press 3\t\t\t\t\n\tTo Return a Book Press 4\t\t\t\n\tTo Check Status Of A Student Press 5\t\t\n\tTo Check Staus Of A Book Press 6\t\t\n\tTo Check If Any Book Is Due Today Press 7\t\n\tTo Go Back To The Previous Menue Press 8\t"<<endl;
for (int star = 0 ; star < 57 ; star++)
cout << "_";
cout << endl;
cout << "Enter your choice (1-8): ";
cin >> choice_lib;
cin.ignore();
system ("cls");
if (choice_lib == 1 || choice_lib == 2 || choice_lib == 3 || choice_lib == 4 || choice_lib == 5 || choice_lib == 6 || choice_lib == 7 || choice_lib == 8)
{
if (choice_lib == 8)
goto top2;
if (choice_lib == 1)
{
search_book (book,book_code); //CALIING SEARCH BOOK FUNCTION
goto top2;
}
if (choice_lib == 2)
{
search_student (student,student_code); //CALIING SEARCH STUDENT FUNCTION
goto top2;
}
if (choice_lib == 3)
{
cout << "If You know the Code of the Book Press 1 to enter it\nIf You Want to Search for the book first press 2" << endl;
cout << "Enter your choice (1-2): ";
cin >> choice_issue;
cin.ignore();
dchrismoore 0 Newbie Poster
It would stand to reason that the teacher had taught you how to do this before making the assignment. If you have a working program, modifying it to be object oriented should not be so difficult, as long as you put some thought into it.
Why would you expect somone else to write it for you?
Also, you shouldn't be using GOTO. The teacher should have failed you for that alone.
Edited by dchrismoore
Hamza_9 commented: Ok what do you suggest me to use ? +0
Traevel 216 Light Poster
This is not what they mean by Delegation Principle.
Obviously no one will redesign your application to comply with OO principles for you.
You can start making your application OO by implementing the first five principles (S.O.L.I.D.).
Single Responsibility Principle - Each class should have its own responsibility. For instance a
Book
class does not containStudent
information.Open Closed Principle - Each class should be open for extension and closed to modification. For instance if you want to add a new book type that can only be read in the library but not lent out you do not want to have to modify the
Book
class. Instead you'll create something like aLibraryItem
whichBook
extends. Later on you can then add aReadOnlyBook
as an extension as well.Liskov Substition Principle - Objects should be replacable by instances of their subtypes. For instance if you have a
LibraryItem
, andBook
extendsLibraryItem
, then in every place where you useLibraryItem
you must be able to replace it with aBook
instance and not alter the behaviour of your application.Interface Segregation Principle - Interfaces should be specific to their clients. For instance if you had a
ScannableItem
interface, which could be a book, video or library card, you would only force methods/functions that are applicable, likescan()
, and not something likeread()
orwatch()
which are specific to items and should be in their own interfaces. Otherwise you'd be forced to implement empty variations in items that can't be watched or read.Dependency Inversion Principle - Depend upon abstractions, not concretions. For instance a
LibraryScanner
class should not know whether the item being lent out is aBook
,Video
orMagazine
, but instead communicate to it via a common interface likeItem
(which could force a method likegetPrice()
). That way if you add a new item you don't have to changeLibrary
, and if you changeLibrary
you don't have to changeBook
,Video
orMagazine
. Their only dependency is via the interfaceItem
.
So in conclusion I would start by chopping up the application into responsibilities (on paper/uml/diagram/napkin). Then establish where the classes will "touch" and put interfaces inbetween to decouple them (think Item
interface for Book
, Video
to put between a LibraryScanner
class etc.). It's always a good idea to check if any Design Patterns can be applied to the result, which can lead to some extra classes/interfaces.
Once you have that in an image, start by applying the changes to the code. Remove entities from the large class and put them in their own classes, which extend/implement the proper abstraction (a Book
that implements Item
for instance). Then split up the logical parts. For instance, inventory management and scanning/returning can be separated. A Scanner
class could then ask an Inventory
class (or even better, their interfaces!) to add/remove a book (Item
!) that has been scanned. Other parts that could be separated are the student management, the user input and so on.
Even if you run out of time now, it will be good practice to apply OO to this when you have some time to spare. That way, in the future, you'll be able to start designing in OO from the start and won't have to spend all that time afterwards to change it.
Hamza_9 0 Newbie Poster
Can you tell me any good website or tutorial videos so i can improve my OO Skills ?
David W 131 Practically a Posting Shark
Why don't you start fresh and code a little name, phone_number and email type data base?
You could use a C++ vector container to hold all the data records (objects)
You might start out with something like this:
class Person
{
public:
Person( const string& name="", const string& phone="", const string& email="" )
: name(name), phone(phone), email(email) {}
// other stuff //
private:
string name;
string phone;
string email;
} ;
David W 131 Practically a Posting Shark
This link may give you some more ideas about a way to proceed ...
http://developers-heaven.net/forum/index.php/topic,2585.0.html
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.