السلام عليكم
كل عام وانتم بخير وعساكم من عواده ..
ندخل للموضوع :unsure:
السؤال :
اقتباسDesign a software application using C++ that helps a bookstore to manage its activities. The bookstore is selling only books and dictionaries. For that purpose, you will define a base class : Document and two derived classes : Book and Dictionary.
The class Document have the following data members:
..
..
..
Class Book inherits from class Document and has the following data members:
..
..
Class Dictionary inherits from class Document and has the following data members:
..
Your application should implemented in a bookstore.cpp file. It should maintain a file called document.dat which stores objects of type Book and/or Dictionary. The bookstore should be able to add new items to the file when it purchases new documents using two global functions:
void addNewBook( const Book& )
void addNewDictionary( const Dictionary& )
Since Book and Dictionary object have different sizes and written in the same file document.dat ( Data file ), you need to maintain a second file called document,ind ( Index File ) that stores for each item in the Data File a corresponded object of type Index where Index is a class that contains the following data members:
int id
long position
int size
enum { Book, Dictionary }
When a document ( Book or Dictionary ) is sold, it should be removed from the Data File ( document.dat ) and its index should be removed from the Index File ( document.ind ). The removed document should be added to a file maintaining sold documents ( document.sol ) as a Document object ( not as a Book or as a Dictionary ).
The following global functions removes an item (Book or Dictionary ) from the Data File and its corresponded index from the Index File and converts the removed item to a Document object that should be appended to the file document.sol
void removeItem( const int )
You should helps the bookstore to compute its benefits ( number of sold items * ( price – cost ) ). The following global function should use the file document.sol in order to compute total benefits of the bookstore.
double computeBenefits()
Also, the bookstore should be able to increase or decrease the price of an item by using the following global function:
void modifyPrice( int id, double newPrice )
if the bookstore buys for example 5 items of the same Book ( same author and same title ), 5 items are added to Data File and the Index File ( these 5 items have different ids ).
المشكلة لدي هي :
عند كتابة أي برنامج يستخدم ملف Random Access File فانه يجب ان اقوم بكتابة 100 objects فارغة سابقا . يعني اسوي for loop من 1 الى 100 واجعلها تكرر دالة الكتابة 100 مرة لكتابة 100 objects فارغ .
مثلاً ، في السؤال الموجود في الاعلى ، فانا احتاج لكتابة الكود التالي :
اقتباسoutFile( “document.dat” , ios::out | iso::binary );
Document blankDocument;
for ( int i =0 ; i < 100 ; i++ )
outFile.write( reinterpret_cast < const char * >( &blankDocument) , sizeof(Document) );
سوف يقوم البرنامج بإنشاء شيء (أوبجيكت) بالقيم الافتراضية ويقوم بكتابته في binary file 100 مره .
المشكلة ان حجم document object يختلف عن حجم book object و dictionary object ( كما هو مكتوب في السؤال ) ، لذلك انا توقفت ولم اعرف مالعمل بعدها وكيف استفيد من ملف جديد Index File ؟
كذلك بالنسبة ل enum الموجودة في index class ، لا اعرف كيف اجعلها في private كـ data member ، وكيف اقوم بعطاء قيمة ابتدائية في constructor ..
كذلك ملف document.sol ، هل اجعله sequential file او binary file ؟
بصراحة هذا البرنامج لم استطع عمل شيء منه الا كتابة الـ classes فقط ، وجزئية الملفات .dat و .ind و .sol محيرتني تماماً !
اتمنى المساعدة في اقرب فرصة وشكراً .