Exemplo n.º 1
0
  public static ArrayList<Reservation> getReservations(Show[] shows, Customer[] customers) {
    Logger.log("Database.getReservations");
    ArrayList<Reservation> reservations = new ArrayList<Reservation>();
    try {
      ArrayList<Reservation> tempReservations = new ArrayList<Reservation>();
      Show show = null;
      Customer customer = null;

      ResultSet rs = query("SELECT COUNT(*) FROM reservation");
      rs.next();

      rs = query("SELECT * FROM reservation");
      while (rs.next()) {
        ArrayList<Point> position = new ArrayList<Point>();
        for (Show s : shows) {
          if (s.getID() == rs.getInt("forestilling_id")) {
            show = s;
          }
        }
        position.add(new Point(rs.getInt("pos_x"), rs.getInt("pos_y")));
        for (Customer c : customers) {
          if (c.getID() == rs.getInt("kunde_id")) {
            customer = c;
          }
        }
        tempReservations.add(
            new Reservation(show, position, customer, rs.getInt("reservation_id")));
      }
      // Merge all reservations which have the same show AND customer and save them in reservations
      // list
      while (tempReservations.size() != 0) {
        Customer cus = tempReservations.get(0).getCustomer();
        Show s = tempReservations.get(0).getShow();
        ArrayList<Point> seats = new ArrayList<Point>();
        int ID = tempReservations.get(0).getID();
        for (int x = 0; x < tempReservations.size(); x++) {
          if (tempReservations.get(x).getCustomer() == cus
              && tempReservations.get(x).getShow() == s) {
            seats.add(tempReservations.get(x).getFirstPos());
            tempReservations.remove(tempReservations.get(x));
            // After deleting the object, decrease x with 1 (else one element would never be
            // reached)
            x--;
          }
        }
        reservations.add(new Reservation(s, seats, cus, ID));
      }

    } catch (SQLException exception) {
      Logger.log(exception, "Database:getReservations");
    }
    Logger.log("Database.getReservations");
    return reservations;
  }
Exemplo n.º 2
0
 public static boolean createReservation(Show show, Point[] points, Customer c) {
   Logger.log("Database.createReservation");
   String sql = "";
   for (Point seat : points) {
     sql =
         "INSERT INTO reservation(forestilling_id, pos_x, pos_y, kunde_id) VALUES ("
             + show.getID()
             + ","
             + seat.x
             + ","
             + seat.y
             + ","
             + c.getID()
             + ");";
     if (update(sql)) {
       continue;
     }
     return false;
   }
   Logger.log("Database.createReservation");
   return true;
 }