Guest User

mayank

a guest
Apr 8th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include<fstream>
  2. #include<iostream>
  3. #include<conio.h>      
  4. #include<windows.h>
  5. #include<string.h>
  6. //#include<bits/stdc++.h>
  7. using namespace std;
  8. class medicine{                                
  9.     char name[40];
  10.     int med_id;
  11.     float price;
  12.     int choice;
  13.     public:
  14.         void getdata(){
  15.             cout<<"\nENTER MEDICINE ID: "; cin>>med_id;
  16.             cout<<"\nENTER MEDICINE NAME: "; cin>>name;
  17.             cout<<"\nENTER PRICE: "; cin>>price;
  18.         }
  19.         void showdata(){
  20.             cout<<"\t"<<med_id<<"\t|\t";
  21.             cout<<name<<"\t|\t";
  22.             cout<<price<<"\t\t";
  23.         }
  24.         void getmodifieddata(){
  25.             char nm[40];
  26.             int m_id;
  27.             float prc;
  28.             cout<<"\nENTER NEW DETAILS: "<<endl;
  29.             cout<<"NEW ID:(enter '1' to retain old one)\n";
  30.             cin>>m_id;
  31.             cout<<"NEW NAME:(enter '.' to retain old one)\n";
  32.             cin>>nm;
  33.             cout<<"NEW PRC:(enter '1' to retain old one)\n";
  34.             cin>>prc;
  35.             if(strcmp(nm,".")!=0){
  36.                 strcpy(name,nm);
  37.             }
  38.             else{
  39.                 strcpy(nm,name);
  40.             }
  41.             if(m_id==1){
  42.                 m_id=med_id;
  43.             }
  44.             else{
  45.                 med_id=m_id;
  46.             }
  47.             if(prc==1){
  48.                 prc=price;
  49.             }
  50.             else{
  51.                 price=prc;
  52.             }
  53.         }
  54.         int med(){
  55.             return med_id;
  56.         }
  57.         int adm(){
  58.             cout<<"1.PRESS 1 TO DELETE RECORD.\n2.PRESS 2 TO INSERT NEW RECORD.\n3.PRESS 3 TO MODIFY OLD RECORD.\n4.PRESS 4 TO SEARCH RECORD.\n5.PRESS 5 TO DISPLAY MEDICINES RECORD.\n6.PRESS 6 TO GO TO RECYCLE BIN.\n\n";
  59.             cout<<"CHOICE: ";
  60.             cin>>choice;
  61.             return choice;
  62.         }
  63. }m;
  64. int login(){
  65.     int a=0,i=0,rt=1;
  66.     char user[10],ch=' ',pwd[8],code[10];
  67.     char uname[10]="admin";
  68.     char psw[8]="clowl78";
  69.     do{
  70.     cout<<"\nENTER USERNAME: ";
  71.     cin>>user;
  72.     cout<<"\nENTER PASSWORD: ";
  73.     while(i<8){
  74.         pwd[i]=getch();
  75.         ch=pwd[i];
  76.         if(ch==13){break;
  77.         }
  78.         else{
  79.             cout<<"*";
  80.             i++;
  81.         }
  82.     }
  83.     pwd[i]='\0';
  84.     i=0;
  85.     if(strcmp(user,uname)==0&&strcmp(pwd,psw)==0){
  86.         cout<<"\n\t\tLOGIN SUCCESSFULL. \n";
  87.         cout<<"\nPRESS ANY KEY TO CONTINUE.";
  88.         getch();
  89.         break;
  90.     }
  91.     else{
  92.         cout<<"\n\tOOPS! SOMETHING WENT WRONG, TRY AGAIN. PRESS ANY KEY TO CONTINUE.\n";
  93.         a++;
  94.         getch();
  95.     }
  96.     }
  97.     while(a<=2);
  98.     if(a>2){
  99.         cout<<"\n\nYOU HAVE ENTERED WRONG USERNAME OR PASSWORD MORE THAN TWO TIMES!\nPRESS ANY KEY TO EXIT.";
  100.         getch();
  101.         return rt;
  102.     }
  103.     system("cls");
  104. }
  105. void mainref(){
  106.     ofstream fout;
  107.     fout.open("medi.txt",ios::out|ios::app);
  108.     fout.close();
  109.     ifstream fin;
  110.     fin.open("medi.txt",ios::in);
  111.     cout<<"\t\t\t MEDICINE RECORD"<<endl<<endl;
  112.     cout<<"\t\t\t-----------------"<<endl<<endl;
  113.     cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  114.     while(fin.read((char*)&m,sizeof(m))){
  115.         m.showdata();
  116.         cout<<endl;
  117.     }
  118.     fin.close();
  119.    
  120. }
  121. void search(){
  122.     int ser;
  123.     char found='n';
  124.     cout<<"\n\nENTER MEDICINE ID TO BE SEARCHED FOR:"<<endl<<endl;
  125.     cin>>ser;
  126.     ifstream f;
  127.     f.open("medi.txt",ios::binary|ios::in);
  128.     f.seekg(0);
  129.     while(f.read((char*)&m,sizeof(m))){
  130.     if(ser==m.med()){
  131.         cout<<"\n\t DETAILS OF MEDICINE WITH ID "<<ser<<":"<<endl;
  132.         cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  133.         m.showdata();
  134.         cout<<endl<<endl<<endl;
  135.         found='y';
  136.         exit(1);
  137.     }   }
  138.     if(found=='n'){
  139.         cout<<"MEDICINE NOT FOUND!"<<endl;
  140.     }
  141.     f.close();
  142.     }
  143.    
  144. void insertnew(){
  145.     ofstream fout;
  146.     fout.open("medi.txt",ios::app|ios::out|ios::binary);
  147.     m.getdata();
  148.     fout.write((char*)&m,sizeof(m));
  149.     fout.close();
  150.     }
  151. void modify(){
  152.     int mid;
  153.     long pos;
  154.     char found='n';
  155.     fstream fio("medi.txt",ios::in|ios::out|ios::binary|ios::app);
  156.     cout<<"ENTER MEDICINE ID WHOSE RECORD IS TO MODIFIED:\n";
  157.     cin>>mid;
  158.     while(!fio.eof()){
  159.         pos=fio.tellg();
  160.         fio.read((char*)&m,sizeof(m));
  161.         if(m.med()==mid){
  162.             cout<<"OLD RECORD OF ID "<<mid<<endl;
  163.             cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  164.             m.showdata();
  165.             m.getmodifieddata();
  166.             fio.seekg(pos);
  167.             fio.write((char*)&m,sizeof(m));
  168.             found='y';
  169.             break;
  170.         }
  171.     }
  172.     if(found=='n'){
  173.         cout<<"MEDICINE WITH ID "<<mid<<" NOT FOUND!!\n";
  174.     }
  175.     fio.seekg(0);
  176.     fio.close();
  177. }
  178. void delrecord(){
  179.     ifstream pf("medi.txt",ios::in);
  180.     ofstream nf("temp.txt",ios::out|ios::app);
  181.     ofstream rf("rcb.txt",ios::out|ios::app);
  182.     medicine m2;
  183.     int mdid;
  184.     char found='t', confirm='y';
  185.     cout<<"\nENTER MEDICINE ID TO DELETE ITS RECORD\n";
  186.     cin>>mdid;
  187.     cin.ignore();
  188.     while(!pf.eof()){
  189.         pf.read((char*)&m2,sizeof(m2));
  190.         if(m2.med()==mdid){
  191.             cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  192.             m2.showdata();
  193.             found='f';
  194.             cout<<"\nARE YOU SURE YOU WANT TO DELETE THIS RECORD?(y/n)\n\t";
  195.             cin>>confirm;
  196.             int pos=pf.tellg();
  197.             pf.seekg(pos);
  198.             rf.write((char*)&m,sizeof(m));
  199.             rf.close();
  200.             if(confirm=='n'){
  201.                 nf.write((char*)&m2,sizeof(m2));
  202.             }
  203.         }
  204.             else{
  205.                 nf.write((char*)&m2,sizeof(m2));
  206.             }
  207.     }
  208.     if(found=='t'){
  209.         cout<<"MEDICINE WITH ID "<<mdid<<" NOT FOUND!!\n\n";
  210.     }
  211.     pf.close();
  212.     nf.close();
  213.     remove("medi.txt");
  214.     rename("temp.txt","medi.txt");
  215.     }
  216. void recyclebin(){
  217.     ifstream rf("rcb.txt",ios::in);
  218.     cout<<"\t\t\tDELETED RECORDS\n\n";
  219.     cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  220.     while(!rf.eof()){
  221.         rf.read((char*)&m,sizeof(m));
  222.         m.showdata();
  223.         cout<<"\n";
  224.     }
  225.     rf.close();
  226.     }
  227. int menu(char yes='y'){
  228.         //medicine m;
  229.     switch(m.adm()){
  230.         case 1: delrecord();
  231.         break;
  232.         case 2: insertnew();
  233.         break;
  234.         case 3: modify();
  235.         break;
  236.         case 4: search();
  237.         break;
  238.         case 5: mainref();
  239.         break;
  240.         case 6: recyclebin();
  241.         break;
  242.         default: cout<<"NO MATCH FOUND!\n";
  243.         break;
  244.     }
  245.     cout<<"GO TO MENU?(y/n)\n";
  246.     cin>>yes;
  247.     if(yes=='y'){
  248.         system("cls");
  249.         menu('y');
  250.     }
  251.     }
  252. int main(){
  253.     if(login()!=1){
  254.     menu();
  255.     }  
  256.    
  257.     return 0;
  258. }
Add Comment
Please, Sign In to add comment