Ver Mensaje Individual
  #3  
Antiguo 31-12-2007
Avatar de gerupc
gerupc gerupc is offline
Miembro
 
Registrado: jul 2007
Ubicación: Universidad de Deusto
Posts: 61
Reputación: 17
gerupc Va por buen camino
Exclamation

Hola Dec!!

Muchas gracias por tu ayuda. Me parece, si no te importa, que será mejor que ponga las clases que he implementado, y el programa principal. Respecto a lo que me comentas, seguro que funciona, y seguro que es mejor solución que la que pueda dar, pero en este caso, me exigen que cree yo todas las clases, funciones...

Mira, aqui, el programa principal:

Código Delphi [-]
program Parking;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ClaseCoche,ClaseGaraje;

procedure Limpiar(lineas: integer);
var
  i: integer;
 Begin
   for i := 1 to lineas do
     writeln('');
 end;

procedure EntradaVehiculo();   //ENTRADA VEHICULO
type
        Tplaza = array[1..100] of Coche;
var
  plaza: Tplaza;
  parking: Garaje;
  i,pos: integer;
  libre: boolean;
 Begin
      //libre := false;
      //i := 1;

      pos := parking.GetPlazaLibre();
      plaza[pos] := Coche.Create();
      plaza[pos].Introducir_Datos();
      parking.EntradaGaraje(plaza[pos]);
      parking.VisualizarGaraje();
 end;
procedure Menu();
var
  opcion: integer;
  parking: Garaje;
 Begin
 repeat
        parking := Garaje.Create();
    Limpiar(5);
    writeln(#9,'1.Entrada de Vehiculo.');
    writeln(#9,'2.Salida de Vehiculo.');
    writeln(#9,'3.Visualizar estado de Garaje.');
    writeln(#9,'4.Estadistica de marca.');
    writeln(#9,'5.Estadistica general.');
    writeln(#9,'6.Salir.');
    Limpiar(1);
    write(#9,'Introduce la opcion: ');
    readln(opcion);

    case opcion of
    1: EntradaVehiculo();


    end;
until opcion>1000;
 end;
begin
    Menu;
end.

Aqui, la clase coche:

Código Delphi [-]
unit ClaseCoche;

interface
type
  Tmarca = (Volvo, Renault, Nissan);
  /////////////////////////////////////////
  ////////////////////////////////////////
  //Clase Coche
  
  Coche = class
    private
       marca: Tmarca;
       matricula: string;
       horaEntrada,minutoEntrada: integer;
    public
       constructor Create;
       Overload; //Porque en otras clases tambien utilizare
                 //el Constructor Create,es decir, una sobrecaraga
       constructor Create(matri: string;marca_coche: Tmarca;horentrada,minentrada: integer);
       Overload;
       function getMatricula():string;
       function getMarca(): Tmarca;
       function getHora(): integer;                                  
       function getMinutos(): integer;
       function getTextoHora(): string; //Para formato Hora:Minutos FORMATEAR

       procedure setMatricula(matri: string);
       procedure setMarca(marca_coche: Tmarca);
       procedure setHora(horaentr: integer);
       procedure setMinuto(minentrada: integer);

       procedure Introducir_Datos();
  end;

Implementation

constructor Coche.Create();
 Begin

 end;

constructor Coche.Create(matri: string; marca_coche: Tmarca; horentrada: Integer; minentrada: Integer);
begin
  matricula := matri;
  marca := marca_coche;
  horaEntrada := horentrada;
  minutoEntrada := minentrada;
end;

procedure Coche.setMatricula(matri: string);
begin
  matricula := matri;
end;

procedure Coche.setMarca(marca_coche: Tmarca);
begin
  marca := marca_coche;
end;

procedure Coche.setHora(horaentr: Integer);
begin
  HoraEntrada := Horaentr;
end;

procedure Coche.setMinuto(minentrada: Integer);
begin
  minutoentrada := minentrada;
end;

function Coche.getMatricula(): string;
 Begin
   result := Matricula;
 end;

function Coche.getMarca(): Tmarca;
begin
      result := Marca;
end;

function Coche.getHora(): integer;
begin
     result := horaentrada;
end;

function Coche.getMinutos(): integer;
begin
  result := minutoentrada;
end;

function Coche.getTextoHora(): string;
var
  hora,minuto,horatexto: string;
begin
    Str(horaentrada,hora);
    Str(minutoentrada,minuto);
    horatexto := hora+':'+minuto;
    result := horatexto;
end;

procedure Coche.Introducir_Datos();
var
  matricula: string;
  hora,minuto,seleccion: integer;
  marca: Tmarca;
begin
    write('Introduce la matricula del coche: ');
    readln(matricula);
    setMatricula(matricula);
repeat
    write('Introduce la marca del coche (0=Volvo//1=Renault//2=Nissan: ');
    readln(seleccion);

      case seleccion of
        0: marca := Volvo;
        1: marca := Renault;
        2: marca := Nissan
        else
          writeln('Error!! Debe introducir una de estas marcas.');
      end;
    until (marca = volvo)or(marca=Renault)or(marca=Nissan);
    setMarca(marca);
 repeat
    write('Hora Entrada: ');
    readln(hora);
    write('Minuto Entrada: ');
    readln(minuto);

    if ((hora<=0)or(hora>24))and((minuto>60)or(minuto<=0)) then
     writeln('Error! Por favor, Introduce una hora correcta.');

 until ((hora>=0)and(hora<=24))and((minuto<=60)and(minuto>=0));
    setHora(horaentrada);
    setMinuto(minuto);
end;
//Fin de Implementacion de los metodos correspondientes a la clase Coche
end.

Y por último , la clase garaje (parking era el objeto de la clase garaje que yo he creado):

Código Delphi [-]
unit ClaseGaraje;

interface

Uses ClaseCoche;

type
  TarrayMarcas = array[Tmarca] of integer; //Porque estan enumeradas [1] = Volvo...
  TarrayGaraje = array[1..100] of Coche; //Posibilidad de crear 100 objetos de clase 'COCHE'
  //Array Coche tendra funcion de lista de objetos,en cada pos, un objeto de clase coche

  //Defino la clase garaje

  Garaje = class
    private
      plazasarray: TarrayGaraje; //El array con los objetos coche
      NumCoches: integer; // Numero de objetos del array coches

    public
      constructor Create;
      Overload;
      function GetNumCoches(): integer;
      function GetPosicionCoche(matricula: string): integer; //Devuelve la Pos del Coche o -1 si no esta
      Overload;
      function GetPlazaLibre(): integer;
      function GetCoche(Posicion: integer): Coche; //Clase como Tipo de Dato, si no hay coche, devuelve Nil
      Overload;
      procedure EntradaGaraje(CocheObjeto: Coche);
      procedure SalidaGaraje(matricula: string);
      procedure VisualizarGaraje();
      procedure EstadisticaMarcas();
      function ContarMarca(marc: Tmarca): integer; //Devuelve el numer de coches que hay por cada Marca
  end;

Implementation

Constructor Garaje.Create();
begin
   NumCoches := 0;
end;

function Garaje.GetNumCoches(): integer;
var
  i: integer;
begin
     NumCoches := 0;
     for i := 1 to length(plazasarray) do
       Begin
         if plazasarray[i] <> nil then
           Begin
             NumCoches := NumCoches + 1;
           end;
       end;
     result := NumCoches;
end;

function Garaje.GetPlazaLibre(): integer;
var
        i,pos: integer;
        libre: boolean;
 Begin
        libre := false;
        i := 1;

        while Not(libre)and(i<=100) do
         Begin
                if plazasarray[i] = nil then
                 Begin
                        pos := i;
                        libre := true;
                 end
                else
                        i := i + 1;
         end;

        result := pos;
 end;
function Garaje.GetPosicionCoche(matricula: string): integer;
var
  i,posicion: integer;
  encontrado: boolean;
begin
      encontrado := false;
      i := 1;

      while (Not(Encontrado))and(i<=100) do
       Begin
         if plazasarray[i].getMatricula() = Matricula then
          Begin
            result := i;
            encontrado := true; //Salimos del Bucle While
          end
          else
           Begin
            result := -1;
            i := i + 1;
           end;
       end;

end;

function Garaje.GetCoche(Posicion: Integer): Coche;
begin
     if plazasarray[posicion] = nil then
        result := nil   //Esta vacia
     else
        result := plazasarray[posicion];
end;

procedure Garaje.EntradaGaraje(CocheObjeto: Coche);
var
  i: integer;
  vacia,lleno: boolean;
begin
  vacia := false;
  lleno := true;
  i := 1;
    while (Not(vacia)and(i<=100)) do
     Begin
       if plazasarray[i] = nil then
        Begin
             plazasarray[i] := CocheObjeto;
             NumCoches := GetNumCoches();
             vacia := true //Salimos del Bucle
        end
       else
         Begin
             i := i + 1;
         end;
     end; //Fin While
     //Compruebo si esta lleno
    while (lleno)and(i<=100) do
     Begin
        if plazasarray[i] <> nil then
         Begin
           lleno := true;
           i := i + 1;
         end
        else
           lleno := false;
     end;
    if Lleno then
     writeln('El Parking Esta lleno');
end;

procedure Garaje.SalidaGaraje(matricula: string);
var
  i,hora,minutos: integer;
  tiempo_total,importe: real;
  encontrado: boolean;
begin
  //Busco el coche por la matricula
  encontrado := false;
  i := 1;
      while  (Not(encontrado)and(i<=100)) do
       Begin
         if plazasarray[i].getMatricula() = matricula then
          Begin
               write('Introduce la hora actual, Hora: ');
               readln(hora);
               write('Minutos: ');
               readln(minutos);

               if minutos>plazasarray[i].getMinutos() then
                minutos := minutos-plazasarray[i].getMinutos()
               else
                Begin
                  minutos := plazasarray[i].getMinutos()-minutos;
                end;

               tiempo_total := ((hora-plazasarray[i].getHora())*60) + minutos;
               importe := tiempo_total * 0.05;
               writeln('IMPORTE: ',importe:0:2);
               plazasarray[i] := nil;
               plazasarray[i].Free;
               NumCoches := GetNumCoches();
               encontrado := true;
          end
         else //Si no encuentra la matricula en una plaza
          Begin
            i := i + 1;
            encontrado := false; //Y sigue buscando hasta que sea true
          end;
       end;
      if Not(encontrado) then
       writeln('El coche de matricula ',matricula,' no existe');
end;

procedure Garaje.VisualizarGaraje();
var
  i: integer;
begin
    for i := 1 to length(plazasarray) do
     Begin
       if plazasarray[i] <> nil then
        write(' ',plazasarray[i].getMatricula(),' ')
       else
          write('  V  ');
       if i mod 10 = 0 then
        writeln('');
     end;
end;

procedure Garaje.EstadisticaMarcas();
var
  i,contvolvo,contrenault,contnissan,contnil: integer;
  arraymarcas: Tarraymarcas;
begin
  contvolvo := 0;
  contrenault := 0;
  contnissan :=0;
  contnil := contnil + 1;
    for i := 1 to 100 do
     Begin
      if plazasarray[i] = nil then
        contnil := contnil + 1     //Cuenta las plazas vacias, ya de paso, porque si no
      else                         //al ser nil,produce una excepcion al no poder acceder a las
                                   //funcion y datos miembro
       Begin
          if plazasarray[i].getMarca() = Volvo then
             contvolvo := contvolvo + 1
             else if plazasarray[i].getMarca() = Renault then
                contrenault := contrenault + 1
                   else if plazasarray[i].getMarca() = Nissan then
                      contnissan := contnissan + 1;
       end;
     end;
    writeln('');
    writeln('');
    writeln('------- ESTADISTICA DE MARCAS -------');
    writeln('');
    writeln('');
    write('Volvo:    |');
    for i := 1 to contvolvo do
     Begin
      write('*');
     end;
    writeln('');
    write('Renault:  |');
    for i := 1 to contrenault do
     Begin
      write('*');
     end;
    writeln('');
    write('Nissan:   |');
    for i := 1 to contnissan do
     Begin
      write('*');
     end;
    readln;
end;

function Garaje.ContarMarca(marc: Tmarca): integer;
var
  i,contvolvo,contrenault,contnissan,contnil: integer;
  arraymarcas: Tarraymarcas;
  es_nil: boolean;
begin
  contvolvo := 0;
  contrenault := 0;
  contnissan :=0;
  contnil := contnil + 1;
    for i := 1 to 100 do
     Begin
      if plazasarray[i] = nil then
        contnil := contnil + 1
      else
       Begin
          if plazasarray[i].getMarca() = Volvo then
             contvolvo := contvolvo + 1
             else if plazasarray[i].getMarca() = Renault then
                contrenault := contrenault + 1
                   else if plazasarray[i].getMarca() = Nissan then
                      contnissan := contnissan + 1;
       end;
     end;

  //HACEMOS QUE DEVUELVA EL NUMERO DE COCHES DE LA MARCA QUE SE HA PASADO COMO PARAM.

    if marc = volvo then
      result := contvolvo
      else if marc = renault then
           result := contrenault
           else if marc = nissan then
                result := contnissan;

end;
//FIN IMPLEMENTACION CLASE GARAJE.
end.
__________________
Mi Blog
---------------------
Universidad de Deusto
ESIDE-Escuela Superior de Ingenieros Deusto
Responder Con Cita