samedi 2 mars 2019

Modeling Event and Reservations in JAVA Class - Best Design Approach

I am pretty new to java and am building a basic tour reservation system. I have a tour class which looks like this.

public class TourModel {
        
        private int tourID;
        private String tourname;
        private String tourlocation;
        private String country;
        private String groupsize;
        private String tourprice;
        private String tourduration;
        private String tourdescription;
        private int availableSeats;
        private Date tourDate;
        private String tourBanner;
        private ArrayList<ReservationModel> reservations;

The Reservation Class looks like

package com.artoftravel.pk.reservations;

import java.util.ArrayList; import java.util.Date;

public class ReservationModel {

private int reservationID;
private int tourID;
private int userID;
private String reservationstatus;
private int reservationpaymentstatus;
private int numberofattendees;
private Date reservationcreationdate;
private ReservationAttendees attendees;

Now in order to get all reservations based on a user ID I have a method in my DOA class that gets me all the reservations based on User ID field. however ,how can I also get all the tour info that the reservation is for?

Should I create another field in reservations model using the TourModel type? Wouldn't that be against the "HAS A" relationship (the reservation doesn't technically have a tour. it the other way around.

Maybe I'm overthinking this. but I wanted to ask for clarification since I'm not familiar with Object Oriented Design patterns.

public ArrayList<ReservationModel> getReservationByTourID(int tourID) {
                
                ArrayList<ReservationModel> reservations = new ArrayList<ReservationModel>();
                
                
                try {
                        Class.forName("com.mysql.cj.jdbc.Driver");

                        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ArtofTravel", "root",
                                        "2001Space");
                        
                        String query = "select * from reservations where tour_ID = ?;";
                        PreparedStatement stmt = con.prepareStatement(query);
                        stmt.setInt(1, tourID);
                        
                        ResultSet rs = stmt.executeQuery();
                        
                        while (rs.next()) {
                                
                                reservations.add(new ReservationModel(rs.getInt("ID"), rs.getInt("tour_ID"),rs.getInt("user_ID"),rs.getString("reservation_Status"),rs.getInt("reservation_payment_status"),rs.getInt("number_of_attendees"), rs.getDate("reservation_create_date")));
                        
                        } 
                                stmt.close();
                                rs.close();
                                con.close();
                }       
                        catch (Exception e) {
                                e.printStackTrace();
                        }
                        
                        
                return reservations;
                
        }

Aucun commentaire:

Enregistrer un commentaire