functionnality.VelibAvailableBikesST4.java Source code

Java tutorial

Introduction

Here is the source code for functionnality.VelibAvailableBikesST4.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package functionnality;

import java.util.ArrayList;
import java.util.List;
import model.VelibStation;
import org.json.simple.JSONObject;
import webrequester.VelibWebRequest;

/**
 *
 * @author loic
 */
public class VelibAvailableBikesST4 {
    private VelibStation station;

    public VelibAvailableBikesST4(VelibStation _station) {
        station = _station;
    }

    public VelibStation request() {
        VelibWebRequest velibR = new VelibWebRequest("/vls/v1/stations/" + station.getNumStation());
        JSONObject response;
        String status;
        long available_bike_stands = 0, available_bikes = 0;
        Object tmp;
        if (!station.getNumStation().equals("")) {
            velibR.addParameterGet("contract", "Toulouse");
            response = velibR.parseResultJSON(velibR.requestWithGet());
            status = (String) response.get("status");
            if (status != null) {
                tmp = response.get("available_bike_stands");
                if (tmp != null) {
                    available_bike_stands = (long) tmp;
                }
                tmp = response.get("available_bikes");
                if (tmp != null) {
                    available_bikes = (long) tmp;
                }
                station.setStatus(status);
                station.setAvailableBikes(new Long(available_bikes).intValue());
                station.setAvailableStands(new Long(available_bike_stands).intValue());
                return station;
            }
        }
        return null;
    }
}