📄 staticsbyyear.java
字号:
package file1;
/*
* 功能描述:酒店的运营情况按年份统计查询入口
* @Author:黄顺武
* Version:1.3
*/
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.util.StringTokenizer;
import java.sql.*;
public class StaticsByYear extends JPanel {
private String tip = "请输入要查询的年份:";
private DateMonthYear dateMonthYear = null;
private String valueGet = null;
private float result = 0;
private String[][] numAndConsume = null;
private String[] hNoS = null;
private IdentityDate identityDate=new IdentityDate();//实例化对两个日期进行相关性判断的类对象
public StaticsByYear() {
dateMonthYear = new DateMonthYear();
valueGet = dateMonthYear.getValueGet(tip);
if (valueGet == null) {
return;
}
try {
int year = -1;
try {
year = Integer.valueOf(valueGet);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "年份必须为如:2007的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
}
if (year <= 0) {
JOptionPane.showMessageDialog(null, "年份必须为如:2007的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
DBConnection con = new DBConnection();
String query = "select bookNum,hNo,consume,dateOut from HUseBook";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
crs.getInt(1);
crs.getString(2);
crs.getFloat(3);
String dateOut = crs.getString(4);//读取该入住记录的退房时间
identityDate.setFirstDate(dateOut.trim());
identityDate.setSecondDate(year+"-01");
if (identityDate.isInTheSameYear()==0) {//两个日期在同一年
count++;
}
}
numAndConsume = new String[count][2];
hNoS = new String[count];
crs.beforeFirst();
count = 0;
while (crs.next()) {
int numTemp = crs.getInt(1);
String hNoTemp = crs.getString(2);
float consume = crs.getFloat(3);
String dateOut = crs.getString(4);
identityDate.setFirstDate(dateOut.trim());
identityDate.setSecondDate(year+"-01");
if (identityDate.isInTheSameYear()==0) {
numAndConsume[count][0] = String.valueOf(numTemp);//读取订房数量
hNoS[count] = hNoTemp;
numAndConsume[count][1] = String.valueOf(consume);//读取该客户本次入住的其它消费金额
count++;
}
}
crs = null;
int temp = count;
count = 0;
float price = 0;
int num = 0;
float consume = 0;
float consumeTotal = 0;
for (; count < temp; count++) {
if (hNoS[count].indexOf(",") == -1) {
query = "select Sprice from House,HGrade where HouseNo='"
+ hNoS[count] + "' and hGrade=grade";
crs = con.getResultSet(query);
if (crs.next()) {
price = crs.getFloat(1);
num = Integer.valueOf(numAndConsume[count][0]);
consume = Float.valueOf(numAndConsume[count][1]);
consumeTotal += consume;
result += ((num * price) + consume);
}
} else {
StringTokenizer st = new StringTokenizer(hNoS[count], ",");
int seed = 0;
String sHNo = null;
while (st.hasMoreTokens()) {
sHNo = st.nextToken();
if (seed == 0) {
break;
}
}
query = "select Sprice from House,HGrade where HouseNo='"
+ sHNo + "' and hGrade=grade";
crs = con.getResultSet(query);
if (crs.next()) {
price = crs.getFloat(1);
num = Integer.valueOf(numAndConsume[count][0]);
consume = Float.valueOf(numAndConsume[count][1]);
consumeTotal += consume;
result += ((num * price) + consume);
}
}
}
JOptionPane.showMessageDialog(null, "该年总收入为 " + result
+ " 元!其中酒水消费金额为 " + consumeTotal + " 元,住房费为 "
+ (result - consumeTotal) + " 元.", "",
JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return;
} 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 + -