package com.jxd.bank.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import util.DBUtil;
import com.jxd.bank.entity.BankAccount;
import com.jxd.bank.entity.Record;
public class BankAccountDaoImpl implements BankAccountDao {
private static Scanner input = new Scanner(System.in);
private static List<Map<BankAccount,Record>> list = new ArrayList<Map<BankAccount,Record>>();
private static BankAccount bac = new BankAccount();
private static Record r = new Record();
private static Map<BankAccount,Record> map = new HashMap<BankAccount, Record>();
private static Connection conn = null;
private static PreparedStatement pst = null;
private static ResultSet rs = null;
/**
* 开户
*/
public BankAccount openAccount() {
System.out.println("欢迎您开办新账户!请根据系统提示输入!\n");
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("请输入密码:");
String pwd = input.next();
System.out.println("请输入初始余额");
double balance = input.nextInt();
System.out.println("请稍后,正在开户中……");
System.out.println("---------------------------");
try {
conn = DBUtil.getConnection();
String sql = "insert into bankaccount(cardid,customername,password,oldpassword,balance,opendate) "
+ "values(seq_bankaccount.nextval,?,?,?,?,sysdate)";
pst = conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, pwd);
pst.setString(3, pwd);
pst.setDouble(4, balance);
pst.executeUpdate();
String sql1 = "select * from bankaccount where customername = ? and password = ? order by cardid desc";
pst = conn.prepareStatement(sql1);
pst.setString(1, name);
pst.setString(2, pwd);
rs = pst.executeQuery();
if (rs.next()) {
String sql2 = "insert into record(id,cardid,transtype,transmoney,transdate) "
+ "values(seq_record.nextval,?,'1',?,sysdate)";
pst = conn.prepareStatement(sql2);
pst.setInt(1, rs.getInt("cardid"));
pst.setDouble(2, rs.getDouble("balance"));
pst.executeUpdate();
r.setCardid(rs.getInt("cardid"));
r.setTranstype("1");
r.setDate(rs.getString("opendate"));
r.setTransmoney(rs.getDouble("balance"));
bac.setCardid(rs.getInt("cardid"));
bac.setCustomername(name);
bac.setPassword(pwd);
bac.setOldpassword(pwd);
bac.setBalance(balance);
bac.setDate(rs.getDate("opendate") + " " + rs.getTime("opendate"));
map.put(bac, r);
list.add(map);
} else {
System.out.println("密码长度过长,开户失败");
}
System.out.println("---------------------------");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, pst, rs);
}
return bac;
}
/**
* 取款
*/
public List<Record> takeMoney() {
System.out.println("欢迎来办理取款业务!请根据提示输入!");
System.out.println("请输入账号:");
int cardid = input.nextInt();
System.out.println("请输入密码,有3次输入机会");
try {
conn = DBUtil.getConnection();
for (int i = 0; i < 3; i++) {
String pwd = input.next();
String sql = "select * from bankaccount where cardid = ? and password = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, cardid);
pst.setString(2, pwd);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("请输入取款金额:");
double balance = input.nextDouble();
if (balance <= rs.getDouble("balance")) {
System.out.println("请稍后,正在取款中……");
System.out
.println("---------------------------------------");
String sql1 = "update bankaccount set balance = balance - ? where cardid = ?";
pst = conn.prepareStatement(sql1);
pst.setDouble(1, balance);
pst.setInt(2, cardid);
pst.executeUpdate();
pst = conn.prepareStatement(sql);
pst.setInt(1, cardid);
pst.setString(2, pwd);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("恭喜你取款成功!");
System.out.println("您的余额是:"
+ rs.getDouble("balance"));
}
String sql2 = "insert into record(id,cardid,transtype,transmoney,transdate) "
+ "values(seq_record.nextval,?,'3',?,sysdate)";
pst = conn.prepareStatement(sql2);
pst.setInt(1, cardid);
pst.setDouble(2, balance);
pst.executeUpdate();
String sql3 = "select * from record where cardid = ? and transmoney = ? and transtype = '3' "
+ "order by id desc";
pst = conn.prepareStatement(sql3);
pst.setInt(1, cardid);
pst.setDouble(2, balance);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("您的存款日期:"
+ rs.getString("transdate"));
r.setCardid(rs.getInt("cardid"));
r.setTranstype("3");
r.setDate(rs.getString("transdate"));
r.setTransmoney(rs.getDouble("transmoney"));
list.add(r);
}
break;
} else {
System.out.println("余额不足!!");
}
} else {
System.out.println("密码错误,请重新输入,还有" + (2 - i) + "次机会");
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, pst, rs);
}
return list;
}
/**
* 存款
*/
public List<Record> saveMoney() {
System.out.println("欢迎您来办理存款业务!请根据提示输入!");
System.out.println("请输入账号:");
int cardid = input.nextInt();
System.out.println("请输入存款的金额:");
double balance = input.nextDouble();
System.out.println("请稍后,正在存款中……");
System.out.println("---------------------------------");
try {
conn = DBUtil.getConnection();
String sql = "select * from bankaccount where cardid = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, cardid);
rs = pst.executeQuery();
if (rs.next()) {
String sql1 = "update bankaccount set balance = balance + ? where cardid = ?";
pst = conn.prepareStatement(sql1);
pst.setDouble(1, balance);
pst.setInt(2, cardid);
pst.executeUpdate();
pst = conn.prepareStatement(sql);
pst.setInt(1, cardid);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("恭喜你存款成功!");
System.out.println("您的存款金额是:" + balance);
System.out.println("您的余额是:" + rs.getDouble("balance"));
String sql2 = "insert into record(id,cardid,transtype,transmoney,transdate) "
+ "values(seq_record.nextval,?,'2',?,sysdate)";
pst = conn.prepareStatement(sql2);
pst.setInt(1, cardid);
pst.setDouble(2, balance);
pst.executeUpdate();
String sql3 = "select * from record where cardid = ? and transmoney = ? and transtype = '2' "
+ "order by id desc";
pst = conn.prepareStatement(sql3);
pst.setInt(1, cardid);
pst.setDouble(2, balance);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("您的存款日期:"
+ rs.getString("transdate"));
r.setCardid(rs.getInt("cardid"));
r.setTranstype("2");
r.setDate(rs.getString("transdate"));
r.setTransmoney(rs.getDouble("transmoney"));
list.add(r);
}
}
} else {
System.out.println("卡号不存在");
}
System.out.println("---------------------------------");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(conn, pst, rs);
}
return list;
}
/**
* 查询
*/
public List<Record> queryMoney() {
String type = null;
System.out.println("欢迎您来查看账户信息!请根据提示输入!");
System.out.println("请输入账号:");
int cardid = input.nextInt();
System.out.println("请输入�
没有合适的资源?快使用搜索试试~ 我知道了~
银行项目代码参考,这个可以帮助大家学习Java
共19个文件
java:8个
class:8个
project:1个
需积分: 14 5 下载量 81 浏览量
2018-01-16
22:36:15
上传
评论 1
收藏 19KB ZIP 举报
温馨提示
myeclipse10安装包以及安装步骤以及插件
资源推荐
资源详情
资源评论
收起资源包目录
bankSystem.zip (19个子文件)
bankSystem
.project 386B
bin
com
jxd
bank
dao
BankAccountDaoImpl.class 7KB
RecordDao.class 116B
RecordDaoImpl.class 332B
BankAccountDao.class 447B
main
Test.class 3KB
entity
Record.class 1KB
BankAccount.class 2KB
util
DBUtil.class 1KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
jxd
bank
dao
RecordDao.java 64B
BankAccountDaoImpl.java 16KB
RecordDaoImpl.java 84B
BankAccountDao.java 377B
main
Test.java 2KB
entity
Record.java 787B
BankAccount.java 1KB
util
DBUtil.java 1KB
.classpath 363B
共 19 条
- 1
资源评论
qq_26826443
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于LQR实现车辆轨迹跟踪matlab源码+项目说明+超详细代码注释(高分项目)
- Android 和 Java 字节码查看器.zip
- android java 和 javascript bridge,灵感来自微信 webview jsbridge.zip
- Amplitude 的 JavaScript SDK.zip
- Allen Downey 和 Chris Mayfield 编写的 Think Java 支持代码 .zip
- 23种设计模式 Java 实现.zip
- 100 多个使用 HTML、CSS 和 JavaScript 的迷你网络项目 .zip
- 100 个项目挑战.zip
- 哈夫曼树-数据压缩与优化:基于哈夫曼树的最佳编码实践及其应用
- 海康工业相机Linux系统下的软件安装及二次开发说明文档
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功