📄 ticketbook.java
字号:
package flight.ticketbook;
import flight.assist.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.border.*;
/*
*订票
*/
public class TicketBook
extends JFrame
implements ActionListener, ItemListener {
DataBaseManager dbManager = DataBaseManager.getInstance();
private String[] string = new String[17];
/*
string[0]----------------起飞城市
string[1]----------------到达城市
string[2]----------------航班日期,格式为:****年**月**日
string[3]----------------航班日期,格式为:********
string[4]----------------航班号
string[5]----------------类型:单程、双程、联程
string[6]----------------航空公司
string[7]----------------总价
string[8]----------------类型+“票”:单程票、双程票、联程票
string[9]----------------返程航班日期,格式为:****年**月**日
string[10]---------------返程航班日期,格式为:********
string[11]---------------返程航班号
string[12]---------------姓名
string[13]---------------身份证号
string[14]---------------成人票数
string[15]---------------儿童票数
string[16]---------------订单号
*/
private JComboBox boxyearComboBox = new JComboBox();
private JComboBox boxyearComboBox1 = new JComboBox();
private JComboBox boxyearComboBox2 = new JComboBox();
private JComboBox boxmonthComboBox = new JComboBox();
private JComboBox boxmonthComboBox1 = new JComboBox();
private JComboBox boxmonthComboBox2 = new JComboBox();
private JComboBox boxdayComboBox1 = new JComboBox();
private JComboBox boxdayComboBox2 = new JComboBox();
private JComboBox boxdayComboBox = new JComboBox();
private String[] year = {
"2006", "2007", "2008"};
private String[] month = {
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
private String[] day = {
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31"};
private JTextField jbtflightTextField = new JTextField(10);
private JTextField jbtflightTextField1 = new JTextField(10);
private JTextField jbtflightTextField2 = new JTextField(10);
private JRadioButton jbrsingle, jbrdouble, jbrmultiple;
private ButtonGroup jbrButtonGroup = new ButtonGroup();
private JButton bookButton = new JButton("机 票 预 定");
private JButton returnButton = new JButton("返回主页面");
private JPanel s1, s2;
private JLabel jblsecondflightLabel = new JLabel(" 第二航班号");
private JLabel jblreturnflightLabel = new JLabel(" 返回航班");
private SeatInfo seatinformation = new SeatInfo();
int flag = 0;
int pflag = 0;
String startplace = new String();
String endplace = new String();
public TicketBook() {
string[5] = "单程";
for (int i = 0; i < year.length; i++)
boxyearComboBox.addItem(year[i]);
for (int i = 0; i < year.length; i++)
boxyearComboBox1.addItem(year[i]);
for (int i = 0; i < year.length; i++)
boxyearComboBox2.addItem(year[i]);
for (int i = 0; i < month.length; i++)
boxmonthComboBox.addItem(month[i]);
for (int i = 0; i < month.length; i++)
boxmonthComboBox1.addItem(month[i]);
for (int i = 0; i < month.length; i++)
boxmonthComboBox2.addItem(month[i]);
for (int i = 0; i < day.length; i++)
boxdayComboBox.addItem(day[i]);
for (int i = 0; i < day.length; i++)
boxdayComboBox1.addItem(day[i]);
for (int i = 0; i < day.length; i++)
boxdayComboBox2.addItem(day[i]);
JPanel p0 = new JPanel();
p0.setVisible(false);
display(p0);
p0.setLayout(new FlowLayout(FlowLayout.LEFT));
p0.add(new JLabel(" 返回日期"));
p0.add(boxyearComboBox1);
p0.add(new JLabel("年"));
p0.add(boxmonthComboBox1);
p0.add(new JLabel("月"));
p0.add(boxdayComboBox1);
p0.add(new JLabel("日"));
//Panel for multiple
JPanel p1 = new JPanel();
p1.setVisible(false);
display1(p1);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(new JLabel("第二出发日期:"));
p1.add(boxyearComboBox2);
p1.add(new JLabel("年"));
p1.add(boxmonthComboBox2);
p1.add(new JLabel("月"));
p1.add(boxdayComboBox2);
p1.add(new JLabel("日"));
//Panel starttime
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
//JLabel starttime
p3.add(new JLabel("第一出发日期:"));
p3.add(boxyearComboBox);
p3.add(new JLabel("年"));
p3.add(boxmonthComboBox);
p3.add(new JLabel("月"));
p3.add(boxdayComboBox);
p3.add(new JLabel("日"));
//Panel style
JPanel p5 = new JPanel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
//style
p5.add(new JLabel("机票类型: "));
p5.add(jbrsingle = new JRadioButton("单程", true));
p5.add(jbrdouble = new JRadioButton("往返", false));
p5.add(jbrmultiple = new JRadioButton("联票 ", false));
jbrButtonGroup.add(jbrsingle);
jbrButtonGroup.add(jbrdouble);
jbrButtonGroup.add(jbrmultiple);
JPanel p6 = new JPanel();
p6.setLayout(new FlowLayout(FlowLayout.LEFT));
p6.add(bookButton);
p6.add(returnButton);
JPanel p8 = new JPanel();
p8.setLayout(new FlowLayout(FlowLayout.LEFT));
//the firstflight
p8.add(new JLabel("第一航班号: "));
p8.add(jbtflightTextField);
p8.add(jblreturnflightLabel);
p8.add(jbtflightTextField1);
p8.add(jblsecondflightLabel);
p8.add(jbtflightTextField2);
jblsecondflightLabel.setVisible(false);
jbtflightTextField1.setVisible(false);
jbtflightTextField2.setVisible(false);
jblreturnflightLabel.setVisible(false);
JPanel p7 = new JPanel();
p7.setBorder(new MatteBorder(new ImageIcon("border.gif")));
p7.setLayout(null);
p7.add(p0);
p7.add(p1);
p7.add(p3);
p7.add(p5);
p7.add(p6);
p7.add(p8);
p3.reshape(50, 30, 350, 30);
p0.reshape(50, 70, 350, 30);
p1.reshape(50, 70, 350, 30);
p8.reshape(50, 110, 350, 30);
p5.reshape(50, 150, 350, 40);
p6.reshape(120, 200, 250, 30);
getContentPane().add(p7, BorderLayout.CENTER);
getContentPane().setBackground(new Color(255, 255, 230));
bookButton.addActionListener(this);
returnButton.addActionListener(this);
jbrsingle.addActionListener(this);
jbrdouble.addActionListener(this);
jbrmultiple.addActionListener(this);
boxyearComboBox.addItemListener(this);
boxyearComboBox1.addItemListener(this);
boxyearComboBox2.addItemListener(this);
boxmonthComboBox.addItemListener(this);
boxmonthComboBox1.addItemListener(this);
boxmonthComboBox2.addItemListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TicketBook.this.setVisible(false);
TicketBook.this.dispose();
}
}
);
setDisplayPresentTime();
this.setSize(430, 300);
this.setResizable(false);
this.setTitle("国内机票实时速定");
}
/**
* 将p赋给s1
*/
public void display(JPanel p) {
s1 = p;
}
/**
* 将p赋给s2
*/
public void display1(JPanel p) {
s2 = p;
}
String otherchoice = new String("");
String dotherchoice = new String("");
String motherchoice = new String("");
boolean isFull = false;
public void actionPerformed(ActionEvent e) {
//book the ticket
if (e.getSource() == bookButton) {//订票
int conditionSatisfied = 0;
if (jbrsingle.isSelected()) {//单程
conditionSatisfied = singleflight();
}
else if (jbrdouble.isSelected()) {//双程
conditionSatisfied = doubleflight();
}
else if (jbrmultiple.isSelected()) {//联程
JOptionPane.showMessageDialog(this, "联程订票功能尚未开通!",
"提示信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (conditionSatisfied == 1) {//条件满足,显示用户信息输入窗口
ClientFrame clientFrame = new ClientFrame();
clientFrame.initTextField(string);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //屏幕大小
clientFrame.setLocation( (d.width - clientFrame.getSize().width) /
2,
(d.height - clientFrame.getSize().height) /
2);
clientFrame.show();
}
}
if (e.getSource() == returnButton) {//返回
this.setVisible(false);
this.dispose();
}
if (e.getSource() == jbrsingle) {
string[11] = jbtflightTextField1.getText().trim();
string[5] = "单程";
s1.setVisible(false);
s2.setVisible(false);
jblsecondflightLabel.setVisible(false);
jbtflightTextField1.setVisible(false);
jblreturnflightLabel.setVisible(false);
jbtflightTextField2.setVisible(false);
}
if (e.getSource() == jbrdouble) {
string[5] = "往返";
s2.setVisible(false);
s1.setVisible(true);
jblreturnflightLabel.setVisible(true);
jbtflightTextField1.setVisible(true);
jblsecondflightLabel.setVisible(false);
jbtflightTextField2.setVisible(false);
}
if (e.getSource() == jbrmultiple) {
string[5] = "联程";
s1.setVisible(false);
jblreturnflightLabel.setVisible(false);
jbtflightTextField1.setVisible(false);
s2.setVisible(true);
jblsecondflightLabel.setVisible(true);
jbtflightTextField2.setVisible(true);
}
}
/**
* 定单程票,返回值为1表示订票条件满足
*/
public int singleflight() {
if (jbtflightTextField.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "航班号不能为空!",
"错误信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
string[4] = jbtflightTextField.getText().trim();
String year1 = new String(boxyearComboBox.getSelectedItem().toString().trim());
String month1 = new String(boxmonthComboBox.getSelectedItem().toString().trim());
String day1 = new String(boxdayComboBox.getSelectedItem().toString().trim());
string[3] = year1 + month1 + day1;
string[2] = year1 + "年" + month1 + "月" + day1 + "日";
if (!this.isTimeValid(year1, month1, day1)) {
JOptionPane.showMessageDialog(null, "对不起,不能预定以前的票了",
"客户信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
ResultSet rs = dbManager.getResult("select * from flight where flight='" +
string[4] + "'");
try {
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "没有第一航班号,请您重新查阅!",
"错误信息", JOptionPane.ERROR_MESSAGE);
jbtflightTextField.setText("");
return 0;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
if (!this.isFlightPresent(string[4], string[3])) {
JOptionPane.showMessageDialog(null, "对不起,这一天没有这个航班",
"客户信息", JOptionPane.ERROR_MESSAGE);
jbtflightTextField.setText("");
return 0;
}
if (seatinformation.isFull(string[4], string[3])) {
JOptionPane.showMessageDialog(this, "你要预定的航班已经满座!请您改选其它航班",
"客户信息", JOptionPane.INFORMATION_MESSAGE);
jbtflightTextField.setText("");
return 0;
}
String sql =
"select start,destination,airFirm from flight where flight='" +
string[4] + "'";
ResultSet rs1 = dbManager.getResult(sql);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -