📄 ticketrefund.java
字号:
nameTextField.setText("");
flightInfoTextField.setText("");
childNumTextField.setText("");
adultNumTextField.setText("");
originCostTextField.setText("");
refundCostTextField.setText("");
timeTextField1.setText("");
timeTextField2.setText("");
childRefundNumTextField.setText("0");
adultRefundNumTextField.setText("0");
timeLabel1.setText("出发时间");
childRefundNumTextField.setEditable(true);
adultRefundNumTextField.setEditable(true);
timeLabel2.setVisible(false);
timeTextField2.setVisible(false);
}
/**
* 退票
*/
private void refund() {
String bookID = bookIDTextField.getText().trim();
if (bookID.length() == 0) {
JOptionPane.showMessageDialog(null, "订单号不能为空",
"错误信息", JOptionPane.ERROR_MESSAGE);
return;
}
String customerid = customerIDTextField.getText().trim();
if (customerid.length() == 0) {
JOptionPane.showMessageDialog(null, "身份证号不能为空",
"错误信息", JOptionPane.ERROR_MESSAGE);
return;
}
boolean isValid = getClientInfo(bookID, customerid);
if (isValid == false) {
rewrite();
return;
}
else {
String childRefundNum = childRefundNumTextField.getText().trim();
String adultRefundNum = adultRefundNumTextField.getText().trim();
int cRefundNum = Integer.parseInt(childRefundNum);
int aRefundNum = Integer.parseInt(adultRefundNum);
if (cRefundNum == 0 && aRefundNum == 0) {
JOptionPane.showMessageDialog(null, "请输入退票数",
"错误信息", JOptionPane.ERROR_MESSAGE);
return;
}
if (cRefundNum != 0 && cRefundNum > Integer.parseInt(childNum)) {
JOptionPane.showMessageDialog(null, "退票数大于已定票数,请按\"查询\"按钮查看信息",
"错误信息", JOptionPane.ERROR_MESSAGE);
return;
}
if (aRefundNum != 0 && aRefundNum > Integer.parseInt(adultNum)) {
JOptionPane.showMessageDialog(null, "退票数大于已定票数,请按\"查询\"按钮查看信息",
"错误信息", JOptionPane.ERROR_MESSAGE);
return;
}
operationForRefund(bookID, customerid, cRefundNum, aRefundNum);
}
}
/**
* 更新数据库
*/
private void operationForRefund(String bookID, String customerId,
int childRefundNum, int adultRefundNum) {
int newChildNum = Integer.parseInt(childNum) - childRefundNum;
int newAdultNum = Integer.parseInt(adultNum) - adultRefundNum;
float refundCost = caculateRefundCost(childRefundNum, adultRefundNum);
float newCost = Float.parseFloat(cost) - refundCost;
try {
String sql = "update bookInfo set childTickets='" +
String.valueOf(newChildNum) +
"',adultTickets='" + String.valueOf(newAdultNum) +
"',cost='" + String.valueOf(newCost) + "' where bookID='" + bookID +
"' and customerID='" + customerId + "'";
int result = dbManager.updateSql(sql);
if (result > 0) {
int totalReundNum = childRefundNum + adultRefundNum;
seatInfo.refundTicket(flight1, leaveTime1, totalReundNum);
if (flight2.length() != 0)
seatInfo.refundTicket(flight2, leaveTime2, totalReundNum);
if (newChildNum == 0 && newAdultNum == 0)
JOptionPane.showMessageDialog(null,
"恭喜你退票成功!" + "\n" + "该订单号已作废!" + "\n" +
"你将获得" + refundCost +
"的退票钱" + "\n" + "欢迎你再次选择我们!",
"退票成功", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"恭喜你退票成功!" + "\n" + "你现在剩余" +
newChildNum +
"张儿童票和" + newAdultNum + "张成人票" + "\n" +
"你将获得" + refundCost +
"的退票钱" + "\n" + "欢迎你再次选择我们!", "退票成功",
JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null, "数据库操作失败", "数据库操作失败",
JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 计算退票金额
*/
private float caculateRefundCost(int childTuiPiaoShu, int adultTuiPiaoShu) {
float tuiPiaoCost = 0;
try {
String sqlString =
"select childFare,adultFare from flight where flight='" + flight1 +
"'";
ResultSet rs = dbManager.getResult(sqlString);
float childFare1 = 0;
float adultFare1 = 0;
while (rs.next()) {
childFare1 = rs.getFloat(1);
adultFare1 = rs.getFloat(2);
}
float childFare2 = 0;
float adultFare2 = 0;
if (flight2.length() != 0) {
String sqlString2 =
"select childFare,adultFare from flight where flight='" + flight2 +
"'";
ResultSet rs2 = dbManager.getResult(sqlString2);
while (rs2.next()) {
childFare2 = rs2.getFloat(1);
adultFare2 = rs2.getFloat(2);
}
}
if (flight2.length() == 0) {
tuiPiaoCost = (childFare1 * childTuiPiaoShu +
adultFare1 * adultTuiPiaoShu) * (float) 0.7;
}
else {
tuiPiaoCost = ( (childFare1 + childFare2) * childTuiPiaoShu +
(adultFare1 + adultFare2) * adultTuiPiaoShu) *
(float) 0.7;
}
}
catch (Exception e) {
e.printStackTrace();
}
return tuiPiaoCost;
}
/**
* 查询顾客及航班信息
*/
private boolean getClientInfo(String bookID, String customerID) {
boolean isBookExist = false;
boolean isIDRight = false;
String sql = "select * from bookInfo where bookID='" + bookID + "'";
ResultSet rs = dbManager.getResult(sql);
try {
if (rs.next()) {
isBookExist = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
if (isBookExist == false) {
JOptionPane.showMessageDialog(null, "订单号不存在", "错误信息",
JOptionPane.ERROR_MESSAGE);
return false;
}
sql = "select * from bookInfo where bookID='" + bookID +
"' and customerID='" + customerID + "'";
rs = dbManager.getResult(sql);
try {
if (rs.next()) {
isIDRight = true;
name = rs.getString("name").trim();
flight1 = rs.getString("flight").trim();
flight2 = rs.getString("returnFlight").trim();
ticketType = rs.getString("bookType").trim();
leaveTime1 = rs.getString("ticketDate").trim();
leaveTime2 = rs.getString("returnDate").trim();
childNum = rs.getString("childTickets").trim();
adultNum = rs.getString("adultTickets").trim();
cost = rs.getString("cost").trim();
}
}
catch (Exception e) {
e.printStackTrace();
}
if (isBookExist == true && isIDRight == false) {
JOptionPane.showMessageDialog(null, "身份证号不正确", "错误信息",
JOptionPane.ERROR_MESSAGE);
return false;
}
boolean isValid = canReturn(leaveTime1);
if (!isValid) {
JOptionPane.showMessageDialog(null, "该票已经过期!不能再退!",
"错误信息", JOptionPane.ERROR_MESSAGE);
return false;
}
if (isBookExist == true && isIDRight == true) {
if (Integer.parseInt(childNum) == 0 && Integer.parseInt(adultNum) == 0) {
JOptionPane.showMessageDialog(null, "该订单号已经无效!",
"错误信息", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
return false;
}
/**
* 判断是否满足退票条件
*/
private boolean canReturn(String time) {
String year = time.substring(0, 4);
String month = time.substring(4, 6);
String day = time.substring(6, 8);
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -