Java实现Web航空订票系统(servlet+jdbc+jsp+mysql)

String transferCity = depatureFlight.getArrivalAirportCity();

List arrivalList = DaoFactory.getFlightInfoDaoInstance(DBName.ABS).findByTransferArrivalAirport(transferCity, arrivalCity, departureDate, depatureTime);

for (FlightInfo arrivalFlight : arrivalList) {

List item = new ArrayList();

if(depatureFlight.getAirlineCode().equals(arrivalFlight.getAirlineCode())){

double discount = DaoFactory.getAirlineDaoInstance(DBName.ABS).findByCode(depatureFlight.getAirlineCode()).getDiscount();

arrivalFlight.setFare(arrivalFlight.getFare() * discount);

}

item.add(depatureFlight);

item.add(arrivalFlight);

list.add(item);

}

}else {

System.err.println(depatureFlight.getArrivalAirportCity() + depatureFlight.getArrivalAirport() + “:没有找到机场信息”);

}

}

return list;

}

public static boolean checkDate(String date) {

String regex= “^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))”;

return Pattern.compile(regex).matcher(date).matches();

}

}

OrderAction


package com.abs.action;

import java.sql.Date;

import java.sql.Time;

import java.util.List;

import com.abs.db.DBName;

import com.abs.factory.DaoFactory;

import com.abs.model.Flight;

import com.abs.model.FlightInfo;

import com.abs.model.Orders;

import com.abs.model.Passenger;

import com.abs.model.Seat;

import com.abs.model.Trip;

public class OrderAction {

public static Orders createOrder(List flightInfos, List passengers, String[] seatTypeList, String contactName, String contactPhone) throws Exception {

// 生成订单编号

Orders.addIdCounter();

Orders order = null;

double total = 0;

for (FlightInfo flightInfo : flightInfos) {

total += flightInfo.getFare();

}

total = total * passengers.size(); // 计算总费用

for (Passenger passenger : passengers) {

DaoFactory.getPassengerDaoInstance(DBName.ABS).add(passenger);

}

for (FlightInfo flightInfo : flightInfos) {

for (Passenger passenger : passengers) {

Trip trip = new Trip();

trip.setId(Trip.getIdCounter());

trip.setFlightInfoID(flightInfo.getId());

trip.setFare(flightInfo.getFare());

trip.setPassport(passenger.getPassport());

trip.setSeatID(OrderAction.orderSeat(flightInfo, passenger, seatTypeList[passengers.indexOf(passenger)])); // 分配座位

flightInfo.setAirplaneEmptySeats(flightInfo.getAirplaneEmptySeats() - 1); // 空闲座位数 - 1

DaoFactory.getFlightInfoDaoInstance(DBName.ABS).addPassenger(flightInfo.getId()); // 空闲座位数 - 1

DaoFactory.getTripDaoInstance(DBName.ABS).add(trip);

Orders orders = new Orders();

orders.setId(Orders.getIdCounter()); // 同一个订单号,代表为同一个订单

orders.setTripID(trip.getId()); // 每个Orders item 与一个 Trip 一一对应

java.util.Date date = new java.util.Date();

orders.setCreateDate(new Date(date.getTime()));

orders.setCreateTime(new Time(date.getTime()));

orders.setTotalFare(total);

orders.setContactName(contactName);

orders.setContactPhone(contactPhone);

DaoFactory.getOrdersDaoInstance(DBName.ABS).add(orders);

order = orders; // 返回一个 Orders item用于显示

}

}

return order;

}

public static int orderSeat(FlightInfo flightInfo, Passenger passenger, String seatType) throws Exception {

int seatID = -1;

String airlineCode = flightInfo.getAirlineCode();

Flight flight = null;

// airline_1 MU(东方航空), airline_2 CZ(南方航空), airline_3 CA(中国国航)

if(airlineCode.equals(“MU”)){ // airline_1 MU(东方航空)

flight = DaoFactory.getFlightDaoInstance(DBName.AIRLINE_1).findByID(flightInfo.getFlightID());

List emptySeats = DaoFactory.getSeatDaoInstance(DBName.AIRLINE_1).findEmptySeatByFlightID(flight.getId());

if (emptySeats.size() > 0) {

Seat seat = null;

for (Seat emptySeat : emptySeats) {

if(emptySeat.getType() == seatType){

seat = emptySeat;

break;

}

}

if(seat == null){

seat = emptySeats.get(0);

}

seat.setPassport(passenger.getPassport());

DaoFactory.getSeatDaoInstance(DBName.AIRLINE_1).modify(seat);

seatID = seat.getRelativeID();

}

}else if (airlineCode.equals(“CZ”)) { // airline_2 CZ(南方航空)

flight = DaoFactory.getFlightDaoInstance(DBName.AIRLINE_2).findByID(flightInfo.getFlightID());

List emptySeats = DaoFactory.getSeatDaoInstance(DBName.AIRLINE_2).findEmptySeatByFlightID(flight.getId());

if (emptySeats.size() > 0) {

Seat seat = null;

for (Seat emptySeat : emptySeats) {

if(emptySeat.getType() == seatType){

seat = emptySeat;

}

}

if(seat == null){

seat = emptySeats.get(0);

}

seat.setPassport(passenger.getPassport());

DaoFactory.getSeatDaoInstance(DBName.AIRLINE_2).modify(seat);

seatID = seat.getRelativeID();

}

}else { // airline_3 CA(中国国航)

flight = DaoFactory.getFlightDaoInstance(DBName.AIRLINE_3).findByID(flightInfo.getFlightID());

List emptySeats = DaoFactory.getSeatDaoInstance(DBName.AIRLINE_3).findEmptySeatByFlightID(flight.getId());

if (emptySeats.size() > 0) {

Seat seat = null;

for (Seat emptySeat : emptySeats) {

if(emptySeat.getType() == seatType){

seat = emptySeat;

}

}

if(seat == null){

seat = emptySeats.get(0);

}

seat.setPassport(passenger.getPassport());

DaoFactory.getSeatDaoInstance(DBName.AIRLINE_3).modify(seat);

seatID = seat.getRelativeID();

}

}

return seatID;

}

}

AirlineDaoImpl


package com.abs.dao.impl;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import com.abs.dao.AirlineDao;

import com.abs.db.DBConnection;

import com.abs.db.DBName;

import com.abs.model.Airline;

public class AirlineDaoImpl implements AirlineDao {

private Connection conn = null;

private PreparedStatement pstmt = null;

public AirlineDaoImpl(Connection conn) {

// TODO Auto-generated constructor stub

this.conn = conn;

}

@Override

public boolean add(Airline airline) throws Exception {

// TODO Auto-generated method stub

String sql = “insert into airline(code, name, discount) values(?,?,?)”;

this.pstmt = this.conn.prepareStatement(sql);

this.pstmt.setString(1, airline.getCode());

this.pstmt.setString(2, airline.getName());

this.pstmt.setDouble(3, airline.getDiscount());

int update = this.pstmt.executeUpdate();

this.pstmt.close();

if(update > 0){

return true;

}else{

return false;

}

}

@Override

public Airline findByCode(String code) throws Exception {

// TODO Auto-generated method stub

String sql = “select * from airline where code=?”;

this.pstmt = this.conn.prepareStatement(sql);

this.pstmt.setString(1, code);

ResultSet resultSet = this.pstmt.executeQuery();

Airline airline = null;

if(resultSet.next()){

airline = new Airline();

airline.setCode(resultSet.getString(1));

airline.setName(resultSet.getString(2));

airline.setDiscount(resultSet.getDouble(3));

}

this.pstmt.close();

return airline;

}

@Override

public List findAll() throws Exception {

// TODO Auto-generated method stub

String sql = “select * from airline”;

this.pstmt = this.conn.prepareStatement(sql);

ResultSet resultSet = this.pstmt.executeQuery();

List list = new ArrayList<>();

Airline airline = null;

while(resultSet.next()){

airline = new Airline();

airline.setCode(resultSet.getString(1));

airline.setName(resultSet.getString(2));

airline.setDiscount(resultSet.getDouble(3));

list.add(airline);

}

this.pstmt.close();

return list;

}

public static void main(String args[]) throws Exception {

Airline airline = new Airline();

airline.setCode(“MU”);

airline.setName(“东方航空”);

airline.setDiscount(0.9);

new AirlineDaoImpl(new DBConnection().getConnection(DBName.AIRLINE_1)).add(airline);

}

}

FlightDaoImpl


package com.abs.dao.impl;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.sql.*;

import java.util.List;

import com.abs.dao.FlightDao;

import com.abs.db.DBName;

import com.abs.factory.DaoFactory;

import com.abs.model.Flight;

public class FlightDaoImpl implements FlightDao {

private Connection conn = null;

private PreparedStatement pstmt = null;

public FlightDaoImpl(Connection conn) {

// TODO Auto-generated constructor stub

this.conn = conn;

}

@Override

public boolean add(Flight flight) throws Exception {

// TODO Auto-generated method stub

String sql = "insert into flight(id, airlineCode, number, depatureDate, depatureTime, arrivalDate, arrivalTime, fare, "

  • “depatureAirport, arrivalAirport, airplaneName, airplaneType) values(?,?,?,?,?,?,?,?,?,?,?,?)”;

this.pstmt = this.conn.prepareStatement(sql);

this.pstmt.setInt(1, flight.getId());

this.pstmt.setString(2, flight.getAirlineCode());

this.pstmt.setInt(3, flight.getNumber());

this.pstmt.setDate(4, flight.getDepatureDate());

this.pstmt.setTime(5, flight.getDepatureTime());

this.pstmt.setDate(6, flight.getArrivalDate());

this.pstmt.setTime(7, flight.getArrivalTime());

this.pstmt.setDouble(8, flight.getFare());

this.pstmt.setString(9, flight.getDepatureAirport());

this.pstmt.setString(10, flight.getArrivalAirport());

this.pstmt.setString(11, flight.getAirplaneName());

this.pstmt.setString(12, flight.getAirplaneType());

int update = this.pstmt.executeUpdate();

this.pstmt.close();

if(update > 0){

return true;

}else {

return false;

}

}

@Override

public Flight findByID(int id) throws Exception {

// TODO Auto-generated method stub

String sql = “select * from flight where id=?”;

this.pstmt = this.conn.prepareStatement(sql);

this.pstmt.setInt(1, id);

ResultSet resultSet = this.pstmt.executeQuery();

Flight flight = null;

if(resultSet.next()){

flight = new Flight();

flight.setId(resultSet.getInt(1));

flight.setAirlineCode(resultSet.getString(2));

flight.setNumber(resultSet.getInt(3));

flight.setDepatureDate(resultSet.getDate(4));

flight.setDepatureTime(resultSet.getTime(5));

flight.setArrivalDate(resultSet.getDate(6));

flight.setArrivalTime(resultSet.getTime(7));

flight.setFare(resultSet.getDouble(8));

flight.setDepatureAirport(resultSet.getString(9));

flight.setArrivalAirport(resultSet.getString(10));

flight.setAirplaneName(resultSet.getString(11));

flight.setAirplaneType(resultSet.getString(12));

}

this.pstmt.close();

return flight;

}

@Override

public List findAll() throws Exception {

// TODO Auto-generated method stub

String sql = “select * from flight”;

this.pstmt = this.conn.prepareStatement(sql);

ResultSet resultSet = this.pstmt.executeQuery();

List list = new ArrayList<>();

Flight flight = null;

while(resultSet.next()){

flight = new Flight();

flight.setId(resultSet.getInt(1));

flight.setAirlineCode(resultSet.getString(2));

flight.setNumber(resultSet.getInt(3));

flight.setDepatureDate(resultSet.getDate(4));

flight.setDepatureTime(resultSet.getTime(5));

flight.setArrivalDate(resultSet.getDate(6));

flight.setArrivalTime(resultSet.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值