Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Infinite Loop

$
0
0
I don't understand why when I call the following method it ends up being an infinite loop.

//Returns a list of suppliers to visit starting with the most valuable
	public ArrayList<Store> getSuppliersToVisit() {
		double maxValue = 0.0;
		Store bestCurrentSupplier = null;
		ArrayList<Store> suppliersToVisit = new ArrayList<Store>(20);
		ArrayList<Store> tempSuppliers;
		tempSuppliers = suppliers;
		
		while(tempSuppliers.size() > 0) {
			for (Store s : tempSuppliers) {
				double distance = 0.0;
				double costOfTravel = 0.0;
				double tempValue = 0.0;
				double costOfItems = 0.0;
				double numOfItems = 0;
				
				GPSLocation gps = new GPSLocation();
				distance = gps.getDistanceInMiles(s.getLocation());
				costOfTravel = distance * 1;
				//Retrieves the inventory of the store and adds to the number
				//of items and there total cost
				for (StoreItem si : s.getInventory()) {
					numOfItems ++;
					costOfItems += si.getPrice();
				}
				//Calculates the value of this supplier and if the supplier
				//is valued higher than the highest valued supplier thus far, 
				//said supplier is initialized as the highest supplier
				tempValue = (numOfItems / costOfItems) + costOfTravel;
				if(tempValue > maxValue) {
					maxValue = tempValue;
					bestCurrentSupplier = s;
				}
				
			}
			tempSuppliers.remove(bestCurrentSupplier);
			suppliersToVisit.add(bestCurrentSupplier);
		}
		
		return suppliersToVisit;
	}


I tell the method to remove the store from the arrayList so I don't understand why the loop would never stop.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>