JAVA课程中的事件和预约建模 - 最佳设计方法

问题描述 投票:0回答:1

我是java的新手,正在建立一个基本的旅游预订系统。我有一个看起来像这样的旅游课程。

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;

预订类看起来像

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;

现在,为了获得基于用户ID的所有预订,我在DOA课程中有一个方法,根据用户ID字段获取所有预约。但是,我怎样才能获得预订的所有旅游信息?

我应该使用TourModel类型在预订模型中创建另一个字段吗?这不是违反“HAS A”的关系(预订在技术上没有巡回演出。反过来说。

也许我在想这个。但是我想要澄清,因为我不熟悉面向对象的设计模式。

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;            
}
java oop design-patterns web-applications javabeans
1个回答
0
投票

首先创建一个界面:

   public interface Tour {
    public ArrayList<ReservationModel> getReservations(String userId);
    public ArrayList<TourModel> getTourDetails(ArrayList<ReservationModel> 
      reservationList);
   }

然后在实现类中首先根据用户ID获取预订详细信息,然后您可以通过此预订列表来获取旅游详细信息。

  public class TourImpl implements Tour{
  private ArrayList<ReservationModel> reservationModelList = new 
  ArrayList<ReservationModel>();
  private ArrayList<TourModel> tourModelList = new ArrayList<TourModel>();

 @Override
 public ArrayList<ReservationModel> getReservations(String userId) {
    ReservationModel reservationModel = // Your logic to query in Database and 
   create the Model accordingly;
     reservationModelList.add(reservationModel);
     return reservationModelList;
  }

@Override
public ArrayList<TourModel> getTourDetails(ArrayList<ReservationModel> reservationList ) {
    TourModel tourModel = // Your logic to query in Database and create the Model based on reservation id;
            tourModelList.add(tourModel);
             return tourModelList;
   }

}

和模型类来保存数据

  public class ReservationModel {
  private int reservationID;
  private int tourID;
  private int userID;
  private String reservationstatus;
  public ReservationModel(int reservationID, int tourID, int userID, String 
  reservationstatus) {
    this.reservationID = reservationID;
    this.tourID = tourID;
    this.userID = userID;
    this.reservationstatus = reservationstatus;
  }
}


  public class TourModel {
  private int tourId;
  private String tourname;
  private String tourlocation;
  private String country;
  public TourModel(int tourId, String tourname, String tourlocation, String 
  country) {
   this.tourId = tourId;
   this.tourname = tourname;
   this.tourlocation = tourlocation;
   this.country = country;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.