0

I'm trying to add together a specific attribute of my objects(that are stored in an arraylist) using a for loop. I've tested it, and for some reason only one object gets repeatedly called. The method I am referring to is getCostOfRun(). There is more code, if you require more context please just ask.

import java.util.ArrayList;                                             
import java.util.Arrays;                                                
public class Phone                                              
{                                               
private static String Brand;                                                
private static String Type;                                             
private static String Model;                                                
private static String UniqueID;                                             
private static int ManufCost;                                               
public static ArrayList<Phone> Phones = new ArrayList<Phone>();                                             
public static int counter;                                              
                                            
public Phone(String Brand, String Type, String Model, String UniqueID, int ManufCost)                                               
{                                               
this.Brand=Brand;                                               
this.Type=Type;                                             
this.Model=Model;                                               
this.UniqueID=UniqueID;                                             
this.ManufCost=ManufCost;                                               
Phones.add(this);                                               
counter =counter +1;                                                
System.out.println(counter);                                                
}

public static int getCostOfRun()                                                
{                                               
int TotalCost=0;                                                
int tempcost=0;                                             
int count=0;                                                
                                            
 while (Phones.size() > count) {                                                
   tempcost=Phones.get(count).ManufCost;                                                
   System.out.println(tempcost);                                                
   TotalCost=TotalCost+tempcost;                                                
   System.out.println(TotalCost);                                               
   count++;                                             
  }                                             
return TotalCost;                                               
}                                               
  • Welcome to StackOverflow. [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Instead, [edit] your question to add all code relevant for your question here. Ideally, post a [mre] of your problem, so people can help you better and more easily. – maloomeister Oct 13 '21 at 14:20
  • Since you have now added the code to your question, have a look at [mre]. Currently we can not help you, since we don't know what you see, what `Phones` is and what it contains. We have no means to see what you see, therefore we can't help you or tell you what's wrong. – maloomeister Oct 13 '21 at 14:25

1 Answers1

1

with java 8 stream you can do it with

int totalCost = Arrays.stream(Phones).map(p -> p.manfCost).reduce(0, Integer::sum);