Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

treinando structs

projetos.livemd

treinando structs

Structs

defmodule People do
  defstruct id: nil,
            first_name: nil,
            last_name: nil, 
            birthday: nil,
            location: "home",
            created_at: nil

  def new(first_name, last_name, birthday, location \\ "home") do
    %People{
      id: :erlang.unique_integer([:positive]),
      first_name: first_name,
      last_name: last_name,
      birthday: birthday,
      created_at: DateTime.utc_now(),
      location: location
    }
  end

  def full_name(%__MODULE__{} = person) do
    "#{person.first_name} #{person.last_name}"
  end

  def age(%__MODULE__{} = person) do
    days = Date.diff(Date.utc_today(), person.birthday)
    days / 365.25
  end

  def toggle_location(%__MODULE__{location: "away"} = person) do
    person |> set_location("home")
  end

  defp set_location(%__MODULE__{} = person, location) do
    %{person | location: location}
  end
  
end
people = People.new("Test", "testing", ~D[2000-01-01])
People.full_name(people)
People.age(people)
People.toggle_location(people)
defmodule Todo do
  defstruct id: nil, title: "", completed: false, created_at: DateTime.utc_now(), date_update: nil  
end
defmodule TodoList do
  defstruct [:id, :title]
  
  def start do
    []
  end

  def add_todo(todos, title) do
    new_todo = %Todo{id: :erlang.unique_integer([:positive]), title: title}
    [new_todo | todos]
  end

  def list_todos(todos) do
    Enum.each(todos, fn todo -> 
      IO.puts("ID: #{todo.id} - #{todo.title} [#{if todo.completed, do: "✅", else: " "}]")
      end)
  end

  def completed_todo(todos, id) do
    Enum.map(todos, fn todo -> 
      if todo.id == id do
        %{todo | completed: true, date_update: DateTime.utc_now()}
      else
        todo
      end
      end)
  end

  def remove_todo(todos, id) do
    Enum.reject(todos, fn todo -> todo.id == id end)
  end
end
todos = TodoList.start()
todos = TodoList.add_todo(todos, "Estudar elixir")
todos = TodoList.add_todo(todos, "Escrever artigo")
todos = TodoList.add_todo(todos, "Fazer exercício")
TodoList.list_todos(todos)
todos = TodoList.completed_todo(todos, 99)
TodoList.list_todos(todos)
todos = TodoList.remove_todo(todos, 1)
TodoList.list_todos(todos)
defmodule Produto do
  defstruct nome: nil, valor: 1.99
end
sabone = %Produto{nome: "Dove"}
shampoo = %Produto{nome: "Nivia", valor: 5.50}
defmodule Estudante do
  
  defstruct [:id, :nome, :idade, :matricula, :curso]

  def new(nome, idade, matricula, curso) do
    case verificar_idade(idade) do
      {:ok, idade} -> 
        %Estudante{
            id: :erlang.unique_integer([:positive]),
           nome: nome,
           idade: idade,
            matricula: matricula,
           curso: curso
        }
      {:error, _} -> "idade menor"
      _ -> "não tem idade"
    end
    
  end

  def add_estudante(estudantes, estudante) do
    [estudante | estudantes]
  end

  def delete_estudante(estudantes, matricula) do
    Enum.filter(estudantes, fn estudante -> estudante.matricula != matricula end)
  end

  def update_estudante(estudantes, matricula, novos_dados) do
    Enum.map(estudantes, fn estudante 
      when estudante.matricula == matricula ->
      Map.merge(estudante, novos_dados)
    estudante -> 
      estudante
      end)    
  end

  def imprimir_estudantes_cadastrados(estudantes) do
    Enum.map(estudantes, fn estudante -> estudante.nome end)
  end

  def listar_estudantes_por_curso(estudantes, curso) do
    Enum.filter(estudantes, fn estudante -> estudante.curso == curso end)
  end

  defp verificar_idade(idade) do
    if idade > 18 do
      {:ok, idade}
    else
      {:error, "digite uma idade maior que 18"}
    end
  end
end
estudantes = []
estudante1 = Estudante.new("Marcus", 19, 11111, "Engenharia de software")
estudante2 = Estudante.new("Sarah", 25, 12352, "Engenharia de software")
estudantes = Estudante.add_estudante(estudantes, estudante1)
estudantes = Estudante.add_estudante(estudantes, estudante2)
IO.inspect(estudantes, label: "Estudantes cadastrados")
estudantes = Estudante.delete_estudante(estudantes, 12352)
Estudante.imprimir_estudantes_cadastrados(estudantes)
IO.inspect(estudantes)
estudantes = Estudante.add_estudante(estudantes, estudante2)
Estudante.listar_estudantes_por_curso(estudantes, "Engenharia de software")