<commit_before>unit Classe.Converte.Unidades;

interface

uses ClientModuleUnit1;

type
  TConversor = class
    class function GetValorDolar: Currency;
    class function RealParaDolar(pValor: Currency): Currency;
    class function DolarParaReal(pValor: Currency): Currency;

    class function RealParaDolarStr(pValor: string): string;
    class function DolarParaRealStr(pValor: string): string;

    // Shift + Ctrl + C
  end;

//function ConverteRealParaDolar(pValor: Currency): Currency;
//function COnverteDolarParaReal(pValor: Currency): Currency;

implementation

uses
  System.SysUtils;

// Modelo procedural > evitar utilizar >> preferir class functions
//function ConverteRealParaDolar(pValor: Currency): Currency;
//begin
//  Result := pValor * 3.5;
//end;

{ TConversor }

class function TConversor.DolarParaReal(pValor: Currency): Currency;
begin
  Result := pValor * GetValorDolar;
end;

class function TConversor.RealParaDolar(pValor: Currency): Currency;
begin
  Result := pValor / GetValorDolar;
end;

class function TConversor.DolarParaRealStr(pValor: string): string;
var
  lValorCurr: Currency;
begin
  lValorCurr := StrToCurr(pValor);
  lValorCurr := TConversor.DolarParaReal(lValorCurr);
  Result := CurrToStr(lValorCurr);
end;

class function TConversor.GetValorDolar: Currency;
begin
  try
    Result := ClientModule1.ServerMethods1Client.GetValorDolarAtual;
  except
    on E: Exception do
    begin
      raise Exception.Create('Erro ao converter valor: ' + E.Message);
    end;
  end;
end;

class function TConversor.RealParaDolarStr(pValor: string): string;
var
  lValor: Currency;
begin
  lValor := StrToCurr(pValor); // Shift + Ctrl + A
  lValor := TConversor.RealParaDolar(lValor);
  Result := CurrToStr(lValor);

//  RealParaDolarStr := Result; // Evitar
end;

end.<commit_msg>Final Aula 1<commit_after>unit Classe.Converte.Unidades;

interface

uses ClientModuleUnit1;

type
  TConversor = class
    class function GetValorDolar: Currency;
    class function RealParaDolar(pValor: Currency): Currency;
    class function DolarParaReal(pValor: Currency): Currency;

    class function RealParaDolarStr(pValor: string): string;
    class function DolarParaRealStr(pValor: string): string;

    // Shift + Ctrl + C
  end;

//function ConverteRealParaDolar(pValor: Currency): Currency;
//function COnverteDolarParaReal(pValor: Currency): Currency;

implementation

uses
  System.SysUtils;

// Modelo procedural > evitar utilizar >> preferir class functions
//function ConverteRealParaDolar(pValor: Currency): Currency;
//begin
//  Result := pValor * 3.5;
//end;

{ TConversor }

class function TConversor.DolarParaReal(pValor: Currency): Currency;
begin
  Result := pValor * GetValorDolar;
end;

class function TConversor.RealParaDolar(pValor: Currency): Currency;
begin
  Result := pValor / GetValorDolar;
end;

class function TConversor.DolarParaRealStr(pValor: string): string;
var
  lValorCurr: Currency;
begin
  lValorCurr := StrToCurr(pValor);
  lValorCurr := TConversor.DolarParaReal(lValorCurr);
  Result := CurrToStr(lValorCurr);
end;

class function TConversor.GetValorDolar: Currency;
begin
  try
    Result := ClientModule1.ServerMethods1Client.GetValorDolarAtual;
  except
    on E: Exception do
    begin
      raise Exception.Create('Erro ao converter valor: ' + E.Message);
    end;
  end;
end;

class function TConversor.RealParaDolarStr(pValor: string): string;
var
  lValor: Currency;
begin
  //lValor := StrToCurr(pValor); // Gera erro em converso invlida
  // lValor := StrToCurrDef(pValor, 0 { -1 }); // no gera erro


  if not TryStrToCurr(pValor, lValor) {= False - feio} then // retorna false caso no consiga
  begin
    //      5,6 >> no vai cair
    if (Pos(',', pValor) > 0) and (Pos('.', pValor) > 0) then
    begin
      if Pos(',', pValor) > Pos('.', pValor) then
      begin //  1.200,5 >> removo o ponto
        pValor := StringReplace(pValor, '.', '', [rfReplaceAll]); //1.000.000,3
        lValor := StrToCurr(pValor);
      end else if Pos('.', pValor) > Pos(',', pValor) then
      begin //  2,300.9 >>
        pValor := StringReplace(pValor, ',', '', [rfReplaceAll]);
        pValor := StringReplace(pValor, '.', ',', [rfReplaceAll]);
        lValor := StrToCurr(pValor);
      end;
    end else if (Pos('.', pValor) > 0) then
    begin
      pValor := StringReplace(pValor, '.', ',', [rfReplaceAll]);
      lValor := StrToCurr(pValor);
    end else begin
      // todo: melhorar exception
      raise Exception.Create('Valor invlido!');
    end;


    //     21.6 >> altera para ','

  end;




  lValor := TConversor.RealParaDolar(lValor);


  Result := CurrToStr(lValor);

//  RealParaDolarStr := Result; // Evitar


// Shift + Ctrl + A >> uses do Delphi
end;

end.
{'fraction_non_alphanumeric': 0.1192486281131279, 'fraction_numerical': 0.00823132123258759, 'mean_word_length': 3.900723888314374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550086', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1690, 'n_words': 398}