📄 ticketbook.java~70~
字号:
}
ResultSet rs = dbManager.getResult("select * from flight where flight='"+string[11]+"'");
try{
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "没有返程航班号,请您重新查阅!",
"错误信息", JOptionPane.ERROR_MESSAGE);
jbtflight1.setText("");
return 0;
}
}catch (Exception ex) {
ex.printStackTrace();
}
if(!this.isFlightPresent(string[11],string[10])){
JOptionPane.showMessageDialog(null, "对不起,这一天没有返程航班",
"客户信息", JOptionPane.ERROR_MESSAGE);
jbtflight1.setText("");
return 0;
}
if (seatinformation.isFull(string[11], string[10])) {
JOptionPane.showMessageDialog(this, "你要预定的返程航班已经满座!请您改选其它航班",
"客户信息", JOptionPane.INFORMATION_MESSAGE);
jbtflight1.setText("");
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);
jbtflight1.setText("");
}
return 1;
}
//***************************************************************************
public int multipleflight() {
int i=singleflight();
if(i==0)return 0;
if(jbtflight2.getText().trim().length() == 0){
JOptionPane.showMessageDialog(this, "第二航班号不能为空!", "错误信息",
JOptionPane.ERROR_MESSAGE);
return 0;
}
string[16]=jbtflight2.getText().trim();
String year3 = new String(boxyear1.getSelectedItem().toString().trim());
String month3 = new String(boxmonth1.getSelectedItem().toString().trim());
String day3 = new String(boxday1.getSelectedItem().toString().trim());
string[17] = year3 + month3 + day3;
string[18] = year3 + "年" + month3 + "月" + day3 + "日";
if(!this.isTimeValid(year3,month3,day3)){
JOptionPane.showMessageDialog(null, "对不起,不能预定以前的票了",
"客户信息", JOptionPane.ERROR_MESSAGE);
return 0;
}
ResultSet rs = dbManager.getResult("select * from flight where flight='"+string[16]+"'");
try{
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "没有第二航班号,请您重新查阅!",
"错误信息", JOptionPane.ERROR_MESSAGE);
jbtflight2.setText("");
return 0;
}
}catch (Exception ex) {
ex.printStackTrace();
}
if(!this.isFlightPresent(string[16],string[17])){
JOptionPane.showMessageDialog(null, "对不起,这一天没有第二航班",
"客户信息", JOptionPane.ERROR_MESSAGE);
jbtflight2.setText("");
return 0;
}
if (seatinformation.isFull(string[16], string[17])) {
JOptionPane.showMessageDialog(this, "你要预定的第二航班已经满座!请您改选其它航班",
"客户信息", JOptionPane.INFORMATION_MESSAGE);
jbtflight2.setText("");
return 0;
}
String mstartplace = new String();
String sql = "select start from flight where flight='" +
string[16] + "'";
ResultSet mrs1 = dbManager.getResult(sql);
try{
if (mrs1.next()) {
mstartplace = mrs1.getString(1).trim();
}
}catch (Exception ex) {
ex.printStackTrace();
}
if (! (mstartplace.equals(endplace))) {
JOptionPane.showMessageDialog(this, "第二航班号与第一航班号不匹配\n请重新输入返回航班号!",
"错误信息", JOptionPane.ERROR_MESSAGE);
jbtflight2.setText("");
}
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);
System.out.println(py);
int pm = cal.get(Calendar.MONTH) + 1;
System.out.println(pm);
int pd = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(pd);
if (y < py)return false;
if (y == py) {
if (m < pm)
return false;
else if (m == pm && d < pd)
return false;
}
return true;
}
//The method that makes the time combobox show the present time
//when you first the program
public void setDisplayPresentTime() {
//Get the instance for the class Calendar which used to get the present time
Calendar cal = Calendar.getInstance();
//Because there are two Date classes(java.util.Date--java.sql.Date)
//So we should designate the full name for the java.util.Date class
cal.setTime(new java.util.Date());
//Get the present year,month,day
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;
//Make the time combobox show the present time
this.boxyear.setSelectedItem(year);
boxyear1.setSelectedItem(year);
boxyear2.setSelectedItem(year);
boxmonth.setSelectedItem(month);
boxmonth1.setSelectedItem(month);
boxmonth2.setSelectedItem(month);
boxday.setSelectedItem(day);
boxday1.setSelectedItem(day);
boxday2.setSelectedItem(day);
//We should change the items in the day combobox dynamically
//according to the year and month
updateDay(year, month, boxday);
updateDay(year, month, boxday1);
updateDay(year, month, boxday2);
}
//The method which used to change the items in the day combobox dynamically
//according to the year and month
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() == boxyear || e.getSource() == boxmonth) {
String year = (String) boxyear.getSelectedItem();
String month = (String) boxmonth.getSelectedItem();
updateDay(year, month, boxday);
}
else if (e.getSource() == boxyear1 || e.getSource() == boxmonth1) {
String year = (String) boxyear1.getSelectedItem();
String month = (String) boxmonth1.getSelectedItem();
updateDay(year, month, boxday1);
}
else if (e.getSource() == boxyear2 || e.getSource() == boxmonth2) {
String year = (String) boxyear2.getSelectedItem();
String month = (String) boxmonth2.getSelectedItem();
updateDay(year, month, boxday2);
}
}
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};
//Caculate the first the day of the designate year is "Xing Qi Ji"
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);
}
//The method used to caculate the first the day of the designate year is "Xing Qi Ji"
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 + -