summaryrefslogtreecommitdiffstats
path: root/2022-2/L8/mitsuo/LAB08_PREG01/Farmacia.cpp
blob: 49c01f32440efeef7fe34523d23570ef3f1fc60e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 
 * File:   Farmacia.cpp
 * Author: mitsuo
 * 
 * Created on 27 November 2022, 11:43
 */

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include "Farmacia.h"
#include "aux.h"

using namespace std;

Farmacia::Farmacia() {
}

Farmacia::Farmacia(const Farmacia& orig) {
}

Farmacia::~Farmacia() {
}


void Farmacia::cargaMedico() {
    ifstream file("../../medicos.csv");
    if (!file) {
        cerr << "Error: can't open file\n";
        return;
    }
    
    int i;
    for (i = 0; this->lmedico[i].read(file); i++)
        ;
}

void Farmacia::leeReceta() {
    ifstream file("../../recetas.csv");
    if (!file) {
        cerr << "Error: can't open file\n";
        return;
    }
    
    int i_sin = 0;
    int i_con = 0;
    char s[MAXLEN];
    char c;
    // paciente
    int pacienteDni;
    char pacienteNombre[MAXLEN];
    // medico
    int medicoId;
    char medicoEsp[MAXLEN];  // to search in lmedico
    // medicamento
    int medicamentoId;
    int medicamentoQty;
    char medicamentoName[MAXLEN];
    while (true) {
        /* needs to "peek" line to determine if medicamento is conreceta or 
         * sinreceta */
        file.getline(s, MAXLEN, ','); // ignore date
        if (file.eof()) break;
        file >> pacienteDni >> c;
        file.getline(pacienteNombre, MAXLEN, ',');
        file >> medicoId;
        this->searchMedico(medicoId, medicoEsp);
        while (file.get() != '\n') {
            /* read medicamentos until '\n' */
            file >> medicamentoId >> c;
            file >> medicamentoQty;
            this->searchMedicamento(medicamentoId, medicamentoName);
            /* (medicamentoId is always 5 digits long */
            if (medicamentoId / 10000 % 2 == 0) {
                /* con receta */
                this->lconreceta[i_con].SetCodigo(medicamentoId);
                this->lconreceta[i_con].SetNombre(medicamentoName);
                this->lconreceta[i_con].SetCantidad(medicamentoQty);
                this->lconreceta[i_con].SetCodmed(medicoId);
                this->lconreceta[i_con].SetEspecialidad(medicoEsp);
                i_con++;
            } else {
                /* sin receta */
                this->lsinreceta[i_sin].SetCodigo(medicamentoId);
                this->lsinreceta[i_sin].SetNombre(medicamentoName);
                this->lsinreceta[i_sin].SetCantidad(medicamentoQty);
                this->lsinreceta[i_sin].SetDni(pacienteDni);
                this->lsinreceta[i_sin].SetPacienteNombre(pacienteNombre);
                i_sin++;
            }
        }
    }
}

void Farmacia::imprimirReceta() {
    ofstream file("reporte.txt");
    if (!file) {
        cerr << "Error: can't open file\n";
        return;
    }
    file << 
"Codigo         Descripcion del Med.                    Cant.   DNI           Nombre\n"
"============================================================================================\n";
    for (int i = 0; this->lsinreceta[i].GetCodigo() != -1; i++) {
        this->lsinreceta[i].write(file);
    }
    
    file << "\n\n\n";
    file << 
"Codigo         Descripcion del Med.                    Cant.   Cod. Medico   Especialidad\n"
"============================================================================================\n";
    for (int i = 0; this->lconreceta[i].GetCodigo() != -1; i++) {
        this->lconreceta[i].write(file);
    }
}

/* return name through `name_out` parameter */
void Farmacia::searchMedicamento(int idTarget, char *name_out) {
    ifstream file("../../medicamentos.csv");
    if (!file) {
        cerr << "Error: can't open file\n";
        return;
    }
    
    char c;
    char s[MAXLEN];
    char name[MAXLEN];
    int id;
    while (file >> id) {
        file >> c;
        file.getline(name, MAXLEN, ',');
        if (id == idTarget) {
            strcpy(name_out, name);
            return;
        }
        file.getline(s, MAXLEN);
    }
    strcpy(name_out, "NO ENCONTRADO");
}


void Farmacia::searchMedico(int idTarget, char *name_out) {
    int i;
    for (i = 0; this->lmedico[i].getCodigo() != -1; i++) {
        if (lmedico[i].getCodigo() == idTarget) {
            strcpy(name_out, lmedico[i].getEspecialidad());
            return;
        }
    }
    strcpy(name_out, "NO ENCONTRADO");
}