Manipulating an object that is present inside an Arraylist

Asked

Viewed 159 times

0

I need to do some operations of combinatorial analysis with information present in objects, but I do not know how to extract them from ArrayList. I need to remove the content present in the variable priority and absence of each of the objects, if there is an easier way to do, I see no problem, can talk, my goal is to make it work. Thanks in advance.

Class Professor:

package horariosprofessores;

import java.util.Arrays;

public class Professor {
    private Integer id;
    private String nome;
    private String[] materia;
    private Integer[] ausencia;
    private Integer prioridade;

    public Professor(Integer id, String nome, String[] materia, Integer[] ausencia) {
        //0 SEG, 1 TER, 2 QUAR, 3 QUIN, 4 SEX, 5 SAB
        this.id = id;
        this.nome = nome;
        this.ausencia = ausencia;
        this.materia = materia;
        this.prioridade = ausencia.length;
    }

    public Professor(Integer id, String nome, String[] materia) {
        this.id = id;
        this.nome = nome;
        this.materia = materia;
        this.prioridade = 0;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

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

    public String[] getMateria() {
        return materia;
    }

    public void setMateria(String[] materia) {
        this.materia = materia;
    }

    public Integer[] getAusencia() {
        return ausencia;
    }

    public void setAusencia(Integer[] ausencia) {
        this.ausencia = ausencia;
    }

    public Integer getPrioridade() {
        return prioridade;
    }

    public void setPrioridade(Integer prioridade) {
        this.prioridade = prioridade;
    }

    @Override
    public String toString() {
        if (ausencia != null)
        return "Professor [id=" + id + ", nome=" + nome + ", materia=" + Arrays.toString(materia) + ", ausencia="
                + Arrays.toString(ausencia) + ", prioridade=" + prioridade + "]";
        else
            return "Professor [id=" + id + ", nome=" + nome + ", materia=" + Arrays.toString(materia) + ", prioridade=" + prioridade + "]";
    }
}

Class Horarios:

package horariosprofessores;

import java.util.ArrayList;

public class Horarios {
    private ArrayList professores;

    public Horarios(ArrayList professores) {
        this.professores = professores;
    }

    public String toString() {
        return professores.toString();
    }

    public void verificaMatriz() {

    }
}

Class Programa:

package horariosprofessores;

import java.util.ArrayList;
import java.util.Arrays;

public class Programa {
    public static void main(String[] args) {
        Professor p1 = new Professor(1,"Caceraghi", new String[]{"Algoritmos","Ling. Programação"}, new Integer[]{1,3});
        Professor p2 = new Professor(2,"Modesto", new String[]{"Inglês"});
        Professor p3 = new Professor(3,"Fernanda", new String[]{"Sistemas Computacionais"});

        Horarios horarios = new Horarios(new ArrayList(Arrays.asList(p1,p2,p3)));

    }
}

1 answer

0

  1. Once you are already using List, then it is better to leave the direct use of arrays there where possible.

  2. Since you are already building your objects with all your filled attributes, it is usually a good idea to dispense with setters.

  3. Once you would have dismissed the setters, avoid nulls when using primitive types and using the modifier final is a good idea.

  4. Where the type is not primitive, reject nulls with one exception is a good idea.

  5. Class names must be singular, so use Horario instead of Horarios.

  6. You can call the builder ArrayList, but do not declare variables whose type is ArrayList, and yes state as List. The reason for this is to be able to interoperate with other implementations of List that is not Arraylist, such as, for example, those that are returned by Arrays.asList(...), Collections.unmodifiableList(...) or List.of(...), or even LinkedList. I believe it was due to this problem you tried to use new ArrayList(Arrays.asList, what is a gambiarra.

  7. To represent days of the week, use the Enum DayOfWeek.

  8. Sets of elements that are enums (in this case the day-of-the-week) are represented by EnumSets.

Your code goes like this:

package horariosprofessores;

import java.time.DayOfWeek;
import java.util.List;
import java.util.EnumSet;
import java.util.Set;

public class Professor {
    private final int id;
    private final String nome;
    private final List<String> materias;
    private final Set<DayOfWeek> ausencias;
    private final int prioridade;

    public Professor(
            int id,
            String nome,
            List<String> materias,
            Set<DayOfWeek> ausencias)
    {
        if (nome == null) throw new IllegalArgumentException();
        if (materias == null) throw new IllegalArgumentException();
        if (ausencias == null) throw new IllegalArgumentException();
        this.id = id;
        this.nome = nome;
        this.ausencias = ausencias;
        this.materias = materias;
        this.prioridade = ausencias.size();
    }

    public Professor(
            int id,
            String nome,
            List<String> materias)
    {
        this(id, nome, materias, EnumSet.noneOf(DayOfWeek.class));
    }

    public int getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }

    public List<String> getMaterias() {
        return materias;
    }

    public Set<DayOfWeek> getAusencias() {
        return ausencias;
    }

    public int getPrioridade() {
        return prioridade;
    }

    @Override
    public String toString() {
        return "Professor"
                + " [id=" + id
                + ", nome=" + nome
                + ", materias=" + materias
                + ", ausencias=" + ausencias
                + ", prioridade=" + prioridade
                + "]";
    }
}
package horariosprofessores;

import java.util.List;

public class Horario {
    private final List<Professor> professores;

    public Horario(List<Professor> professores) {
        if (professores == null) throw new IllegalArgumentException();
        this.professores = professores;
    }

    public String toString() {
        return professores.toString();
    }

    public void verificaMatriz() {

    }
}
package horariosprofessores;

import java.util.Arrays;
import java.time.DayOfWeek;
import java.util.EnumSet;

public class Programa {
    public static void main(String[] args) {
        Professor p1 = new Professor(1, "Caceraghi",
                Arrays.asList("Algoritmos", "Ling. Programação"),
                EnumSet.of(DayOfWeek.TUESDAY, DayOfWeek.THURSDAY));

        Professor p2 = new Professor(2, "Modesto",
                Arrays.asList("Inglês"));

        Professor p3 = new Professor(3, "Fernanda",
                Arrays.asList("Sistemas Computacionais"));

        Horario horarios = new Horario(Arrays.asList(p1, p2, p3));
    }
}

If you are using Java 9 or higher, you can change the Arrays.asList(...) for List.of(...), that is simpler and safer.

To extract information from List (within the class Horario), you can iterate them, stream them or access them directly:

// Iterando.
for (Professor p : professores) {
    System.out.println(p);
}

// Com stream.
professores.stream().forEach(System.out::println);

// Acessando diretamente o terceiro professor (índice 2, porque o primeiro é 0).
Professor pa = professores.get(2);

So you can do this:

for (Professor p : professores) {
    // Faz o que quiser com p aqui dentro.
    System.out.println(p.getId() + ": " + p.getPrioridade() + " - " + p.getAusencias());
}

However, to be honest, I do not believe that a good modeling for the class Horario will really evolve by this way, but then that would be subject to another issue.

  • I thank you so much for your help, I’m still half a beginner in language, I study for only 6 months, I will study your code and try to improve more mine. If you have time, could you explain to me, what is the best way to evolve my Horario class?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.