package cn.com.csu.command.impl;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import cn.com.csu.command.DispatchCommand;
import cn.com.csu.common.DateCaculate;
import cn.com.csu.dao.BillDao;
import cn.com.csu.dao.SaveKindDao;
import cn.com.csu.dao.UserVODao;
import cn.com.csu.vo.impl.Bill;
import cn.com.csu.vo.impl.BillVO;
import cn.com.csu.vo.impl.SavekindVO;
import cn.com.csu.vo.impl.UserVO;
/**
* 基于前端控制+应用控制+命令对象的设计思想,采用了MVC设计模式,而该类是一个命对象组件,
* 我将银行储蓄系统的操作方法均写在该类中。
* @author 杨泽敏
*/
public class Login extends DispatchCommand {
@SuppressWarnings("unchecked")
/**
* 用户登陆时的方法
*/
public String login(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
String user = request.getParameter("user"); //从界面获取用户名
String pw = request.getParameter("password"); //从界面获取用户密码
boolean flag = false;
UserVODao dao = new UserVODao();
List<UserVO> list = dao.findByAll();
/**
* 通过循环比较用户登陆是否已注册,若已注册则转入个人账户页面
*/
for(UserVO vo : list){
if(vo.getUserName().equals(user)&&vo.getPassword().equals(pw)){
flag = true;
request.setAttribute("user", user);
request.setAttribute("cardid", vo.getCardId());
session.setAttribute("user", user);
session.setAttribute("cardid", vo.getCardId());
}
}
BillDao bd = new BillDao();
List<Bill> result = bd.findByAll();
request.setAttribute("result", result); //将个人账户信息保存在request级别会话中
int amount = 0;
for(Bill vo : result){
amount = amount + vo.getAmount();
}
request.setAttribute("amount", amount);
if(flag == true){
return "/user.jsp";
} else {
return "/wrong.html";
}
}
/**
* 开户时的方法
* @param request
* @param response
* @return
*/
public String newUser(HttpServletRequest request , HttpServletResponse response){
String user = request.getParameter("name");
String pw = request.getParameter("pw");
String cardId = request.getParameter("card");
UserVO vo = new UserVO();
vo.setUserName(user);
vo.setPassword(pw);
vo.setCardId(cardId);
UserVODao dao = new UserVODao();
dao.add(vo);
return "login.html";
}
@SuppressWarnings("unchecked")
/**
* 存款时的方法
*/
public String showSave(HttpServletRequest request , HttpServletResponse response){
String user = request.getParameter("user");
String cardId = request.getParameter("cardid");
request.setAttribute("user", user);
request.setAttribute("cardId", cardId);
SaveKindDao dao = new SaveKindDao();
List<SavekindVO> list = dao.findByAll();
request.setAttribute("list", list);
return "/save.jsp";
}
@SuppressWarnings("unchecked")
/**
* 查找利率的方法
*/
public void queryIntrest(HttpServletRequest request , HttpServletResponse response){
int kindId = Integer.parseInt(request.getParameter("kindId"));
SaveKindDao dao = new SaveKindDao();
List<SavekindVO> list = dao.findById(kindId);
JSONArray jsonArray = JSONArray.fromObject(list);
response.setContentType("text/xml;charset=UTF-8");
PrintWriter pw = null;
try {
pw = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.write(jsonArray.toString());
pw.flush();
pw.close();
}
@SuppressWarnings("unchecked")
/**
* 储户存款时的方法
*/
public String save(HttpServletRequest request , HttpServletResponse response){
int kindId = Integer.parseInt(request.getParameter("kid"));
Calendar clendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//获取存款日期
Date date = clendar.getTime();
String saveDate = format.format(date);
int amount = Integer.parseInt(request.getParameter("amount"));
BillVO vo = new BillVO();
vo.setAmount(amount);
vo.setKindId(kindId);
vo.setSaveDate(saveDate);
BillDao dao = new BillDao();
dao.add(vo);
BillDao bd = new BillDao();
List<Bill> result = bd.findByAll();
request.setAttribute("result", result);
return "/user.jsp";
}
/**
* 修改用户密码的方法
* @param request
* @param response
* @return
*/
public String alter(HttpServletRequest request , HttpServletResponse response){
HttpSession session = request.getSession();
String user = session.getAttribute("user").toString();
String pw = request.getParameter("new");
UserVODao dao = new UserVODao();
dao.update(pw, user);
return "login.html";
}
@SuppressWarnings("deprecation")
/**
* 取款时显示利息清单的方法
*/
public String showGet(HttpServletRequest request , HttpServletResponse response){
String[] billid = request.getParameterValues("c");
int n = billid.length;
int[] billId = new int[n];
for(int i = 0; i < n; i++){
billId[i] = Integer.parseInt(billid[i]);
}
BillDao dao = new BillDao();
List<Bill> list = new ArrayList<Bill>();
Calendar ca1 = Calendar.getInstance();
Date date = ca1.getTime();
float[] amount = new float[n];
for(int i = 0; i < n; i++){
Bill vo = dao.findById(billId[i]);
String savedate = vo.getSaveDate().replace("-", "/");
Date saveDate = new Date(savedate);
Calendar ca2 = Calendar.getInstance();
ca2.setTime(saveDate);
DateCaculate dc = new DateCaculate();
int days = dc.getBetweenDays(ca1, ca2);
float interest = Float.valueOf(vo.getInterestRate())*days*vo.getAmount();
amount[i] = vo.getAmount() + interest;
vo.setMoney(amount[i]);
vo.setInterest(interest);
list.add(vo);
}
request.setAttribute("list", list);
float total = 0;
for(int i = 0; i < n; i++){
total = amount[i] + total;
}
request.setAttribute("total", total);
return "/get.jsp";
}
@SuppressWarnings("unchecked")
/**
* 取款时的方法
*/
public String get(HttpServletRequest request , HttpServletResponse response){
String[] billid = request.getParameterValues("hid");
int n = billid.length;
int[] billId = new int[n];
BillDao dao = new BillDao();
for(int i = 0; i < n; i++){
billId[i] = Integer.parseInt(billid[i]);
dao.delete(billId[i]);
}
BillDao bd = new BillDao();
List<Bill> result = bd.findByAll();
request.setAttribute("result", result);
int amount = 0;
for(Bill vo : result){
amount = amount + vo.getAmount();
}
request.setAttribute("amount", amount);
return "/user.jsp";
}
}