📄 staticsmostbyyear.java
字号:
package file1;
/*
* 功能描述:酒店的最受欢迎客房级别按年份统计查询入口
* @Author:黄顺武
* Version:1.3
*/
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.sql.*;
public class StaticsMostByYear extends JPanel {
private String tip = "请输入要查询的年份:";
private DateMonthYear dateMonthYear = null;
private String valueGet = null;
private String[][] bookNumAndGrade = null;
private String[][] mostPopular = null;
private IdentityDate identityDate=new IdentityDate();//实例化对两个日期进行相关性判断的类对象
public StaticsMostByYear() {
dateMonthYear = new DateMonthYear();
valueGet = dateMonthYear.getValueGet(tip);
if (valueGet == null) {
return;
}
try {
int year =Integer.valueOf(valueGet);
DBConnection con = new DBConnection();
String query = "select bookNum,hNo,dateIn,hGrade from HUseBook order by hGrade";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
crs.getInt(1);
crs.getString(2);
String dateIn = crs.getString(3);
crs.getString(4);
identityDate.setFirstDate(dateIn.trim());
identityDate.setSecondDate(year+"-01");
if (identityDate.isInTheSameYear()==0) {//两个日期在同一个年份
count++;
}
}
if (count == 0) {//该年份没有客房出租记录,程序将按原路返回程序控制点
JOptionPane.showMessageDialog(null, "该年份没有客房出租记录!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
bookNumAndGrade = new String[count][2];
count = 0;
crs.beforeFirst();
while (crs.next()) {
int hNum = crs.getInt(1);
crs.getString(2);
String dateIn = crs.getString(3);
String grade = crs.getString(4);
identityDate.setFirstDate(dateIn.trim());
identityDate.setSecondDate(year+"-01");
if (identityDate.isInTheSameYear()==0) {//两个日期在同一个年份
bookNumAndGrade[count][0] = String.valueOf(hNum);
bookNumAndGrade[count][1] = grade;
count++;
}
}
int temp = count;
String bookNumAndGradeTemp[][] = new String[temp][2];
bookNumAndGradeTemp[0][0] = bookNumAndGrade[0][0];
bookNumAndGradeTemp[0][1] = bookNumAndGrade[0][1];
count = 1;
int gradeCount = 1;
for (; count < temp; count++) {
int indicate = 0;
for (int gradeCountTemp = 0; gradeCountTemp < gradeCount; gradeCountTemp++) {// 累加同级别的客房数目
if (bookNumAndGrade[count][1]
.equals(bookNumAndGradeTemp[gradeCountTemp][1])) {
int temp1 = Integer
.valueOf(bookNumAndGradeTemp[gradeCountTemp][0]);
int temp2 = Integer.valueOf(bookNumAndGrade[count][0]);
bookNumAndGradeTemp[gradeCountTemp][0] = String
.valueOf(temp1 + temp2);
indicate = 1;
}
}
if (indicate != 1) {
bookNumAndGradeTemp[gradeCount][0] = bookNumAndGrade[count][0];
bookNumAndGradeTemp[gradeCount][1] = bookNumAndGrade[count][1];
gradeCount++;
}
}
int biggest = 0;
int temp1 = Integer.valueOf(bookNumAndGradeTemp[0][0]);
for (count = 1; count < gradeCount; count++) {
int temp2 = Integer.valueOf(bookNumAndGradeTemp[count][0]);
if (temp1 < temp2) {
biggest = count;
}
}
mostPopular = new String[gradeCount][2];
int base = 0;
for (count = 0; count < gradeCount; count++) {
if (bookNumAndGradeTemp[count][0]
.equals(bookNumAndGradeTemp[biggest][0])) {
mostPopular[base][0] = bookNumAndGradeTemp[count][0];
mostPopular[base][1] = bookNumAndGradeTemp[count][1];
base++;
}
}
String hGrades = null;
int commonCount = Integer.valueOf(mostPopular[0][0]);
if (base > 1) {
StringBuffer sb = new StringBuffer();
for (count = 0; count < base; count++) {
sb.append(mostPopular[count][1] + " ");
}
hGrades = sb.toString();
} else {
hGrades = mostPopular[0][1];
}
JOptionPane.showMessageDialog(null, "该年最受欢迎客房级别为 " + hGrades
+ ",出租的客房数(各)为 " + commonCount + " 间!", "",
JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "输入日期格式必须为如2008的格式!","",JOptionPane.INFORMATION_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -