Java - Calculating the value of hours for the person responsible for an event

Asked

Viewed 49 times

-1

I am building a project (I am a student) in which I need to create an event company.

The system shall provide:

  1. the amount spent with the responsible;
  2. the total cost of lectures;
  3. the total amount spent per lecture;
  4. the speaker’s data with the highest and lowest value spent on his lecture.

In the code below I created the classes Palestra, Palestrantes and Responsavel. I’m caught trying to create some way to return the total amount spent on those responsible for the events. I created a for to walk the arraylist, It just didn’t work out. The way I did it just displays on screen the values of the time of the responsible ones, but I could not calculate the value of the hour by the number of hours of the events.

Somebody give me a hand?

package eventoHSM;

import java.util.ArrayList;
import java.util.List;


public class Eventos {

    public static void main(String[] args) {

        Palestra p1 = new Palestra();
        p1.setTitulo("Java Guanabara");
        p1.setCargaHoraria(8);
        p1.setDescricao("Aprenda Java com Orientação objetos nessa série de 30 aulas");
        p1.setHorarioInicio("18h");
        p1.setNomePalestrante("Gustavo Guanabara");
        p1.setNomeResponsavel("Pablo");

        Palestra p2 = new Palestra();
        p2.setTitulo("Python Guanabara");
        p2.setCargaHoraria(16);
        p2.setDescricao("Aprenda Python com Orientação objetos nessa série de 30 aulas");
        p2.setHorarioInicio("20h");
        p2.setNomePalestrante("Gustavo Guanabara"); 
        p2.setNomeResponsavel("Pablo");

        Palestra p3 = new Palestra();
        p3.setTitulo("Aprendendo Libras");
        p3.setCargaHoraria(40);
        p3.setDescricao("Aprenda Libras com quem fala libras");
        p3.setHorarioInicio("20h");
        p3.setNomePalestrante("Ana Claudia");   
        p3.setNomeResponsavel("Ana");

        Palestra p4 = new Palestra();
        p4.setTitulo("Daniel e Apocalipse");
        p4.setCargaHoraria(16);
        p4.setDescricao("Aprenda Daniel e Apocalipse com Felipe Silva");
        p4.setHorarioInicio("20h");
        p4.setNomePalestrante("Felipe Silva");  
        p4.setNomeResponsavel("Ana");
        
        
        Responsavel re1 = new Responsavel();
        re1.setNomeResponsavel("Pablo");
        re1.setNumTelefone("71992092623");
        re1.setValorHora(26);
        
        Responsavel re2 = new Responsavel();
        re2.setNomeResponsavel("Ana");
        re2.setNumTelefone("988599600");
        re2.setValorHora(32);
        
        List<Responsavel> responsaveis = new ArrayList<Responsavel>();
        responsaveis.add(re1);
        responsaveis.add(re2);
        
        
        List<Palestra> palestras = new ArrayList<Palestra>();
        palestras.add(p1);
        palestras.add(p2);
        palestras.add(p3);
        palestras.add(p4);
        

        
        for(int a = 0; a < palestras.size(); a++) {
            //System.out.println(palestras.get(a).getCargaHoraria() + " - " + palestras.get(a).getNomeResponsavel());
            for(int i = 0; i< responsaveis.size(); i++) {
                if(palestras.get(a).getNomeResponsavel() == responsaveis.get(i).getNomeResponsavel() ) {
                    float valorPorResponsavel = 0;
                    valorPorResponsavel = valorPorResponsavel + responsaveis.get(i).getValorHora();
                    
                    System.out.println(valorPorResponsavel);
                }
            }
        }
        
        


    }

}
  • I can give an idea, I do not want to make the solution because the way is this same, you need to try and find out, but you could have a class of Costs and in this class would have a method of attribution making the proper sums, could add the data inputados and return.

2 answers

1

Create a variable outside the for to store the total sum of the responsible ones. Inside of the for you make the account of how many hours were spent * the value of the hour of each responsible person. Ex.:

float total=0;
for(int a = 0; a < palestras.size(); a++) {
    for(int i = 0; i< responsaveis.size(); i++) {
        if(palestras.get(a).getNomeResponsavel() == responsaveis.get(i).getNomeResponsavel() ) {
                
            total += palestras.getDuracao() * responsaveis(i).getValorHora();
                
        }
    }
}
System.out.println(total);

But... here we can improve and much the code. Look at this:

  • Use objects of the type Localdatetime in the object Lecture to, later, calculate how long the lecture lasted;
  • Convert into minutes, and convert the speaker’s time value into minutes, so that it has the correct value;
  • In the Lecture object, instead of defining the speaker’s name, create an object of the Speaker type. This will help in the;
  • Utilize foreach to go through the array;

0

Create a variable to store the total value of everything. A good practice is also to use foreach for lists, in addition to equals to compare strings, this way:

float valorTotalporHora;
for(Palestra palestra : palestras){
    for(Responsavel responsavel : responsaveis){
        if(palestra.getNomeResponsavel().equals(responsavel.getNomeResponsavel()))
            valorTotalporHora += palestra.getDuracao() * responsavel.getValorHora();
            break;
    }
}

Note: I used break in case I do the operation, he does not need to check the other responsible in the list.

Friend @lelos also gave great tips on how to improve your code!

Browser other questions tagged

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