📄 ticketbook.java
字号:
try {
if (rs1.next()) {
startplace = rs1.getString(1).trim();
endplace = rs1.getString(2).trim();
string[6] = rs1.getString(3).trim();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
string[0] = startplace;
string[1] = endplace;
string[8] = string[5] + "票";
return 1;
}
/**
* 定双程票,返回值为1表示订票条件满足
*/
public int doubleflight() {
int i = singleflight();
if (i == 0)return 0;
if (jbtflightTextField1.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "返程航班号不能为空!", "错误信息",
JOptionPane.ERROR_MESSAGE);
return 0;
}
string[11] = jbtflightTextField1.getText().trim();
String year2 = new String(boxyearComboBox1.getSelectedItem().toString().trim());
String month2 = new String(boxmonthComboBox1.getSelectedItem().toString().trim());
String day2 = new String(boxdayComboBox1.getSelectedItem().toString().trim());
string[10] = year2 + month2 + day2;
string[9] = year2 + "年" + month2 + "月" + day2 + "日";
if (!this.isTimeValid(year2, month2, day2)) {
JOptionPane.showMessageDialog(null, "对不起,不能预定以前的票了",
"客户信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
ResultSet rs = dbManager.getResult("select * from flight where flight='" +
string[11] + "'");
try {
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "没有返程航班号,请您重新查阅!",
"错误信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
if (!this.isFlightPresent(string[11], string[10])) {
JOptionPane.showMessageDialog(null, "对不起,这一天没有返程航班",
"客户信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
if (seatinformation.isFull(string[11], string[10])) {
JOptionPane.showMessageDialog(this, "你要预定的返程航班已经满座!请您改选其它航班",
"客户信息", JOptionPane.INFORMATION_MESSAGE);
return 0;
}
String dstartplace = new String();
String dendplace = new String();
String sql = "select start,destination from flight where flight='" +
string[11] + "'";
ResultSet drs1 = dbManager.getResult(sql);
try {
if (drs1.next()) {
dstartplace = drs1.getString(1).trim();
dendplace = drs1.getString(2).trim();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
if (! (dstartplace.equals(endplace) && dendplace.equals(startplace))) {
JOptionPane.showMessageDialog(this, "返程航班号与第一航班号不匹配\n请重新输入返回航班号!",
"错误信息", JOptionPane.ERROR_MESSAGE);
}
return 1;
}
/**
* 判断所填机票时间是否大于当前时间
*/
private boolean isTimeValid(String year, String month, String day) {
int y = Integer.parseInt(year);
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
//Get the present time
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
int py = cal.get(Calendar.YEAR);
int pm = cal.get(Calendar.MONTH) + 1;
int pd = cal.get(Calendar.DAY_OF_MONTH);
if (y < py)return false;
if (y == py) {
if (m < pm)
return false;
else if (m == pm && d < pd)
return false;
}
return true;
}
/**
* 初始设置时间组合框的时间为当前日期
*/
public void setDisplayPresentTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
String year = String.valueOf(cal.get(Calendar.YEAR));
String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
if (month.length() < 2) month = "0" + month;
String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
if (day.length() < 2) day = "0" + day;
this.boxyearComboBox.setSelectedItem(year);
boxyearComboBox1.setSelectedItem(year);
boxyearComboBox2.setSelectedItem(year);
boxmonthComboBox.setSelectedItem(month);
boxmonthComboBox1.setSelectedItem(month);
boxmonthComboBox2.setSelectedItem(month);
boxdayComboBox.setSelectedItem(day);
boxdayComboBox1.setSelectedItem(day);
boxdayComboBox2.setSelectedItem(day);
updateDay(year, month, boxdayComboBox);
updateDay(year, month, boxdayComboBox1);
updateDay(year, month, boxdayComboBox2);
}
/**
* 根据年份和月份设置日期组合框的条目
*/
private void updateDay(String year, String month, JComboBox jcb) {
//There are 30 days in the months 4,6,9,11
if (month.equals("04") || month.equals("06") || month.equals("09") ||
month.equals("11")) {
//jcb.getItemCount() == 31 means that there are 31 days in the day combobox,
//but exactly it is 30 days,so we should remove the 31st day from the day combobox
if (jcb.getItemCount() == 31)
jcb.removeItem("31");
else if (jcb.getItemCount() == 29)
jcb.addItem("30");
else if (jcb.getItemCount() == 28) {
jcb.addItem("29");
jcb.addItem("30");
}
}
//There are 28 or 29 days in the month 2
else if (month.equals("02")) {
int years = Integer.parseInt(year);
//The year is leap year
if ( (years % 400 == 0) || (years % 4 == 0 && years % 100 != 0)) {
if (jcb.getItemCount() == 31) {
jcb.removeItem("30");
jcb.removeItem("31");
}
else if (jcb.getItemCount() == 30) {
jcb.removeItem("30");
}
else if (jcb.getItemCount() == 28) {
jcb.addItem("29");
}
}
//The year is not leap year
else {
if (jcb.getItemCount() == 29) {
jcb.removeItem("29");
}
else if (jcb.getItemCount() == 30) {
jcb.removeItem("29");
jcb.removeItem("30");
}
else if (jcb.getItemCount() == 31) {
jcb.removeItem("29");
jcb.removeItem("30");
jcb.removeItem("31");
}
}
}
//There are 31 days in the left months
else {
if (jcb.getItemCount() == 28) {
jcb.addItem("29");
jcb.addItem("30");
jcb.addItem("31");
}
else if (jcb.getItemCount() == 29) {
jcb.addItem("30");
jcb.addItem("31");
}
else if (jcb.getItemCount() == 30) {
jcb.addItem("31");
}
}
}
public void itemStateChanged(ItemEvent e) {
//Change the items in the day combobox dynamically
//according to the year and month that you choose
if (e.getSource() == boxyearComboBox || e.getSource() == boxmonthComboBox) {
String year = (String) boxyearComboBox.getSelectedItem();
String month = (String) boxmonthComboBox.getSelectedItem();
updateDay(year, month, boxdayComboBox);
}
else if (e.getSource() == boxyearComboBox1 || e.getSource() == boxmonthComboBox1) {
String year = (String) boxyearComboBox1.getSelectedItem();
String month = (String) boxmonthComboBox1.getSelectedItem();
updateDay(year, month, boxdayComboBox1);
}
else if (e.getSource() == boxyearComboBox2 || e.getSource() == boxmonthComboBox2) {
String year = (String) boxyearComboBox2.getSelectedItem();
String month = (String) boxmonthComboBox2.getSelectedItem();
updateDay(year, month, boxdayComboBox2);
}
}
/**
* 判断航班在指定日期是否飞行
*/
private boolean isFlightPresent(String flightNum, String day) {
String sqlString = "select week from flight where flight='" +
flightNum + "' ";
ResultSet rs = dbManager.getResult(sqlString);
String week = "";
try {
if (rs.next()) {
week = rs.getString(1);
}
}
catch (Exception e) {
e.printStackTrace();
}
String c = parseWeek(day);
int flag = 0;
for (int i = 0; i < week.length(); i++) {
String w = week.substring(i, i + 1);
if (c.equals(w)) {
flag = 1;
break;
}
}
return flag == 1 ? true : false;
}
/**
* 根据日期判定该日期是星期几
*/
private String timeToWeek(String year, String month, String day) {
int sum = 0;
int y = Integer.parseInt(year);
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int[] dayOfMonth = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int firstDayOfYear = firstDay(y);
for (int i = 1; i < m; i++) {
sum = sum + dayOfMonth[i];
}
sum = sum + (d - 1) + firstDayOfYear;
//If month is over February and the designate year is leap year,
//the total days should be add one
if ( (m >= 2) && ( (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)))
sum++;
int week = 0;
//The weekday for the designate day is:
int x = sum % 7;
switch (x) {
case 1:
week = 1;
break;
case 2:
week = 2;
break;
case 3:
week = 3;
break;
case 4:
week = 4;
break;
case 5:
week = 5;
break;
case 6:
week = 6;
break;
case 0:
week = 7;
break;
}
return String.valueOf(week);
}
/**
*用于判定给定年份的第一天是星期几
*/
private int firstDay(int year) {
int a, b;
if (year <= 2000) {
a = 2000 - year;
b = 6 - (a + a / 4 - a / 100 + a / 400) % 7;
return b;
}
else {
a = year - 2000;
b = (a + 1 + (a - 1) / 4 - (a - 1) / 100 + (a - 1) / 400) % 7 + 6;
return b % 7;
}
}
/**
* 解析订票日是星期几
*/
private String parseWeek(String date) {
String year = date.substring(0, 4);
String month = date.substring(4, 6);
String day = date.substring(6, 8);
String week = timeToWeek(year, month, day);
return week;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -