Program TPEstructuras;
Uses
    CRT, LSL;
Type
    tclave= string [14];
    tclavecita= integer;
    tfecha= string [10];
    RegPas= Record
          clave: tclavecita;
          fecha: tfecha;
          cod_op: char;
    end;
    RegV= Record
          clave: tclave;
          vuelo: string [6];
          fecha: tfecha;
          cap: integer;
          dest: string [30];
          pas: tlista of regPas;
    end;
    RegP= Record
          fechaP: tfecha;
          vueloP: string [6];
          Dnip: integer;
          Cod_op: char;
    end;
 
Var
   rta: char;
   factual: string;
   LV: tlista of regV;
   LP: tlista of regPas;
   ArchV: file of regV;
   ArchP: file of regP
   Error: boolean;
 
Procedure FechaActual(Var factual: string);
Begin
writeln ('Ingrese la fecha de hoy (dd/mm/aaaa)');
readln (factual);
end;
 
Procedure CListas(Var Error: boolean, Var LV: tlista of regV)
Var
   d: regV;
   h: regPas;
   xv: regV;
   xp: regP;
   exito: boolean;
 
Begin
open(ArchV)
read(ArchV, xv);
Lcrear(lv);
While not eof(ArchV) and not Lllena(LV) do
Begin
      d.clave:= xv.fecha+xv.vuelo
      Lbuscarordenado(LV, d.clave, exito)
      If exito then
         writeln ('Vuelo repetido, error en la carga');
      else
          d.vuelo:= xv.vuelo;
          d.fecha:= xv.fecha;
          d.cap:= xv.capacidad;
          d.dest:= xv.destino;
          lcrear(d.pas)
          Linsertarorden(lv, d, 'A');
      end;
      read(ArchP, xv);
end;
If not eof(ArchV) then
   writeln ('Error de memoria')
   Error:= true;
else
    open(ArchP);
    read(ArchP, xp);
    while not eof(ArchP) and not Lllena(lv.pas) do
    Begin
          d.clave:= xp.fecha+xp.vuelo;
          Lbuscarordenado(lv, d.clave, exito);
          if not exito then
             writeln ('Error en datos, vuelo inexistente');
          else
              if (xp.cod_op='V') or (xp.cod_op='R') and (fdifdias(factual, xp.fecha))>2) then
                 linfo(lv, d);
                 h.clave:= xp.dnip;
                 h.fecha:= xp.fechap;
                 h.cod_op= xp.cod_op;
                 linsertarordenado(d.pas, h, 'A');
                 lmodificar(lv, d);
              else
                  linfo(lv, d);
                  d.cap:= d.cap+1;
                  lmodificar(lv, d);
              end;
          end;
          read(ArchP, xp);
          end;
          if not eof(ArchP) then
             writeln ('Error de memoria');
             Error:= true;
          end;
          close(ArchP);
          close(ArchP);
    end;
 
Procedure Reserva(Var lv: tlista of regV, factual: tfecha);
Var
   vuelo: string
   fecha: tfecha;
   dni: integer;
   d: regV;
   e: regPas;
 
Begin
writeln ('Ingrese el vuelo');
readln (vuelo);
writeln ('Ingrese fecha de vuelo');
readln (fecha);
convertirtfecha(fecha, tfecha);
if ((fdifdias(factual, fecha)>15) or (fdifdias(factual, fecha)<3)) then
   writeln ('No puede reservar con menos de 15 dias de anticipaci¢n, ni a menos de 3 dias del vuelo');
else
    writeln ('Ingrese DNI del pasajero');
    readln (dni);
    d.clave:= vuelo+fecha;
    lbuscarordenado(lv, d.clave, existe);
                         if existe then
                            linfo(lv, d);
                            if lv.capacidad>0 then
                               lbuscar_ordenado(lv, dni, existe);
                               if not existe then
                                  lv.capacidad:= lv.capacidad-1;
                                  e.clave:= dni;
                                  e.fecha:= fecha;
                                  e.cod_op; r;
                                  lmodificar(lv, e);
                               else
                                   writeln ('El pasajero ya ha reservado pasaje en este vuelo');
                               end;
                            else;
                                 writeln ('No hay mas lugar en el vuelo solicitado');
                            end;
                            else
                                writeln ('Codigo de vuelo o fecha incorrecta');
                            end;
end;
end;
 
Procedure venta(lp: tlista de regV, factual: tfecha);
Var
   d:regV;
   e: regP;
 
Begin
writeln ('Ingrese el vuelo');
readln (vuelo);
writeln ('Ingrese fecha de vuelo');
readln (fecha);
convertirtfecha(fecha, tfecha);
if (fecha<factual) then
   writeln ('La fecha de vuelo es anterior a la fecha actual');
else
    writeln ('Ingrese dni del pasajero');
    readln (dni);
    d.clave:= vuelo + fecha;
    lbuscarodenado(lv, d.clave, existe);
    if existe then
       if lv.capacidad>0 then
          l.info(lv, d);
          lbuscarordenado(lv, dni, existe);
          if not existe then
             lv.capacidad:= lv.capacidad + 1
             e.clave:= dni;
             e.fecha:= fecha;
             e.cod_op:= v;
             lmodificar(lv, e);
          else
              writeln ('El pasajero ya tiene un ticket a su nombre en este vuelo');
          end;
       else
           writeln ('No hay mas lugar en este vuelo');
       end;
    else
        writeln ('Codigo de vuelo o fecha incorrecto');
    end;
end;
end;
 
Procedure alta(lv: tlista of regV, factual: tfecha);
Var
   cod_v; string;
   fecha_v: string;
   dest: string;
   ok: boolean;
   x: regV;
   cap: integer;
 
Begin
writeln('Ingrese codigo de vuelo');
readln(cod_v);
wrtieln('Ingrese fecha del vuelo');
fconvertirtfecha(fecha_v, tfechavalida, ok);
if ok then
   if (fecha_v<factual) then
      wrtieln ('La fecha de vuelo es invalida');
   else
       x.clave:= cod_v+fecha_v;
       x.vuelo:=cod_v;
       x.fecha:=fecha_v;
       repeat
             wrtieln ('Ingrese capacidad');
             readln(cap);
       until (cap>0);
       x.cap:= cap;
       writeln ('Ingrese destino');
       readln(dest);
       x.destino:= dest;
       lcrear(lp);
       linsertarordenado(lv, x, 'A');
   end;
else
    wrtieln ('Fecha invalida');
end;
end;
 
Procedure Baja(lv: tlista of regV);
Var
   vuelo: string;
   fvuelo: string;
   fecha: tfecha;
   clave: tclave;
   error: boolean;
   existe: boolean;
 
Begin
writeln('Ingrese numero de vuelo');
readln (vuelo);
Repeat
      writeln ('Ingrese fecha de vuelo');
      readln (fvuelo);
      Fconvertirflegibletfecha(fvuelo, fecha, error);
until ok;
clave:= vuelo+fecha;
Lbuscarorden(lv, clave, existe);
if existe then
   Lborraractual(lv)
else
    writeln ('Numero de vuelo o fecha incorrectos');
end;
 
Procedure ListaVuelo (Var lv: tlista of regV);
Var
   auxvuelos: regV, fechaL: string, ok: boolean;
 
Begin
if not Lvacia(lv) then
   Lleerppio(lv);
   Linfo(lv, auxvuelos);
   writeln ('Vuelo', auxvuelos.vuelo,'Fecha' fconvertirfechalegible(auxvuelos.fecha, fechaL, ok);
   wrtieln ('Capacidad', auxvuelos.cap, 'Destino', auxvuelos.destino);
Lsig(lv, auxvuelo);
while not Lfin(lv) do
      Begin
      writeln ('Vuelo', auxvuelos.vuelo,'Fecha' fconvertirfechalegible(auxvuelos.fecha, fechaL, ok);
   wrtieln ('Capacidad', auxvuelos.cap, 'Destino', auxvuelos.destino);
Lsig(lv, auxvuelo);
end;
end;
end;
 
Procedure ListaPasajeros(Var lv: tlista of regV);
Var
   vuelo: string;
   fvuelo: string;
   fecha: tfecha;
   ok: boolean;
   clave: tclave;
   auxvuelos: regV;
   auxpas: regPas;
   exito: boolean;
 
Begin
wrtieln ('Ingrese el vuelo que desea listar');
readln (vuelo);
Repeat
      writeln ('Ingrese fecha del vuelo');
      readln (fvuelo);
      FconvertirFlegibleatfecha(fvuelo, fecha, ok);
until ok;
clave:= vuelo+fecha;
Lbuscarorden(lv, clave, exito);
if exito then
   Linfo(lv, auxvuelos);
   if not Lvacia(auxvuelos.pas) then
      Lleerppio(auxvuelos.pas);
      Linfo(auxvuelos.pas, auxpas);
      wrtieln ('Dni del pasajero', auxvuelos.clave);
      wrtieln ('Codigo de operacion', auxvuelos.cod_op);
      Lsig(auxvuelos.pas, auxpas);
      while not Lfin(auxvuelos.pas) do
      Begin
           Linfo(auxvuelos.pas, auxpas);
           wrtieln ('Dni del pasajero', auxvuelos.clave);
           wrtieln ('Codigo de operacion', auxvuelos.cod_op);
           Lsig(auxvuelos.pas, auxpas);
      end;
   end;
else
    writeln ('Nro de vuelo o fecha invalida');
end;
end;
 
Begin
clrscr;
FechaActual(factual);
Clistas(Error, LV);
clrscr;
Repeat
      writeln ('1. Reserva');
      writeln ('2. Pago');
      writeln ('3. Alta');
      writeln ('4. Baja');
      writeln ('5. Lista de pasajeros');
      writeln ('6. Lista de vuelos');
      writeln ('0. Salir');
      readln (rta);
Until (rta='0') or (rta='1') or (rta='2') or (rta='3') or (rta='4') or (rta='5') or (rta='6');
while (rta<>'0') do
Begin
      case rta of
           '1': Reserva(lv, factual);
           '2': Venta(lp, factual);
           '3': Alta(lv, factual);
           '4': Baja(lv);
           '5': ListaVuelo(lv);
           '6': ListaPasajeros(lv);
      end;
      readkey;
      if (rta<>'0') then
      Begin
         clrscr;
         Repeat
               writeln ('1. Reserva');
               writeln ('2. Pago');
               writeln ('3. Alta');
               writeln ('4. Baja');
               writeln ('5. Lista de pasajeros');
               writeln ('6. Lista de vuelos');
               writeln ('0. Salir');
               readln (rta);
         Until (rta='0') or (rta='1') or (rta='2') or (rta='3') or (rta='4') or (rta='5') or (rta='6');
      end;
end;
readkey;
end.