Guest User

Mansi

a guest
Apr 8th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 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.     }
  139.     if(found=='n'){
  140.         cout<<"MEDICINE NOT FOUND!"<<endl;
  141.     }
  142.     f.close();
  143. }
  144.    
  145. void insertnew(){
  146.     ofstream fout;
  147.     fout.open("medi.txt",ios::app|ios::out|ios::binary);
  148.     m.getdata();
  149.     fout.write((char*)&m,sizeof(m));
  150.     fout.close();
  151. }
  152.  
  153. void modify(){
  154.     int mid;
  155.     long pos;
  156.     char found='n';
  157.     fstream fio("medi.txt",ios::in|ios::out);
  158.     cout<<"ENTER MEDICINE ID WHOSE RECORD IS TO MODIFIED:\n";
  159.     cin>>mid;
  160.     while(fio.read((char*)&m,sizeof(m))){
  161.        
  162.         if(m.med()==mid){
  163.             cout<<"OLD RECORD OF ID "<<mid<<endl;
  164.             cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  165.             m.showdata();
  166.             m.getmodifieddata();
  167.             // fio.seekg(sizeof(m),ios::cur);
  168.             fio.write((char*)&m,sizeof(m));
  169.             found='y';
  170.             break;
  171.         }
  172.     }
  173.     if(found=='n'){
  174.         cout<<"MEDICINE WITH ID "<<mid<<" NOT FOUND!!\n";
  175.     }
  176.     fio.seekg(0);
  177.     fio.close();
  178. }
  179.  
  180. void delrecord(){
  181.     ifstream pf("medi.txt",ios::in);
  182.     ofstream nf("temp.txt",ios::out|ios::app);
  183.     ofstream rf("rcb.txt",ios::out|ios::app);
  184.     medicine m2;
  185.     int mdid;
  186.     char found='t', confirm='y';
  187.     cout<<"\nENTER MEDICINE ID TO DELETE ITS RECORD\n";
  188.     cin>>mdid;
  189.     cin.ignore();
  190.     while(pf.read((char*)&m2,sizeof(m2))){
  191.        
  192.         if(m2.med()==mdid){
  193.             cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  194.             m2.showdata();
  195.             found='f';
  196.             cout<<"\nARE YOU SURE YOU WANT TO DELETE THIS RECORD?(y/n)\n\t";
  197.             cin>>confirm;
  198.             // int pos=pf.tellg();
  199.             // pf.seekg(pos);
  200.             rf.write((char*)&m,sizeof(m));
  201.             rf.close();
  202.             if(confirm=='n'){
  203.                 nf.write((char*)&m2,sizeof(m2));
  204.             }
  205.         }else{
  206.             nf.write((char*)&m2,sizeof(m2));
  207.         }
  208.     }
  209.     if(found=='t'){
  210.         cout<<"MEDICINE WITH ID "<<mdid<<" NOT FOUND!!\n\n";
  211.     }
  212.     pf.close();
  213.     nf.close();
  214.     remove("medi.txt");
  215.     rename("temp.txt","medi.txt");
  216. }
  217.  
  218. void recyclebin(){
  219.     ifstream rf("rcb.txt",ios::in);
  220.     cout<<"\t\t\tDELETED RECORDS\n\n";
  221.     cout<<"\t"<<"ID\t\tNAME\t\tPRICE\t\t"<<endl<<endl;
  222.     while(rf.read((char*)&m,sizeof(m))){
  223.         m.showdata();
  224.         cout<<"\n";
  225.     }
  226.     rf.close();
  227. }
  228.  
  229. int menu(char yes='y'){
  230.         //medicine m;
  231.     switch(m.adm()){
  232.         case 1: delrecord();
  233.         break;
  234.         case 2: insertnew();
  235.         break;
  236.         case 3: modify();
  237.         break;
  238.         case 4: search();
  239.         break;
  240.         case 5: mainref();
  241.         break;
  242.         case 6: recyclebin();
  243.         break;
  244.         default: cout<<"NO MATCH FOUND!\n";
  245.         break;
  246.     }
  247.     cout<<"GO TO MENU?(y/n)\n";
  248.     cin>>yes;
  249.     if(yes=='y'){
  250.         system("cls");
  251.         menu('y');
  252.     }
  253. }
  254.  
  255. int main(){
  256.     if(login()!=1){
  257.         menu();
  258.     }  
  259.     return 0;
  260. }
Add Comment
Please, Sign In to add comment