new java developer here so please bear with me.
I'm currently creating a basic program which will take user inputs and store them in an array list. There are some support logs in an array list and I am trying to calculate the sum of total hours spent from all support log entries.
This is my array list and the records:
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<User> listOfUsers = new ArrayList<>();
ArrayList<Customer> listOfCustomers = new ArrayList<>();
ArrayList<Logs> listOfLogs = new ArrayList<>();
listOfUsers.add(new User("janedoe@gmail.com", "Jane Doe", "testABC"));
listOfLogs.add(new Logs(11, 24, 05, 2018, 3, 280.04));
listofLogs.add(new Logs(12, 12, 09, 2018, 4, 290.11));
the fifth values in the list are the hours (3 & 4 respectively).
This is what I tried at the moment but it didn't work:
if (menuOption == 5) {
double sum = 0;
for(int i = 0; i < l.getHours(); i++)
sum += l.get(i);
return sum;
}
my getter is as follows:
public int getHours() {
return this.hours;
}
I will appreciate all support, thanks in advance.
EDIT: So after some answers have been posted, I have managed to iterate over the lists and get the values of the hours as follows:
if (menuOption == 5) {
for(Logs aLog : listOfLogs) {
double sum = 0;
sum += aLog.getHours();
System.out.println(sum);
}
}
I get the following output:
3.0
4.0
How do I add the values so that they would show as a total of "7.0" ?
You could try this:
if (menuOption == 5) {
double sum = 0;
for(int i = 0; i < listOfLogs.size() ; i++){
sum += listOfLogs.get(i).getHours();
}
return sum;
}
Changes made:
In the for
loop i<(size of the listOfLogs List)
which contains the Logs Object. Then, you have to iterate through each Logs instance and get the hours and add that to sum.