Pascal/Turbo Pascal - Manejo de ID con archivos

 
Vista:

Manejo de ID con archivos

Publicado por Ignacio (1 intervención) el 20/07/2017 16:10:51
Hola gente, mi primera vez en este foro y vi que responden muy bien las consultas. Estoy aprendiendo algo de Pascal para la uni y tengo que hacer un ABM de productos, y en la parte de Carga de productos quiero que el ID del producto se genere automaticamente sin pedirselo al usuario, pero no se como hacer que busque el ultimo producto cargado y use el id+1. Tengo hecho esto ( estuve probando cosas en PantallaCarga para ver si funcionaba pero nada):

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
program Practica;
uses crt;
type TRegistro = record
        idProducto:integer;
        nombreProducto:string;
        precioProducto:real;
        activoProducto:boolean;
        end;
var teclaInicio:char;
Productos:TRegistro;
AProductos: file of Tregistro;
 
procedure PantallaInicio;
begin
        ClrScr;
        writeln('       ADMINISTRADOR DE PRODUCTOS ');
        writeln('Ingrese la opcion que desea realizar');
        writeln('1 - CARGA PRODUCTO');
        writeln('2 - MODIFICAR PRODUCTO');
        writeln('3 - ELIMINAR PRODUCTO');
        writeln('4 - BUSCAR PRODUCTO');
        writeln('5 - SALIR');
end;
 
procedure PantallaCarga;
var idUltPro:integer;
begin
        reset(AProductos);
        while not EoF (Aproductos) do
                begin
                        read(AProductos, Productos);
                        idUltPro:=Productos.idProducto;
                end;
        seek(AProductos, Filesize(AProductos));
        ClrScr;
        writeln('       CARGA DE PRODUCTOS');
        writeln('Producto con ID: ', idUltPro+1);
 
        writeln('Nombre producto: ');
        readln(Productos.nombreProducto);
        writeln('Precio producto: ');
        readln(Productos.precioProducto);
        write(AProductos, Productos);
        writeln('       PRODUCTO CARGADO');
end;
 
 
BEGIN
        assign(AProductos, 'C:\Users\Jorge\Desktop\AProductos.dat');
        {$I-}
                Reset(AProductos);
        {$I+}
        if IoResult<>0 then
                Rewrite(AProductos);
 
        repeat
                ClrScr;
                PantallaInicio;
                teclaInicio:=Readkey;
                case teclaInicio of
                        '1':
                                begin
                                        PantallaCarga;
                                        Readkey;
                                end;
                end;
        until teclaInicio = '5';
 
END.

EDITE AL PRINCIPIO EN TYPE ME FALTO PASAR EL RECORD. PERDON
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Manejo de ID con archivos

Publicado por ramon (2158 intervenciones) el 09/08/2017 10:56:54
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
{A ver si esto sirve }
 
program Practica;
uses crt;
type TRegistro = record
        idProducto:integer;
        nombreProducto:string;
        precioProducto:real;
        activoProducto:boolean;
        end;
var
teclaInicio:char;
Productos:TRegistro;
AProductos: file of Tregistro;
 
procedure PantallaInicio;
begin
        ClrScr;
        writeln('       ADMINISTRADOR DE PRODUCTOS ');
        writeln;
        writeln('Ingrese la opcion que desea realizar');
        writeln;
        writeln('    1 - CARGA PRODUCTO');
        writeln('    2 - MODIFICAR PRODUCTO');
        writeln('    3 - ELIMINAR PRODUCTO');
        writeln('    4 - BUSCAR PRODUCTO');
        writeln('    5 - SALIR');
end;
 
 procedure PantallaCarga;
 var
   idUltPro:integer;
   tec : char;
  begin
    assign(AProductos, 'C:\tp\AProductos.dat');
   {$I-} reset(AProductos); {$I+}
   if ioresult <> 0 then
   begin
       rewrite(AProductos);
       idUltPro := 0;
       Productos.idProducto := idUltPro;
       write('  Entre Nombre Producto : ');
       readln(Productos.nombreProducto);
       write('  Entre Precio Producto : ');
       readln(Productos.precioProducto);
       write('  Entre Activo Producto [S/N] : ');
       repeat
       tec := readkey;
       until tec in['S','s','N','n'];
       if (tec = 'S') or (tec = 's') then
       Productos.activoProducto := true
    else
       Productos.activoProducto := false;
       write(AProductos,productos);
       close(AProductos);
   end
 else
    begin
        idUltPro :=  filesize(AProductos);
        Productos.idProducto := idUltPro;
       write('  Entre Nombre Producto : ');
       readln(Productos.nombreProducto);
       write('  Entre Precio Producto : ');
       readln(Productos.precioProducto);
       write('  Entre Activo Producto [S/N] : ');
       repeat
       tec := readkey;
       until tec in['S','s','N','n'];
       if (tec = 'S') or (tec = 's') then
       Productos.activoProducto := true
    else
       Productos.activoProducto := false;
       write(AProductos,productos);
       close(AProductos);
    end;
end;
 
procedure BUSCAR_PRODUCTO;
var
   prod : TRegistro;
   vus, id : longint;
   esta : boolean;
begin
   assign(AProductos, 'C:\tp\AProductos.dat');
   {$I-} reset(AProductos); {$I+}
   if ioresult <> 0 then
   begin
     writeln('  Error de archivo o no existe [Pulse Una Tecla]');
     readkey;
   end
  else
     begin
       esta := false;
        write('  Producto Id : ');
        readln(id);
        for vus := 0 to filesize(AProductos) - 1 do
        begin
           seek(AProductos,vus);
           read(AProductos,prod);
           if prod.idProducto = id then
           begin
              esta := true;
              break;
           end;
       end;
         close(AProductos);
         if esta = true then
         begin
            writeln('   El Producto Es');
            writeln;
            writeln('  ID     = ',prod.idProducto);
            writeln('  Nombre = ',prod.nombreProducto);
            writeln('  Precio = ',prod.precioProducto:0:2);
            writeln('  Activo = ',prod.activoProducto);
            writeln;
            writeln('  Pulse Una Tecla');
            readkey;
         end
      else
         begin
            writeln('  Producto no encontrado [Pulse Una Tecla]');
            readkey;
         end;
     end;
  end;
 
 
BEGIN
    repeat
       PantallaInicio;
       repeat
       teclaInicio := readkey;
       until teclaInicio in['1','2','3','4','5'];
    case teclaInicio of
 '1' : PantallaCarga;
 '2' :;
 '3' :;
 '4' : BUSCAR_PRODUCTO;
    end;
    until teclaInicio = '5';
END.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar