mercredi 9 mai 2018

Odd writing of files after first iteration of the whole ArrayList

Yet another question from me. After circling through an ArrayList once and populating another correctly, my for loop goes on and populates the other list oddly. I can't really understand why. Please can somebody give me a fast response?

public SearchResult getNearby(ClientDescriptor clientDescriptor) {
    final int R = 6371; //Earth's radius
    List<HotelDescriptor> hotelsforit = new ArrayList<>();
    Result tempRes = new Result(clientDescriptor,hotelsforit);
    for (HotelDescriptor i : hotelData) {
        double n = i.getCoordinates().getLatitude();
        double dist1 = clientDescriptor.getCoordinates().getLatitude() - i.getCoordinates().getLatitude();
        double dist2 = clientDescriptor.getCoordinates().getLongitude() - i.getCoordinates().getLongitude();
        double latDist = toRad(dist1);
        double lonDist = toRad(dist2);
        double a = Math.sin(latDist / 2) * Math.sin(latDist / 2) +
                Math.cos(toRad(i.getCoordinates().getLatitude())) * Math.cos(toRad(clientDescriptor.getCoordinates().getLatitude())) *
                        Math.sin(lonDist / 2) * Math.sin(lonDist / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c;
        double radius = clientDescriptor.getRadius();
        System.out.println("The distance between " + clientDescriptor.getName() + " and " + i.getName() + " is " + distance + " kilometers");
        System.out.println();
        if (distance < radius)
            tempRes.hotels.add(i);
    }
    return  tempRes;
}

Input :

Hampton by Hilton;Bulevardul 21 Decembrie 1989 67, Cluj-Napoca 400124;46.775113;23.601878;2;350;1;400;1;650

Grand Hotel Italia;Strada Trifoiului 2, Cluj-Napoca 400478;46.752879;23.605848;2;360;1;420;1;700

Beyfin;Piata Avram Iancu 3, Cluj-Napoca 400098;46.771342;23.596176;4;300;8;370;0;600

Hotel Opera Plaza;Strada General Traian Mosoiu 10-12, Cluj-Napoca 400132;46.768870;23.600571;4;450;8;520;2;800

Grand Hotel Napoca;Strada Octavian Goga 1, Cluj-Napoca 400698;46.771694;23.576061;5;450;7;450;3;800;

Hotel Confort;Calea Turzii 48, Cluj-Napoca 400193;46.763728;23.597908;6;200;7;200;1;350

Hotel Paradis;Strada Ciocarliei, Cluj-Napoca 400124;46.779862;23.611739;7;200;8;250;1;400

Hotel Sunny Hill;Strada Fagetului 31A, Cluj-Napoca 400497;46.716030;23.573740;4;150;6;190

Golden Tulip Ana Dome;Strada Observatorului 129, Cluj-Napoca 400352;46.751989;23.576580;0;330;0;350;0;600

And the odd output after writing into a .csv :

Hampton by Hilton,Single,2,350.0,Double,1,400.0,Suite,1,650.0, Beyfin,Single,4,300.0,Double,8,370.0,Suite,0,600.0, Hotel Opera Plaza,Single,4,450.0,Double,8,520.0,Suite,2,800.0, Hotel Paradis,Single,7,200.0,Double,8,250.0,Suite,1,400.0, Hampton by Hilton,Single,2,350.0,Double,1,400.0,Suite,1,650.0, Beyfin,Single,4,300.0,Double,8,370.0,Suite,0,600.0, Hotel Opera Plaza,Single,4,450.0,Double,8,520.0,Suite,2,800.0, Hotel Confort,Single,6,200.0,Double,7,200.0,Suite,1,350.0, Hotel Paradis,Single,7,200.0,Double,8,250.0,Suite,1,400.0, Grand Hotel Italia,Single,2,360.0,Double,1,420.0,Suite,1,700.0, Hotel Confort,Single,6,200.0,Double,7,200.0,Suite,1,350.0, Grand Hotel Italia,Single,2,360.0,Double,1,420.0,Suite,1,700.0, Beyfin,Single,4,300.0,Double,8,370.0,Suite,0,600.0, Hotel Opera Plaza,Single,4,450.0,Double,8,520.0,Suite,2,800.0, Hotel Confort,Single,6,200.0,Double,7,200.0,Suite,1,350.0, Golden Tulip Ana Dome,Single,0,330.0,Double,0,350.0,Suite,0,600.0, Hampton by Hilton,Single,2,350.0,Double,1,400.0,Suite,1,650.0, Grand Hotel Italia,Single,2,360.0,Double,1,420.0,Suite,1,700.0, Beyfin,Single,4,300.0,Double,8,370.0,Suite,0,600.0, Hotel Opera Plaza,Single,4,450.0,Double,8,520.0,Suite,2,800.0, Grand Hotel Napoca,Single,5,450.0,Double,7,450.0,Suite,3,800.0, Hotel Confort,Single,6,200.0,Double,7,200.0,Suite,1,350.0, Hotel Paradis,Single,7,200.0,Double,8,250.0,Suite,1,400.0, Golden Tulip Ana Dome,Single,0,330.0,Double,0,350.0,Suite,0,600.0,

This one is for a client searching hotels in a 1 km radius. As you can see, the first 4 lines are totally correct then the addition to the list goes nuts.

The distances:

The distance between - and Hampton by Hilton is 0.3174609238931157 kilometers

The distance between - and Grand Hotel Italia is 2.578986251311183 kilometers

The distance between - and Beyfin is 0.9022687388120708 kilometers

The distance between -and Hotel Opera Plaza is 0.8944861012510631 kilometers

The distance between - and Grand Hotel Napoca is 2.316705705166163 kilometers

The distance between - and Hotel Confort is 1.4985385967615898 kilometers

The distance between - and Hotel Paradis is 0.6178315047415021 kilometers

The distance between - and Golden Tulip Ana Dome is 3.4824383011487625 kilometers

Aucun commentaire:

Enregistrer un commentaire