예제 #1
0
  private List<Car> createCars(int carsToCreate) {
    Car car;
    List<Car> carList = new ArrayList();

    for (int i = 0; i < carsToCreate; i++) {
      car = new Car();
      car.setBodyStyle(car.getBodyStyle());
      car.setColor(car.getColor());
      car.setMake(car.getMake());
      car.setIsFast(car.getIsFast());

      carList.add(car);
    }

    return carList;
  }
예제 #2
0
 public String printCar() {
   String result = "Cars\nid\t\tprice\t\tname\t\tmodel\t\tcolor\n";
   for (int i = 0; i < cars.size(); i++) {
     Car temp = (Car) cars.get(i);
     result =
         result
             + i
             + "\t\t"
             + temp.getPrice()
             + "\t\t"
             + temp.getName()
             + "\t\t"
             + temp.getModel()
             + "\t\t"
             + temp.getColor()
             + "\n";
   }
   return result;
 }
  public static void main(String[] args) {
    ParkingLot parkingLot = new ParkingLot();

    Car car1 = new Car("Honda", "Civic", "gray", 2010);
    parkingLot.addCar(car1);
    Car car2 = new Car("Toyota", "Camry", "green", 2011);
    parkingLot.addCar(car2);
    Car car3 = new Car("Ford", "Focus", "silver", 2001);
    parkingLot.addCar(car3);

    ArrayList<Car> myCars = parkingLot.getAllCars();
    for (Car car : myCars) {
      System.out.println(
          "There is a " + car.getColor() + " " + car.getModel() + " in the parking lot.");
    }

    /*
     If done correctly, the output should be the following:
    		There is a gray Civic in the parking lot.
    	There is a green Camry in the parking lot.
    	There is a silver Focus in the parking lot.
    */
  }
예제 #4
0
  public void postVehicle(Car car) throws JSONException {
    /*
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading Messages... Please wait...");
    dialog.show();
    */
    final Car carToAdd = car;

    JSONObject params = new JSONObject();
    // params.put("alt", "json");
    params.put("username", Global.localUser.getUsername());
    params.put("plateNumber", car.getLicensePlateNumber());
    params.put("plateState", car.getLicensePlateState());
    params.put("make", car.getMake());
    params.put("model", car.getModel());
    params.put("year", car.getYear());
    params.put("color", car.getColor());
    StringEntity entity = new StringEntity(params.toString(), ContentType.APPLICATION_JSON);

    Global.localUser.addCar(carToAdd);
    ParkingRestClient.post(
        this,
        "addVehicle?",
        entity,
        new JsonHttpResponseHandler() {

          @Override
          public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            // dialog.dismiss();

            Log.d("POST VEHICLE", "Success with object response");
            Log.d("POST VEHICLE", response.toString());
            try {
              JSONObject key = response.getJSONObject("key");
              int id = Integer.parseInt(key.getString("id"));
              carToAdd.setId(id);
            } catch (JSONException e) {
              e.printStackTrace();
            }
          }

          @Override
          public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
            Global.localUser.addCar(carToAdd);
            Log.d("POST VEHICLE", "Success with array response");
            Log.d("POST VEHICLE", response.toString());
          }

          @Override
          public void onFailure(
              int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
            // dialog.dismiss();
            Global.localUser.getCars().remove(carToAdd);
            System.out.println(errorResponse);
            Toast toast =
                Toast.makeText(getBaseContext(), "Error connecting to Server", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();
          }
        });
  }