digioz
10-07-03, 10:46 PM
Hi all,
I saw this forum was empty, so here is a short Command Line Based Item Order Program I wrote, which can be modified to use with any item. You can download and use it for free if you like. I also included a compiled version of it for Windows. Enjoy!!
#include <iostream.h>
#include <fstream.h>
// ----------------------- Class Item --------------------------------------
class Item
{
private:
int itemno, quantity;
float unitPrice;
public:
virtual void getData();
virtual void showData();
long getId(){return itemno;}
virtual char getType()=0;
};
void Item::getData(){cout<<"\nEnter Item Number: "; cin>>itemno;
cout<<"\n Quantity: "; cin>>quantity;
cout<<"\n Unit Price: "; cin>>unitPrice;
}
void Item::showData(){cout<<endl<<itemno<<" "<<quantity<<" "<<unitPrice;}
// ------------------------ Class Book -------------------------------------
class Book: public Item
{
private:
char author[30];
char subject[30];
int pages;
public:
void getData(){ cout<<"\nItem Information: ";Item::getData();
cout<<"\n Enter Author:"; cin.ignore(); cin.get(author, 30);
cout<<"\n Enter Subject:"; cin.ignore(); cin.get(subject, 30);
cout<<"\nPages:"; cin>>pages;
}
void showData(){ cout<<"\nItem Information: "; Item::showData();
cout<<endl<<author<<" "<<subject<<" "<<pages;}
char getType(){return 'b';}
};
// ------------------------ Class CD ----------------------------------------
class CD: public Item
{
private:
char title[30];
char publisher[30];
int size;
public:
void getData(){cout<<"\nItem information: "; Item::getData();
cout<<"\n CD Title: "; cin.ignore(); cin.get(title, 30);
cout<<"\n CD Publisher: "; cin.ignore(); cin.get(publisher, 30);
cout<<"\n CD Size: "; cin>>size;}
void showData(){cout<<"\nItem information: "; Item::showData();
cout<<endl<<title<<" "<<publisher<<" "<<size;}
char getType(){return 'c';}
};
// ------------------------- Class VTape -----------------------------------
class VTape: public Item
{
private:
char subject[30];
int minutes;
int version;
public:
void getData(){cout<<"\nItem information: "; Item::getData();
cout<<"\n Subject: "; cin.ignore(); cin.get(subject, 30);
cout<<"\n Minutes: "; cin>>minutes;
cout<<"\n Version: "; cin>>version;}
void showData(){cout<<"\nItem information: "; Item::showData();
cout<<endl<<subject<<" "<<minutes<<" "<<version;}
char getType(){return 't';}
};
// -------------------------- Add Object ----------------------------------
void addObject(Item *p, fstream& f, char *str, char tag, int size)
{
f.open(str, ios::app|ios::binary);
f.write(&tag, 1);
f.write((char *)p, size);
f.close();
}
// ----------------------- Display Object ---------------------------------
void display(fstream& f, char *fname)
{
Item *p;
int count=0, size;
char tag;
f.open(fname, ios::in|ios::binary);
f.read(&tag, 1);
while(!f.eof())
{
switch(tag)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
f.read((char*)p, size);
p->showData();
delete p;
count++;
f.read(&tag,1);
}
f.close();
cout<<endl<<count<<" objects were listed.";
}
// ----------------------- Display Certain Object ------------------
void display(fstream& f, char fname[], char& tag)
{
Item *p;
int count=0, size;
char x;
f.open(fname, ios::in|ios::binary);
f.read(&x, 1);
while(!f.eof())
{
switch(x)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
if(x==tag)
{
f.read((char *)p, size);
p->showData();
delete p;
count++;
}
else
f.seekg(size, ios::cur);
f.read(&x, 1);
}
f.close();
cout<<endl<<count<<" objects were listed.";
}
// ----------------------- Search File ---------------------------------
void search(fstream f, char *fname, long id, char tag)
{
Item *p;
char x;
int size;
bool found=false;
f.open(fname, ios::in|ios::binary);
f.read(&x, 1);
while(!f.eof() && !found)
{
switch(x)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
if(x!=tag)
f.seekg(size, ios::cur);
else
f.read((char *)p, size);
if(p->getId()==id)
{
p->showData();
found=true;
delete p;
break;
}
f.read(&x, 1);
}
f.close();
if(!found)
cout<<"\nNo matching Item found in our file!!!!!!!";
}
// ----------------------- Program Menu ---------------------------------
int menu()
{
int c;
cout<<"\n I T E M O R D E R P R O G R A M";
cout<<"\n -----------------------------------";
cout<<"\n\nEnter: 1.....To add new Book to the file";
cout<<"\n 2.....To add new CD to the file";
cout<<"\n 3.....to add new VTape to the file";
cout<<"\n 4.....Show all Books from the file";
cout<<"\n 5.....Show all CDs from the file";
cout<<"\n 6.....Show all VTapes from the file";
cout<<"\n 7.....Show Everything from the file";
cout<<"\n 8.....Search for a Book in the file";
cout<<"\n 9.....Search for a CD in the file";
cout<<"\n 10.....Search for a Video Tape in the file";
cout<<"\n 11.....Search for a Specific Item";
cout<<"\n\nEnter your choice: "; cin>>c;
return c;
}
// ----------------------- Main Function --------------------------------
void main()
{
Item *p;
fstream file;
char tag;
long id;
int choice, size;
choice=menu();
char fileName[]="items.dat";
while(choice > 0 && choice < 12)
{
switch(choice)
{
case 1: p=new Book;
p->getData();
tag=p->getType();
size=sizeof(Book);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 2: p=new CD;
p->getData();
tag=p->getType();
size=sizeof(CD);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 3: p=new VTape;
p->getData();
tag=p->getType();
size=sizeof(VTape);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 4: cout<<"\nL I S T O F B O O K S\n";
tag = 'b';
display(file, fileName, tag);
break;
case 5: cout<<"\nL I S T O F C D\n";
tag = 'c';
display(file, fileName, tag);
break;
case 6: cout<<"\nL I S T O F V I D E O T A P E S\n";
tag = 't';
display(file, fileName, tag);
break;
case 7: cout<<"\nL I S T O F A L L I T E M S\n";
display(file, fileName);
break;
case 8: cout<<"Enter Item id :"; cin>>id;
tag = 'b';
search(file, fileName, id, tag);
break;
case 9: cout<<"Enter Item id :"; cin>>id;
tag = 'c';
search(file, fileName, id, tag);
break;
case 10:cout<<"Enter Item id :"; cin>>id;
tag = 't';
search(file, fileName, id, tag);
break;
case 11:cout<<"Enter Item id :"; cin>>id;
cout<<"\nEnter b..Books, c..CDs, t..VTapes: ";
cin>>tag;
search(file, fileName, id, tag);
}
choice=menu();
}
}
I saw this forum was empty, so here is a short Command Line Based Item Order Program I wrote, which can be modified to use with any item. You can download and use it for free if you like. I also included a compiled version of it for Windows. Enjoy!!
#include <iostream.h>
#include <fstream.h>
// ----------------------- Class Item --------------------------------------
class Item
{
private:
int itemno, quantity;
float unitPrice;
public:
virtual void getData();
virtual void showData();
long getId(){return itemno;}
virtual char getType()=0;
};
void Item::getData(){cout<<"\nEnter Item Number: "; cin>>itemno;
cout<<"\n Quantity: "; cin>>quantity;
cout<<"\n Unit Price: "; cin>>unitPrice;
}
void Item::showData(){cout<<endl<<itemno<<" "<<quantity<<" "<<unitPrice;}
// ------------------------ Class Book -------------------------------------
class Book: public Item
{
private:
char author[30];
char subject[30];
int pages;
public:
void getData(){ cout<<"\nItem Information: ";Item::getData();
cout<<"\n Enter Author:"; cin.ignore(); cin.get(author, 30);
cout<<"\n Enter Subject:"; cin.ignore(); cin.get(subject, 30);
cout<<"\nPages:"; cin>>pages;
}
void showData(){ cout<<"\nItem Information: "; Item::showData();
cout<<endl<<author<<" "<<subject<<" "<<pages;}
char getType(){return 'b';}
};
// ------------------------ Class CD ----------------------------------------
class CD: public Item
{
private:
char title[30];
char publisher[30];
int size;
public:
void getData(){cout<<"\nItem information: "; Item::getData();
cout<<"\n CD Title: "; cin.ignore(); cin.get(title, 30);
cout<<"\n CD Publisher: "; cin.ignore(); cin.get(publisher, 30);
cout<<"\n CD Size: "; cin>>size;}
void showData(){cout<<"\nItem information: "; Item::showData();
cout<<endl<<title<<" "<<publisher<<" "<<size;}
char getType(){return 'c';}
};
// ------------------------- Class VTape -----------------------------------
class VTape: public Item
{
private:
char subject[30];
int minutes;
int version;
public:
void getData(){cout<<"\nItem information: "; Item::getData();
cout<<"\n Subject: "; cin.ignore(); cin.get(subject, 30);
cout<<"\n Minutes: "; cin>>minutes;
cout<<"\n Version: "; cin>>version;}
void showData(){cout<<"\nItem information: "; Item::showData();
cout<<endl<<subject<<" "<<minutes<<" "<<version;}
char getType(){return 't';}
};
// -------------------------- Add Object ----------------------------------
void addObject(Item *p, fstream& f, char *str, char tag, int size)
{
f.open(str, ios::app|ios::binary);
f.write(&tag, 1);
f.write((char *)p, size);
f.close();
}
// ----------------------- Display Object ---------------------------------
void display(fstream& f, char *fname)
{
Item *p;
int count=0, size;
char tag;
f.open(fname, ios::in|ios::binary);
f.read(&tag, 1);
while(!f.eof())
{
switch(tag)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
f.read((char*)p, size);
p->showData();
delete p;
count++;
f.read(&tag,1);
}
f.close();
cout<<endl<<count<<" objects were listed.";
}
// ----------------------- Display Certain Object ------------------
void display(fstream& f, char fname[], char& tag)
{
Item *p;
int count=0, size;
char x;
f.open(fname, ios::in|ios::binary);
f.read(&x, 1);
while(!f.eof())
{
switch(x)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
if(x==tag)
{
f.read((char *)p, size);
p->showData();
delete p;
count++;
}
else
f.seekg(size, ios::cur);
f.read(&x, 1);
}
f.close();
cout<<endl<<count<<" objects were listed.";
}
// ----------------------- Search File ---------------------------------
void search(fstream f, char *fname, long id, char tag)
{
Item *p;
char x;
int size;
bool found=false;
f.open(fname, ios::in|ios::binary);
f.read(&x, 1);
while(!f.eof() && !found)
{
switch(x)
{
case 'b':p=new Book;
size=sizeof(Book);
break;
case 'c':p=new CD;
size=sizeof(CD);
break;
case 't':p=new VTape;
size=sizeof(VTape);
break;
}
if(x!=tag)
f.seekg(size, ios::cur);
else
f.read((char *)p, size);
if(p->getId()==id)
{
p->showData();
found=true;
delete p;
break;
}
f.read(&x, 1);
}
f.close();
if(!found)
cout<<"\nNo matching Item found in our file!!!!!!!";
}
// ----------------------- Program Menu ---------------------------------
int menu()
{
int c;
cout<<"\n I T E M O R D E R P R O G R A M";
cout<<"\n -----------------------------------";
cout<<"\n\nEnter: 1.....To add new Book to the file";
cout<<"\n 2.....To add new CD to the file";
cout<<"\n 3.....to add new VTape to the file";
cout<<"\n 4.....Show all Books from the file";
cout<<"\n 5.....Show all CDs from the file";
cout<<"\n 6.....Show all VTapes from the file";
cout<<"\n 7.....Show Everything from the file";
cout<<"\n 8.....Search for a Book in the file";
cout<<"\n 9.....Search for a CD in the file";
cout<<"\n 10.....Search for a Video Tape in the file";
cout<<"\n 11.....Search for a Specific Item";
cout<<"\n\nEnter your choice: "; cin>>c;
return c;
}
// ----------------------- Main Function --------------------------------
void main()
{
Item *p;
fstream file;
char tag;
long id;
int choice, size;
choice=menu();
char fileName[]="items.dat";
while(choice > 0 && choice < 12)
{
switch(choice)
{
case 1: p=new Book;
p->getData();
tag=p->getType();
size=sizeof(Book);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 2: p=new CD;
p->getData();
tag=p->getType();
size=sizeof(CD);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 3: p=new VTape;
p->getData();
tag=p->getType();
size=sizeof(VTape);
addObject(p, file, fileName, tag, size);
delete p;
break;
case 4: cout<<"\nL I S T O F B O O K S\n";
tag = 'b';
display(file, fileName, tag);
break;
case 5: cout<<"\nL I S T O F C D\n";
tag = 'c';
display(file, fileName, tag);
break;
case 6: cout<<"\nL I S T O F V I D E O T A P E S\n";
tag = 't';
display(file, fileName, tag);
break;
case 7: cout<<"\nL I S T O F A L L I T E M S\n";
display(file, fileName);
break;
case 8: cout<<"Enter Item id :"; cin>>id;
tag = 'b';
search(file, fileName, id, tag);
break;
case 9: cout<<"Enter Item id :"; cin>>id;
tag = 'c';
search(file, fileName, id, tag);
break;
case 10:cout<<"Enter Item id :"; cin>>id;
tag = 't';
search(file, fileName, id, tag);
break;
case 11:cout<<"Enter Item id :"; cin>>id;
cout<<"\nEnter b..Books, c..CDs, t..VTapes: ";
cin>>tag;
search(file, fileName, id, tag);
}
choice=menu();
}
}