Ejemplo n.º 1
0
  /** The client can book a moto if he/she hasn't an active reserve. */
  private void book() {
    boolean r = ((Client) current).hasActiveReserve();
    boolean correct = false;
    boolean check = false;
    int hours = 0;
    int days = 0;
    int month = 0;
    int year = 0;
    Calendar date = null;
    if (!r) {
      while (!correct) {
        Consola.escriu("Insert a date (hh/dd/mm/yyyy)");
        String fecha = Consola.llegeixString();
        String[] parser = fecha.split("/");
        date = Consola.llegeixDataSistema();
        try {
          hours = Integer.parseInt(parser[0]);
          days = Integer.parseInt(parser[1]);
          month = Integer.parseInt(parser[2]) - 1;
          year = Integer.parseInt(parser[3]);

          while (!check) {
            check = true;
            if (hours > 20 || hours <= 8) {
              check = false;
              Consola.escriu(
                  "The inserted hour is incorrect (8-20 hours only accepted). Please insert it again");
              hours = Consola.llegeixInt();
            }
          }
          check = false;
          while (!check) {
            check = true;
            if (days > 31 || days <= 0) {
              check = false;
              Consola.escriu("The inserted day is incorrect. Please insert it again");
              days = Consola.llegeixInt();
            }
          }
          check = false;
          while (!check) {
            check = true;
            if (month > 12 || month <= 0) {
              check = false;
              Consola.escriu("The inserted month is incorrect. Please insert it again");
              month = Consola.llegeixInt();
            }
          }
          check = false;
          while (!check) {
            check = true;
            if (year >= 2017 || year <= 0) {
              check = false;
              Consola.escriu("The inserted year is incorrect. Please insert it again");
              year = Consola.llegeixInt();
            }
          }

          date.set(year, month, days, hours, 0);
          Calendar currentDate = Consola.llegeixDataSistema();
          correct = currentDate.before(date);

          if (!correct) {
            Consola.escriu("Invalid date, insert again");
          }
        } catch (Exception ex) {
          Consola.escriu(
              "There's a invalid character in the date or an enmpy value, please, write again the date:");
        }
      }

      // Trying to get our starter local.
      Local localS = bookGetLocalS();
      // If it is different from null, then we can keep going.
      if (localS != null) {
        // We can try to get our ending local.
        Local localE = bookGetLocalE();
        // And if indeed we can reach up to an ending local, we can keep going.
        if (localE != null) {
          // We DON'T have to check the same for the motos, because it is considered that a local is
          // available
          // If it has a X amount of motos. So if we reach here, then our starting local is HAS
          // motos for sure.
          Moto motoreta = bookGetMoto(localS);

          Consola.escriu("Insert number of days you would like to reserve:");
          int cantDias = Consola.llegeixInt();
          Consola.escriu("Insert number of hours you would like to reserve:");
          int cantHoras = Consola.llegeixInt();
          this.bookPrintInfo(localS, localE, motoreta, date, cantDias, cantHoras);
          Consola.escriu("Insert an S to confirm or an N to cancel the reserve");
          String ans = Consola.llegeixString();
          correct = checkYesNo(ans);
          if (correct) {
            ((Client) current)
                .addActiveReserve(localS, localE, date, cantHoras, cantDias, motoreta);
            String code = new String();
            code =
                ((Client) current).getId()
                    + localS.getId()
                    + localE.getId()
                    + motoreta.getId()
                    + Integer.toString(cantHoras)
                    + Integer.toString(cantDias);
            code =
                code
                    + date.get(Calendar.HOUR_OF_DAY)
                    + date.get(Calendar.DAY_OF_MONTH)
                    + Integer.toString(date.get(Calendar.MONTH) + 1)
                    + date.get(Calendar.YEAR);
            ((Client) current).setCodeToActiveReserve(code);
            Consola.escriu("Code: " + code);
            Consola.escriu("The reserve has been done and saved");
          }
        }
      }
    } else {
      Consola.escriu("You have already done a reserve");
    }
  }