Handle an object that is present inside an ArrayList

0

I need to do some combinatorial analysis operations 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 do not see problem, you can talk, my goal is to make it work. Thank you 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)));

    }
}
    
asked by anonymous 27.06.2018 / 06:04

1 answer

0
  • Since you are already using List , then it is best to leave the direct use of arrays wherever possible.

  • Since you are already building your objects with all their attributes filled, it is often a good idea to dispense with the setters.

  • Where the type is not primitive, rejecting null s with an exception is a good idea.

  • Class names should be in the singular, so use final instead of null .

  • You can call the constructor from Horario , but do not declare variables whose type is Horarios , but rather declare ArrayList . The reason for this is to be able to interoperate with other implementations of ArrayList that is not List , such as those that are returned by List , Arraylist or Arrays.asList(...) , or even Collections.unmodifiableList(...) . I believe it was due to this problem that you tried to use List.of(...) , which is a gambiarra.

  • To represent days of the week, use enum LinkedList .

  • Elements sets that are new ArrayList(Arrays.asList s (in this case, the day of the week) are represented by DayOfWeek s.

    Your code looks 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 switch enum to EnumSet , which is simpler and safer.

    To extract the information from Arrays.asList(...) (within the List.of(...) class), you can iterate through them, use stream 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 think a good modeling for the List class will really go down this path, but then that would be a matter for another question.

        
  • 27.06.2018 / 07:33