Sei sulla pagina 1di 18

#include<conio.h> #include<fstream.h> #include<stdio.h> #include<string.h> #include<process.h> #include<dos.

h>

class item { int pid; char productname[40];

protected: float price; int quantity;

public: int retprodid(); char*retproductname(); float retprice(); void getit(int); void putit(); void add(); void disp();

int check (int quant); void show() { cout << pid << "\t"<< productname << "\t"<< price;} void searchprodname(char*); void modify(); void delete1(); void select(); void updatequantity(int quant)

{ quantity=quantity-quant; } }ob;

class procitem: protected item { int prquan; float procprice;

void caldynprice() { procprice=prquan*price; }

public: float retprocprice() { return procprice; }

void getproc();

void disproc() { show(); cout << "\t" << prquan; cout << "\t" << procprice; } };

const int size=20; class bill { procitem data[size]; int top; float total;

public: bill() { top=-1; }

void buy(); void cancel(); void display(); };

void procitem::getproc() { select();

int flag, loop=0; do{ cout << "\n Enter quantity: "; cin >> prquan;

flag=check(prquan);

if(flag==1) // available quantitiy loop=1;

else {

cout<<"\nERROR: Quantity of product in store is not enough. Please enter a lesser value : \n";

} while(loop==0);

caldynprice();

void bill::buy() { if(top==size-1) cout << "ERROR: OVERFLOW\n"; else { top++; data[top].getproc();

getch(); } } void bill::cancel() { if (top==-1) cout << "Underflow\n";

else { top--; cout << "CANCELLED\n"; }}

void bill::display() { int i=0; total=0;

if(top==-1) cout << "NOT BOUGHT ANYTHING\n";

else { cout << "Sno.\tID\tName\tPrice\tQty\tProc\n"; for(i=0;i<=top;i++) { cout << (i+1) << "\t"; data[i].disproc(); cout << "\n"; total+=data[i].retprocprice(); } }

cout << "\n Total price: " << total<< "\n"; }

int item::check ( int quant) // Check the avialibility of quantity { fstream item_file; int ret=0; long pos; item_file.open("itemfile.dat",ios::in);

item_file.seekg(0);

while(!item_file.eof()) { pos=item_file.tellg(); item_file.read((char*)&ob,sizeof(ob));

if(pid==ob.pid&&quantity>=quant) ret=1; ob.updatequantity(quant); item_file.seekg(pos); item_file.write((char*)&ob,sizeof(ob)); } item_file.close();

return ret; } void item::select() { int val, flag=0; fstream f2; do{ cout << "\n Enter product ID: "; cin>>val;

f2.open("item1.dat",ios::in|ios::binary); while(f2.read((char*)&ob,sizeof(ob))) { if(val==ob.pid) { pid=ob.pid; strcpy(productname,ob.productname); price=ob.price; flag=1; break; } if(flag==0) { cout<<"ERROR: INVALID ID. Please enter the correct code agian : "; } } }while(flag==0); f2.close(); }

int item::retprodid() { return pid; }

float item::retprice() { return price; }

char*item::retproductname() { return productname; }

void item::getit(int n) {

pid=n; cout<<"\n\t\tEnter name:\t"; gets(productname); cout<<"\n\t\tEnter price:\t"; cin>>price; } void item::putit() { cout<<"\n\n\t\tProduct ID:\t"<<pid; cout<<"\n\n\t\tProduct Name:\t"<<productname; cout<<"\n\n\t\tPrice of the product:\t"<<price; }

void item::add() { fstream f;

f.open("item1.dat",ios::app|ios::binary);

int nu,loop=0,flag=0; cout<<"\n\n Product code : "; cin>>nu; cout<<endl;

while(loop==0) // to get an appropriate code. ( code entered by the user should not be code of //someother pre-existing product) { ifstream fi("item1.dat",ios::binary|ios::in); while(!fi.eof()) { fi.read((char*)&ob,sizeof(ob)); if(ob.pid==nu) { flag=1; break; } } if(flag==1) { cout<<"\n\n ERROR!!: The entered code is already present\n\n"; cout<<"\n\n Please try again with a different id:\n"; cout<<"\n\t\tEnter product id:\t"; cin>>nu; flag=0; } else loop=1; // loop will end because an appropriate code has been choosen } ob.getit(nu); f.write((char*)&ob,sizeof(ob)); f.close(); }

void item::disp() { fstream f1; f1.open("item1.dat",ios::binary|ios::in); while(f1.read((char*)&ob,sizeof(item))) ob.putit(); f1.close(); } void item::searchprodname(char*val) { fstream f2; int flag=0;

f2.open("item1.dat",ios::in|ios::binary); while(f2.read((char*)&ob,sizeof(ob))) { if(!strcmpi(val,ob.retproductname())) { cout << "\n\t\tDisplaying data\n"; ob.putit(); flag=1; break; } }

f2.close(); if(!flag) cout << "\n\n\t\tRecord not found !!!\n"; } void item::modify() { int c; char found='f'; cout<<"\nEnter the item code : "; cin>>c; fstream fio;

fio.open("item1.dat",ios::app|ios::in|ios::out|ios::binary);

long pos; while(!fio.eof()) {

pos=fio.tellg(); fio.read((char*)&ob, sizeof(ob)); if(ob.pid==c) { cout<<"Product id:"<<pid<<endl; cout<<"Product name :"<<productname<<endl; cout<<"Price:"<<price<<endl; cout<<"Quantity:"<<quantity<<endl; char ps[40]=" ";

cout<<"\n\n\t ENTER DETAILS TO BE MODIFIED \n";

cout<<"\nProduct name : (Enter '.' to retain old name)"; cin>>ps;

if(strcmp(ps,".")!=0) strcpy(productname,ps);

cout<<"\nEnter price : " ; cin>>price; cout<<"\nEnter quantity : ";cin>>quantity;

fio.seekg(pos); fio.write((char*)&ob,sizeof(ob));

found='t'; break;

} if(found=='f') { cout<<"\n\n\t\t\t ERROR: Record not found"<<endl; fio.seekg(0); } }

void item::delete1() {

ifstream fio("item1.dat",ios::binary|ios::in); ofstream item1("temp.dat",ios::out);

int pc; char found = 'f', confirm='n'; cout<<" \nPlease enter the product code :\t\t "; cin>>pc; cout<<endl;

while(!fio.eof()) { fio.read((char*)&ob,sizeof(ob)); if(ob.pid==pc) {cout<<endl; ob.putit(); found='t'; cout<<"\n\n\n\t\t Are you sure you want to delete this record ? (y/n) : "; cin>>confirm ;

if(confirm=='n') item1.write((char*)&ob,sizeof(ob)); // when confirm is 'Y' the record is not written to temp item1.dat

else item1.write((char*)&ob,sizeof(ob)); }

if(found=='f') cout<<"\n\n\n ERROR : Record not found \n\n"; fio.close(); item1.close();

remove("item1.dat"); rename("temp.dat","item1.dat"); // old item1.dat removed and new item1.dat added } void process() { bill s; int ch; char ans;

do { clrscr(); cout << "BILL:\n1.Buy\n2.Cancel\n3.Display Bill\n4. Display items\nEnter choice: "; cin >> ch; switch(ch) { case 1: s.buy(); break;

case 2: s.cancel(); break;

case 3: s.display(); break;

case 4: ob.show(); break; }

cout << "Do you want to continue (y/n): "; cin >> ans; } while(ans=='y' || ans=='Y'); getch(); }

void main() { int ch,i1=0; char choice,ans,arr[45][70],answer; char name[20], mname[20], mpass[20], ucode[20], uid[20]; float mcode; int j,i;

do{clrscr();

Z:cout<<"\n\n\n\t ***************** Welcome to the Retail Store ****************\n"; delay(400); cout << "\t\t 1.Do once shopping\n\n"; delay(250); cout << "\t\t 2.Administrator's place\n\n"; delay(250); cout << "\t\t 3.Exit\n\n"; delay(250);

cout << "\t\t Enter your choice\t\t"; delay(250); cin >> ch;

switch(ch) { case 1:

process(); break;

case 2: cout << "\n\n\t\t Computer welcomes you\t\t"; cout << "\n\n\t\t Enter 6 digit code\t\t";

X: cin>>mcode; if(mcode!=250116) { if(i1==2) { cout << "\n\n\t\t Sorry to inform that the usercode entered is wrong"; cout << "\n\n\t\t Thank you. Come back later again"; getch(); exit(0); break; }

else { i1++; cout << "\n\n\t\t" << 3-i1 << "chances left"; cout << "\n\n\t\t Reenter code\t\t"; goto X; }

else { do {

clrscr(); cout<<"\n\n\n\n\n\n\t\t~ * Welcome * ~ \n\n"; cout<<"\t\tYou can make new innovations as per your convenience\n\n\n"; cout<<"\t\t 1.Add products to the store\n\n"; cout<<"\t\t 2.View items list\n\n"; cout<<"\t\t 3.Modify the details of product\n\n"; cout<<"\t\t 4.Remove items from the store\n\n"; cout<<"\t\t 5.Search a product\n\n"; cout<<"\t\t 6.Exit\n\n";

cout << "\t\t Enter your choice:\t\t"; cin>>ch;

switch(ch) { case 1: clrscr();

cout << "\n\n\t\t----**** Entering data ****---- \n"; do { ob.add(); cout<<"\n\n\t\tWant to enter more products:"; cin>>choice;

}while(choice=='y'||choice=='Y');

cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break;

case 2:

clrscr(); cout << "\n\n\t\t~~~~~~~~~ Dispalying data ~~~~~~~~\t"; ob.disp(); cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break;

case 3: clrscr(); do {

ob.modify();

cout << "\n\n\t\tWant to modify more products:\t"; cin>>choice;

}while(choice=='y'||choice=='Y'); cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break;

case 4: clrscr(); do {

ob.delete1();

cout << "\n\n\t\tWant to delete more products:\t"; cin>>choice;

}while(choice=='y'||choice=='Y');

cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break;

case 5: clrscr(); do { cout << "\n\n\t\tEnter the item to be searched:\t"; gets(name); ob.searchprodname(name);

cout << "\n\n\t\tWant to search more products:\t"; cin>>choice;

}while(choice=='y'||choice=='Y'); cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break;

case 6: cout << "\n\n\t\tDo you want to exit to main menu:\t"; cin>>ans;

if(ans=='y'||ans=='Y') goto Z;

else break;

default: cout << "Wrong choice entered";

getch(); cout<<"Do you want to go back to the administer's menu? "; cin>>ans; break; }

}while(ans=='y'||ans=='Y'); }

case 3: cout << "\n\t\t Thanks for the visit"; getch(); exit(0); }

cout<<"\nDo you want to go back to main menu(y/n): "; cin>>answer;

}while(answer=='y' || answer=='Y'); getch(); }

Potrebbero piacerti anche