// This is a .cpp file run this with a C++ code editor

#include <iostream>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <string.h>

////////         MADE BY- PRANAV BHILE
////////             2019B5PS0818G

using namespace std;

void screen();
void patient();
void doctor();


struct meds{
    char name[100];
    char id;
    int quantity;
    float cost;

public:

    void disp(){
        if (quantity >0 ){
            cout<<"\nName : "<<name;
            cout<<"\nCost : "<<cost;
        }
        else {
            cout<<"\n"<<name<<"is out of stock";
        }
    }
    void inp(){
        cout<<"\nEnter name of medicine ";
        scanf( "%s",name);
        cout<<"\nEnter cost ";
        cin>> cost;
        cout<<"\nEnter quantity available";
        cin>> quantity;
        cout<<"\nEnter your id ";
        cin>> id;
    }

}meds[100];

class doc {
    char pass[50];

public:
    char name[50];
    int experience;
    char address[1000];
    char spec[50];
    void input();
    int login();

}obj;

void doc::input(){
    ofstream file_obj;

    file_obj.open("doc.txt", ios::app);

    cout<<"Enter name ";
    scanf( "%s",obj.name);
    cout<<"Enter password ";
    scanf( "%s",obj.pass);
    cout<<"Enter years of experience ";
    cin >> obj.experience;
    cout<<"Enter address/ locality of clinic ";
    scanf( "%s",obj.address);
    cout<<"Enter specialization ";
    scanf( "%s",obj.spec);

    file_obj.write((char*)&obj, sizeof(obj));
    file_obj.close();

}

int doc::login()
{
    int id =0;
    ifstream file_obj;
    file_obj.open("doc.txt", ios::in);

    int flag =1;
    char nam[50];
    char pas[50];

    cout<<"Enter name\n";
    cin >> nam;
    cout<<"Enter pass\n";
    cin >> pas;

    while (!file_obj.eof() && flag ==1) {

        file_obj.read((char*)&obj, sizeof(obj));

        if ((!strcmp(nam,obj.name))&&(!strcmp(pas,obj.pass))){
            flag = 0;
        }

        id++;

    }
    if (flag==1){
        cout<<"Not found ";
        return -1;
    }
    if (flag==0){
        cout<<"found "<<id;
        return id;
    }
    file_obj.close();

}

void doctor(){

    char c ='y';

    int k = -2;
    int i;
    doc obj;
    while (c == 'y'){
        cout<<"a. Register\n";
        cout<<"b. Login\n";
        cin >> c;

        switch (c)
        {
            case 'a':obj.input();
                break;
            case 'b':k = obj.login();
                break;
        }
    }

    if (k >=0)
    {
        char f ='n';
        do{
            cout<<"\nDo you want to add medicine? (y/n)";
            cin >>f;
            if (f == 'y'){
                cout<<"\nEnter medicine id ";
                cin >>i;
                meds[i].inp();

            }
        }while(f != 'n');
    }
}

void disp(){
    cout<<"\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
    cout<<"\t\tMediServ\n\n";
    cout<<"\t\t\t\t\t\t\t       -By Pranav Bhile\n\n\n";

}

void screen(){
    int c = 0;
    while (c == 0){
        cout<<"Login as (Enter the number next to choice)\n";
        cout<<"1. Patient\n";
        cout<<"2. Doctor \n";
        cout<<"3. Chemist\n";
        cout<<"4. Quit\n";
        cin >>c;
        switch (c)
        {
            case 1: patient();
                break;
            case 2:
            case 3: doctor();
                break;
            case 4:
                break;
            default:cout<<"Invalid entry\n";
                c=0;

        }
        cout<<"\nPress 0 to do again";
        cin >> c;

        if (c!=0){
            cout <<"\n\n\n\n\n\t\t\t\t\tMADE BY PRANAV ARVIND BHILE";

        }
    }

}

void appoi(){

    int flag =1;
    char c[100];
    ifstream file_obj;
    file_obj.open("doc.txt");

    cout<<"What type of doctor do you need?";
    scanf("%s", c);

    while ((!file_obj.eof())&& flag == 1) {
        file_obj.read((char*)&obj, sizeof(obj));

        if (!strcmp(obj.spec,c))
        {
            flag = 0;
            cout<<"\nName of the doctor is"<<obj.name;
            cout<<"\nLocation of the clinic is"<<obj.address;
        }
    }
    if (flag==1){
        cout<<"No Doctor found with specifics";
    }

}

void buy(){

    int flag = 1;
    char na[100];
    cout<<"Enter the name of medicine that you need.";
    scanf("%s", na);
    int i;
    for (i =0;i<=100;i++){
        if (!strcmp(meds[i].name,na)){
            meds[i].disp();
            flag = 0;

        }
    }
    if (flag==1){
        cout<<"\nOut of stock";
    }
}

void patient(){
    char c1;
    int c2 =0;
    char address[1000];
    cout <<"Enter location\n";
    cin >> address;
    cout<<"\nIs this an emergency(y/n) ";
    cin>> c1;
    if (c1=='y') {
        cout<<"\nCalling for medical services";
        c2=1;
    }

    while(!c2)
    {
        cout<<"\n1.Do you want to make an appointment?";
        cout<<"\n2.Do you want to buy medicine?";
        cin >>c2;

        switch (c2){
            case 1: appoi();
                break;
            case 2: buy();
                break;
            default : cout<<"\nInvalid entry ";
                c2 =0;
                break;
        }
    }
}


int main()
{
    disp();
    screen();
    return 0;
}