0

I want to make a method that tells me who is the oldest person in the ArrayList and who is the youngest person. The method will receive the arraylist that i want to apply the method, i have 3 in my code, each one is an contact list.

The method is suppose to return the name of the person, but i don't know how to do it. Familia, Profissional and Amigos are my arraylists and "idade" = age and "nome" = name.

package com.company;

public class Contato {

    public String nome;
    public int idade;
    public String sexo;
    public String profissao;
    public String telefone;
    public String email;

    public Contato(String nome, int idade, String sexo, String profissao, String telefone, String email) {
        this.nome = nome;
        this.idade = idade;
        this.sexo = sexo;
        this.profissao = profissao;
        this.telefone = telefone;
        this.email = email;
    }

    @Override
    public String toString() {
        return "" +
                nome + ',' +
                idade + " anos de idade, " +
                "do sexo " + sexo + ',' +
                profissao + ',' +
                " telefone nº " + telefone + ", " +
                "e-mail:" + email;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getProfissao() {
        return profissao;
    }

    public void setProfissao(String profissao) {
        this.profissao = profissao;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

package com.company;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class GestaoContatos extends Contato {

    ArrayList<Contato> Familia = new ArrayList();
    ArrayList<Contato> Amigos = new ArrayList();
    ArrayList<Contato> Profissional = new ArrayList();

    public GestaoContatos(Contato c) {
        super(c.nome, c.idade, c.sexo, c.profissao, c.telefone, c.email);
    }

    public void adicionaContato(String nomeAgenda, Contato contato) {
        if( nomeAgenda == "Familia"){
            Familia.add(contato);
        } else
            if(nomeAgenda == "Amigos"){
                Amigos.add(contato);
            } else
                if(nomeAgenda == "Profissional") {
                    Profissional.add(contato);
                } else
                    System.out.println("Indispnível");
    }

    public void eliminaContato(String nomeContato) {
        for(int i = 0; i < Familia.size(); i++) {
            if(getFamilia().contains(nomeContato)) {
                Familia.remove(nomeContato);
            }
        }
    }

    public void printaLista(String nomeAgenda){
        if(nomeAgenda.equals("Familia")) {
            Familia.forEach(System.out::println);
        }
        if(nomeAgenda.equals("Amigos")) {
            Amigos.forEach(System.out::println);
        }
        if(nomeAgenda.equals("Profissional")) {
            Profissional.forEach(System.out::println);
        }
        else {
            throw new RuntimeException("Opcao invalida");
        }
    }

    public void tooString() {
        var contatos = new ArrayList<Contato>();
        Familia.forEach(it -> contatos.add(it));
        Amigos.forEach(it -> contatos.add(it));
        Profissional.forEach(it -> contatos.add(it));

        System.out.println(contatos.toString());
    }

    public void olderPerson(String nomeAgenda){
        int i = 0;
        if (nomeAgenda.equals("Amigos")) {
            for (i = 0; Familia.size(); i++) {
                Familia.stream().filter();
            }

        }
    }

    public void geraListaBinaria() throws IOException {
        var file = new File("C:\\Users\\Jorge Luiz\\Desktop\\contatos.txt");
        var writer = new FileWriter(file.getName());
        writer.write(this.getProfissional().toString());
        writer.write(this.getFamilia().toString());
        writer.write(this.getProfissional().toString());
        writer.close();
    }

    public ArrayList<Contato> getFamilia() {
        return Familia;
    }

    public void setFamilia(ArrayList<Contato> familia) {
        Familia = familia;
    }

    public ArrayList<Contato> getAmigos() {
        return Amigos;
    }

    public void setAmigos(ArrayList<Contato> amigos) {
        Amigos = amigos;
    }

    public ArrayList<Contato> getProfissional() {
        return Profissional;
    }

    public void setProfissional(ArrayList<Contato> profissional) {
        Profissional = profissional;
    }
}

7
  • 1
    Well, in order to get a minimum (or maximum) value, the easiest solution is to pick an impossibly-high value for the initial current minimum, iterate the list, and each time you find a value less than the current minimum, update the current minimum value. After you've exhausted all the values, the "current minimum" will be the minimum of all the values. Finding a maximum is the same, but you'd start with an impossibly-low value so the first "real" value is guaranteed to be higher than that. How might that be implemented? Commented May 25, 2021 at 0:11
  • How do i get it tho? I'm trying with forEach, is that right? public void olderPerson(String nomeAgenda){ int i = 99999; if (nomeAgenda.equals("Amigos")) { Familia.forEach(contato -> { int c = contato.getIdade(); if(c < i) { System.out.println(); } }); } } Commented May 25, 2021 at 0:24
  • getIdade() is the method that gets age from Contato or Contact. Commented May 25, 2021 at 0:26
  • I provided an answer using streams, but I now see you flagged your question with for-loop. If you want the stream answer let me know and I'll restore my answer. Commented May 25, 2021 at 0:40
  • Side note: one does not compare String objects with ==. So the if (nomeAgenga == "Amigos") is not going to work. Use if ("Amigos".equals(nomeAgenda) so that the .equals method is used. The example code has a mix of == and .equals() in it. Commented May 25, 2021 at 1:00

1 Answer 1

0

If you want to return oldest person in one list:

public Contato oldestPerson(String nomeAgenda){
    return Familia.stream()
            .filter(c -> c.getNome().equals(nomeAgenda)) // contatos named nomeAgenda
            .max(Comparator.comparingInt(Contato::getIdade)) // take oldest
            .get();
}

For all lists you can do:

public Contato oldestPerson(){
    return Stream.of(Familia,  Amigos, Profissional)
            .flatMap(Collection::stream) // flatting to one long stream
            .filter(c -> c.getNome().equals(nomeAgenda)) // contatos named nomeAgenda
            .max(Comparator.comparingInt(Contato::getIdade))
            .get();
}

EDIT

Based on the comment, we should change a couple of things to achieve what you want. First, we should define a Map<String, List<Contato>> and populate it in the constructor:

private Map<String, List<Contato>> contatoGroups;
private static final String familiaKey = "Familia";
private static final String amogisKey = "Amigos";
private static final String profissionalKey = "Profissional";

public GestaoContatos(Contato c) {
    super(c.nome, c.idade, c.sexo, c.profissao, c.telefone, c.email);

    contatoGroups.put(familiaKey, new ArrayList<>());
    contatoGroups.put(amogisKey, new ArrayList<>());
    contatoGroups.put(profissionalKey, new ArrayList<>());
}

(Consider using enum instead of String as key in the map)

Then wherever you want to get a group, for example: Familia, you should do:

List<Contato> contatoes = contatoGroups.get(familiaKey);

And then we should change the oldestPerson() like this:

public Contato oldestPerson(String nomeAgenda){ // nomeAgenda could be "Familia", "Amigos"...
    List<Contato> selectedGroup = contatoGroups.get(nomeAgenda); 

    return selectedGroup.stream()
            .max(Comparator.comparingInt(Contato::getIdade)) // take oldest
            .get();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man! But this method receive what parameter? nomeAgenda in the context means the name of the arraylist(Amigos, Familia or Profissional) where the method will search the oldest person.
@Ozzott I have edited my answer now based on your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.