演示界面
一、 系统页面选择,只能输入正确选项
二、开户,如果两次输入密码不同开户失败,重新显示主页面
如果输入预存金额为负同样开户失败,另外密码必须同时包含字母数字和字符,存款金额只能是100的整数倍
三、登录情况有账号输入错误,账号不存在,密码输入错误,如果错误次数超过三次系统退出
四、存钱操作情况有输入正确,输入为字符,输入为负,同样存钱只能存100的整数倍
五、取款操作有输入字符,输入为负,取款金额超过余额,取款成功,同样取款只能取100的整数倍
六、 余额查询
七、 操作记录查询
八、 转账操作,情况有账号错误,账号不存在,转账金额为负转账数额超过余额
九、 修改密码、注销操作、退出
代码
目录结构
DAO层
dao层IOperationRecordDao接口
package com.jxd.dao;
import com.jxd.model.Account;
public interface IOperationRecordDao {
boolean openAccount(Account account);//开户操作记录
boolean sign(Account account);//登录
boolean saveMoney(Account account);//存钱
boolean withdrawMoney(Account account);//取款
boolean checkBalance(Account account);//查询余额
boolean transAccount(Account account);//转账
boolean queryTransRecord(Account account);//查询交易记录
boolean changePassword(Account account);//修改密码
boolean cancellation(Account account);//注销账户
}
dao层IBankProjectDao接口
package com.jxd.dao;
import com.jxd.model.Account;
public interface IBankProjectDao {
boolean openAccount(Account account);//开户
boolean sign(Account account);//登录
boolean saveMoney(Account account);//存钱
boolean withdrawMoney(Account account);//取款
boolean checkBalance(Account account);//查询余额
boolean transAccount(Account account);//转账
boolean queryTransRecord(Account account);//查询交易记录
boolean changePassword(Account account);//修改密码
boolean cancellation(Account account);//注销账户
}
dao层OperationRecord实现类
package com.jxd.dao.impl;
import com.jxd.dao.IOperationRecordDao;
import com.jxd.model.Account;
import com.jxd.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/5 13:40
*/
public class OperationRecord implements IOperationRecordDao {
/**
* 开户操作记录
* @param account
* @return
*/
@Override
public boolean openAccount(Account account) {
boolean isOpen = false;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
try {
String sql1 = "select cardid,customername,balance,opendate from account where customername = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setString(1,account.getCustomername());
resultSet = preparedStatement.executeQuery();
Account account1 =new Account();
while (resultSet.next()){
account1.setCardid(resultSet.getInt("cardid"));
account1.setCustomername(resultSet.getString("customername"));
account1.setBalance(resultSet.getDouble("balance"));
account1.setOpendate(resultSet.getString("opendate"));
list.add(account1);
}
String sql = "insert into operationrecord(id,cardid,transtype,transmoney,transdate)" +
"values (seq_record.nextval,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account1.getCardid());
preparedStatement.setString(2,"开户操作");
preparedStatement.setDouble(3, account1.getBalance());
preparedStatement.setTimestamp(4,dateSQL);
int num = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
isOpen = true;
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return isOpen;
}
@Override
public boolean sign(Account account) {
return false;
}
/**
* 存钱操作记录
* @param account
* @return
*/
@Override
public boolean saveMoney(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
try {
String sql = "insert into operationrecord(id,cardid,transtype,transmoney,transdate)" +
"values (seq_record.nextval,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
preparedStatement.setString(2,"存钱操作");
preparedStatement.setDouble(3, account.getTransMoney());
preparedStatement.setTimestamp(4,dateSQL);
int num = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
/**
* 取钱操作
* @param account
* @return
*/
@Override
public boolean withdrawMoney(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
try {
String sql = "insert into operationrecord(id,cardid,transtype,transmoney,transdate)" +
"values (seq_record.nextval,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
preparedStatement.setString(2,"取钱操作");
preparedStatement.setDouble(3, account.getTransMoney());
preparedStatement.setTimestamp(4,dateSQL);
int num = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
@Override
public boolean checkBalance(Account account) {
return false;
}
/**
* 转账操作记录
* @param account
* @return
*/
@Override
public boolean transAccount(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
try {
String sql = "insert into operationrecord(id,cardid,transtype,transmoney,transdate)" +
"values (seq_record.nextval,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
preparedStatement.setString(2,"转账操作");
preparedStatement.setDouble(3, account.getTransMoney());
preparedStatement.setTimestamp(4,dateSQL);
int num = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
/**
* 查询交易记录
* @param account
* @return
*/
@Override
public boolean queryTransRecord(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
try {
ResultSet resultSet = null;
Account account1 = new Account();
String sql = "select id,cardid,transtype,transmoney,transdate from operationrecord where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
int num2 = 1;
while (resultSet.next()){
account1.setId(resultSet.getInt("id"));
account1.setCardid(resultSet.getInt("cardid"));
account1.setTransType(resultSet.getString("transtype"));
account1.setTransMoney(resultSet.getDouble("transmoney"));
account1.setTransDate(resultSet.getString("transdate"));
System.out.println(num2 + "\t账号:" + account1.getCardid() +
"\t操作类型:" + account1.getTransType() +
"\t操作金额:" + account1.getTransMoney() +
"元\t操作日期:" + account1.getTransDate());
num2++;
}
return true;
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
/**
* 修改密码操作
* @param account
* @return
*/
@Override
public boolean changePassword(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp dateSQL = DBUtil.Timestamp();
try {
String sql = "insert into operationrecord(id,cardid,transtype,transdate)" +
"values (seq_record.nextval,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
preparedStatement.setString(2,"修改密码操作");
preparedStatement.setTimestamp(3,dateSQL);
int num = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
@Override
public boolean cancellation(Account account) {
return false;
}
}
dao层BankProjectImpl实现类
package com.jxd.dao.impl;
import com.jxd.dao.IBankProjectDao;
import com.jxd.model.Account;
import com.jxd.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/4 19:38
*/
public class BankProjectImpl implements IBankProjectDao {
Scanner input = new Scanner(System.in);
/**
* 开户操作包括姓名,密码以及开户金额,完成后储
* 户信息被保存到一个储户基本信息表中,并反馈给储户开户后的账号,开
* 户金额,开户日期。同时生成一个操作记录包括账号,操作类型,操作金
* 额,本次操作的具体时间,旧密码,新密码
* @param account
* @return
*/
@Override
public boolean openAccount(Account account) {
boolean isOpen = false;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
java.sql.Timestamp date = DBUtil.Timestamp();
String sql = "insert into account(cardid,customername,balance,password,opendate)" +
"values (seq_account.nextval,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,account.getCustomername());
preparedStatement.setDouble(2,account.getBalance());
preparedStatement.setString(3,account.getPassword());
preparedStatement.setTimestamp(4, date);
int num = preparedStatement.executeUpdate();
if (num == 1){
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
try {
String sql1 = "select cardid,customername,balance,opendate from account where customername = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setString(1,account.getCustomername());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
Account account1 =new Account();
account1.setCardid(resultSet.getInt("cardid"));
account1.setCustomername(resultSet.getString("customername"));
account1.setBalance(resultSet.getDouble("balance"));
account1.setOpendate(resultSet.getString("opendate"));
list.add(account1);
}
for (Account account1:list){
System.out.println("账户" + account1.getCardid()+"\t名字"+
account1.getCustomername()+ "\t余额"+ account1.getBalance() +
"\t日期" + account1.getOpendate()
);
}
}catch (SQLException e){
e.printStackTrace();
}
isOpen = true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return isOpen;
}
@Override
public boolean sign(Account account) {
return false;
}
/**
* 存款操作
* @param account
* @return
*/
@Override//
public boolean saveMoney(Account account) {
boolean isInput = false;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp date = DBUtil.Timestamp();
System.out.println("请输存款金额:");
//判断输入金额是否正确
do {
if (input.hasNextDouble()){
account.setTransMoney(input.nextDouble());
if (account.getTransMoney() > 0){
if (account.getTransMoney() % 100 == 0){
isInput = true;
}else {
System.out.println("只能存100元的整数倍");
}
}else {
System.out.println("输入金额要大于0,请重新输入");
}
}else {
String str = input.next();
System.out.println("输入金额错误,请重新输入");
}
}while (!isInput);
try {
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
//查询余额
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
account.setBalance(resultSet.getDouble("balance"));
list.add(account);
}
//更新余额
String sql = "update account set balance = ? where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1,account.getTransMoney() + account.getBalance());
preparedStatement.setInt(2,account.getCardid());
int num = preparedStatement.executeUpdate();
if (num == 1){
System.out.println("成功存款" + account.getTransMoney() + "元");
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
/**
* 取款操作
* @param account
* @return
*/
@Override
public boolean withdrawMoney(Account account) {
boolean isInput = false;
boolean isBalance = false;//能否取款
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp date = DBUtil.Timestamp();
System.out.println("请输取款金额:");
do {
if (input.hasNextDouble()){
account.setTransMoney(input.nextDouble());
if (account.getTransMoney() > 0){
if (account.getTransMoney() % 100 == 0){
isInput = true;
}else {
System.out.println("只能取100元的整数倍");
}
}else {
System.out.println("输入金额要大于0,请重新输入");
}
}else {
String str = input.next();
System.out.println("输入金额错误,请重新输入");
}
}while (!isInput);
try {
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
account.setBalance(resultSet.getDouble("balance"));
//判断余额是否大于转账金额
if (account.getTransMoney() > account.getBalance()){
System.out.println("余额不足");
return false;
}else {
isBalance = true;
list.add(account);
}
}
if (isBalance){
String sql = "update account set balance = ? where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1,account.getBalance() - account.getTransMoney());
preparedStatement.setInt(2,account.getCardid());
int num = preparedStatement.executeUpdate();
if (num == 1){
System.out.println("取款成功" + account.getTransMoney() + "元");
return true;
}
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
/**
* 查询余额
* @param account
* @return
*/
@Override
public boolean checkBalance(Account account) {
boolean isInput = false;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp date = DBUtil.Timestamp();
try {
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
account.setBalance(resultSet.getDouble("balance"));
list.add(account);
}
System.out.println("您的账户余额为:" + account.getBalance() + "元。");
return true;
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
/**
* 转账操作
* @param account
* @return
*/
@Override
public boolean transAccount(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
//获取当前余额
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
account.setBalance(resultSet.getDouble("balance"));
list.add(account);
}
//更新余额
String sql = "update account set balance = ? where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1,account.getBalance() + account.getTransMoney());
preparedStatement.setInt(2,account.getCardid());
int num = preparedStatement.executeUpdate();
if (num == 1){
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
@Override
public boolean queryTransRecord(Account account) {
return false;
}
/**
* 修改密码
* @param account
* @return
*/
@Override
public boolean changePassword(Account account) {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp date = DBUtil.Timestamp();
String newPassword1;
String newPassword2;
do {
System.out.println("请输新密码:");
newPassword1 = input.next();
System.out.println("请再次输入新密码:");
newPassword2 = input.next();
if (newPassword1.equals(newPassword2)){
break;
}else {
System.out.println("两次输入密码不一致");
}
}while (true);
try {
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
String sql1 = "update account set password = ?, oldpassword = ? where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setString(1,newPassword1);
preparedStatement.setString(2,account.getPassword());
preparedStatement.setInt(3,account.getCardid());
int num = preparedStatement.executeUpdate();
if (num == 1){
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
/**
* 注销账户
* @param account
* @return
*/
@Override
public boolean cancellation(Account account) {
boolean isInput = false;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
java.sql.Timestamp date = DBUtil.Timestamp();
try {
List<Account> list = new ArrayList<>();
ResultSet resultSet = null;
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
account.setBalance(resultSet.getDouble("balance"));
if (account.getBalance() == 0){
String sql2 = "delete from operationrecord where cardid = ?";
preparedStatement = connection.prepareStatement(sql2);
preparedStatement.setInt(1,account.getCardid());
int num2 = preparedStatement.executeUpdate();
String sql = "delete from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
int num = preparedStatement.executeUpdate();
if (num == 1){
return true;
}else {
System.out.println("注销失败");
return true;
}
}else {
System.out.println("您的账户内还有余额,不能注销!");
return false;
}
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(null,preparedStatement,connection);
}
return false;
}
}
model层
model层Account实体类
package com.jxd.model;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/4 19:40
*/
public class Account {
private int cardid;
private String customername;
private String password;
private String oldpassword;
private double balance;
private String opendate;
private int id;
private String transType;
private double transMoney;
private String transDate;
public Account() {
}
public Account(int cardid) {
this.cardid = cardid;
}
public Account(int cardid, String password) {
this.cardid = cardid;
this.password = password;
}
public Account(int cardid, int id, String transType, double transMoney, String transDate) {
this.cardid = cardid;
this.id = id;
this.transType = transType;
this.transMoney = transMoney;
this.transDate = transDate;
}
public Account(String password, double balance, String oldpassword) {
this.password = password;
this.oldpassword = oldpassword;
this.balance = balance;
}
public Account(String customername, String password, String oldpassword, double balance) {
this.customername = customername;
this.password = password;
this.balance = balance;
this.oldpassword = oldpassword;
}
public Account(int cardid, String customername, String password, String oldpassword, double balance, String opendate) {
this.cardid = cardid;
this.customername = customername;
this.password = password;
this.oldpassword = oldpassword;
this.balance = balance;
this.opendate = opendate;
}
public int getCardid() {
return cardid;
}
public void setCardid(int cardid) {
this.cardid = cardid;
}
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOldpassword() {
return oldpassword;
}
public void setOldpassword(String oldpassword) {
this.oldpassword = oldpassword;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getOpendate() {
return opendate;
}
public void setOpendate(String opendate) {
this.opendate = opendate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTransType() {
return transType;
}
public void setTransType(String transType) {
this.transType = transType;
}
public double getTransMoney() {
return transMoney;
}
public void setTransMoney(double transMoney) {
this.transMoney = transMoney;
}
public String getTransDate() {
return transDate;
}
public void setTransDate(String transDate) {
this.transDate = transDate;
}
}
service层
service层IBankProjectService接口
package com.jxd.service;
import com.jxd.model.Account;
import java.sql.SQLException;
public interface IBankProjectService {
boolean openAccount(Account account);//开户
boolean sign(Account account) throws SQLException;//登录
boolean saveMoney(Account account);//存钱
boolean withdrawMoney(Account account);//取款
boolean checkBalance(Account account);//查询余额
boolean transAccount(Account account);//转账
boolean queryTransRecord(Account account);//查询交易记录
boolean changePassword(Account account);//修改密码
boolean cancellation(Account account);//注销账户
}
service层BankProjectServiceImpl实现类
package com.jxd.service.impl;
import com.jxd.dao.IBankProjectDao;
import com.jxd.dao.IOperationRecordDao;
import com.jxd.dao.impl.OperationRecord;
import com.jxd.model.Account;
import com.jxd.service.IBankProjectService;
import com.jxd.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/4 19:41
*/
public class BankProjectServiceImpl implements IBankProjectService {
IBankProjectDao bankProjectDao = new com.jxd.dao.impl.BankProjectImpl();
IOperationRecordDao operationRecordDao = new OperationRecord();
Scanner input = new Scanner(System.in);
/**
* 开户操作,判断两次输入密码是否相同
* @param account
* @return
*/
@Override
public boolean openAccount(Account account) {
if (account.getPassword().equals(account.getOldpassword())) {
if (account.getBalance() > 0){
if (account.getBalance() % 100 == 0){
boolean isOpen = bankProjectDao.openAccount(account);
return isOpen;
}else {
System.out.println("只能存100元的整数倍");
return false;
}
}else {
System.out.println("预存金额不能为负!!!");
return false;
}
}else {
System.out.println("两次密码不相同!!!");
return false;
}
}
/**
* 登录操作,判断输入密码是否匹配
* @param account
* @return 是否登录成功
* @throws SQLException
*/
@Override
public boolean sign(Account account) {
ResultSet resultSet = null;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
String sql = "select customername,password from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String pwd = resultSet.getString("password");
String name = resultSet.getString("customername");
if (pwd.equals(account.getPassword())){
System.out.println("欢迎您" + name);
return true;
}else {
System.out.println("账号或密码错误");
return false;
}
}
System.out.println("账号不存在");
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,preparedStatement,connection);
}
return false;
}
@Override
public boolean saveMoney(Account account) {
return false;
}
@Override
public boolean withdrawMoney(Account account) {
return false;
}
@Override
public boolean checkBalance(Account account) {
return false;
}
/**
* 转账操作
* @param account
* @return
*/
@Override
public boolean transAccount(Account account) {
boolean isYes = false;
boolean isNo = true;
System.out.println("请输入要转给的账户:");
do {
//判断输入账户是否为整数
if (input.hasNextInt()){
int cardid = input.nextInt();
if (cardid == account.getCardid()){
System.out.println("不能给自身账号转账,请重新输入:");
}else {
ResultSet resultSet = null;
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
String sql = "select customername,password from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,cardid);
resultSet = preparedStatement.executeQuery();
//判断是否找到账户
while (resultSet.next()){
Account account1 = new Account(cardid);
System.out.println("请输入转账金额:");
boolean isMoney = false;
do {
//判断输入是否为数字
if (input.hasNextDouble()){
double money = input.nextDouble();
//判断输入金额是否为正数
if (money > 0){
double balanceMoney = 0;
try {
String sql1 = "select balance from account where cardid = ?";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,account.getCardid());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
balanceMoney = (resultSet.getDouble("balance"));
//判断能否转账
if (money > balanceMoney){
System.out.println("转账金额超过当前余额!!!,请重新输入:");
}else {
account.setTransMoney(-money);
account1.setTransMoney(money);
if (bankProjectDao.transAccount(account) && bankProjectDao.transAccount(account1)){
//转账成功插入记录
operationRecordDao.transAccount(account);
operationRecordDao.transAccount(account1);
System.out.println("转账成功,转账金额为:" + money + "元");
isMoney = true;
}
}
}
}catch (SQLException e){
e.printStackTrace();
}
}else {
System.out.println("转账金额不能为负,请重新输入:");
}
}else {
String s = input.next();
System.out.println("输入错误,请重新输入:");
}
}while (!isMoney);
isYes = true;
}
if (!isYes){
System.out.println("账号不存在,请重新输入");
isNo = false;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,preparedStatement,connection);
}
}
}else if(isNo){
String str = input.next();
System.out.println("输入错误,请重新输入:");
}
isNo = true;
}while (!isYes);
return false;
}
@Override
public boolean queryTransRecord(Account account) {
return false;
}
@Override
public boolean changePassword(Account account) {
return false;
}
@Override
public boolean cancellation(Account account) {
return false;
}
}
test层
test层ShowMenu实现类
package com.jxd.test;
import com.jxd.dao.IBankProjectDao;
import com.jxd.dao.IOperationRecordDao;
import com.jxd.dao.impl.BankProjectImpl;
import com.jxd.dao.impl.OperationRecord;
import com.jxd.model.Account;
import com.jxd.service.IBankProjectService;
import com.jxd.service.impl.BankProjectServiceImpl;
import java.sql.SQLException;
import java.util.Scanner;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/5 11:22
*/
public class ShowMenu {
Scanner input = new Scanner(System.in);
IBankProjectDao bankProjectDao = new BankProjectImpl();
IBankProjectService bankProjectService = new BankProjectServiceImpl();
IOperationRecordDao operationRecordDao = new OperationRecord();
/**
* 登陆选项
* @return 登录选项
*/
public int showHome(){
boolean isChoice = false;
int num = 0;
System.out.println("\t\t银行管理系统");
System.out.println("***********************************************");
System.out.println("\t\t1、开户");
System.out.println("\t\t2、登录");
System.out.println("\t\t3、退出");
System.out.println("**********************************************");
System.out.println("请选择服务种类:");
do {
if (input.hasNextInt()){
num = input.nextInt();
if (num == 1 || num == 2 || num == 3){
isChoice = true;
}else {
System.out.println("选择错误,请重新输入");
}
}else {
String str = input.next();
System.out.println("选择错误,请重新输入");
}
}while (!isChoice);
return num;
}
/**
* 开户操作
*/
public boolean openAccount(){
String password;//第一次输入密码
String password2;//第二次输入密码
boolean isPass = false;
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("请输入密码(必须同时包含字母数字和符号):");
do {
boolean isPass2 = false;
int numPass = 0;
int wordPass = 0;
int charPass = 0;
password = input.next();
if (password.length() >= 6){
char[] str = password.toCharArray();
//判断输入密码是否包含字母数字和符号
for (int i = 0; i < str.length; i++) {
if (str[i] >= 33 && str[i] <= 126){
if (str[i] >= 48 && str[i] <= 57){
numPass = 1;
}else if ((str[i] >= 65 && str[i] <= 90 ) || (str[i] >= 97 && str[i] <= 122 )){
wordPass = 1;
}else {
charPass = 1;
}
}else {
System.out.println("输入格式不正确,请重新输入:");
isPass2= true;
break;
}
}
if (!isPass2){
if (numPass != 0 && wordPass != 0 && charPass != 0){
isPass = true;
}else {
System.out.println("必须同时包含字母数字和符号!!!");
System.out.println("请重新输入密码:");
}
}
}else {
System.out.println("密码长度不能小于6位,请重新输入:");
}
}while (!isPass);
System.out.println("请再次确认密码:");
password2 = input.next();
System.out.println("请缴纳预存余额:");
double balance = input.nextDouble();
Account account = new Account(name,password,password2,balance);
boolean isOpen = bankProjectService.openAccount(account);;
if (isOpen){
operationRecordDao.openAccount(account);
System.out.println("开户成功!");
return true;
}else {
System.out.println("开户失败!");
return false;
}
}
/**
* 菜单选择,返回选择
* @return 菜单选项
*/
public Account signIn() {
boolean isGo = false;
boolean isSign = false;
int cardId = 0;//卡号
String password = null;//密码
Account account = null;
int signNum = 0;
System.out.println("**************用户登陆页面********************");
do {
do {
System.out.println("请输入卡号:");
if (input.hasNextInt()){
cardId = input.nextInt();
System.out.println("请输入密码:");
password = input.next();
isGo = true;
}else {
String str = input.next();
System.out.println("输入错误!!!");
}
}while (!isGo);
account = new Account(cardId, password);
try {
boolean isYes = bankProjectService.sign(account);
if (isYes){
showLandPage();
isSign = true;
}else {
signNum++;
}
if (signNum == 3){
System.out.println("密码错误3次,系统退出");
System.exit(0);
}
}catch (SQLException e){
e.printStackTrace();
}
}while (!isSign);
return account;
}
/**
* 判断是否选择正确
*/
public int choiceLandPage(){
int choiceNum = 0;//菜单选择
boolean isChoice1 = false;
do {
if (input.hasNextInt()){
choiceNum = input.nextInt();
if (choiceNum >=1 && choiceNum <= 8){
isChoice1 = true;
}else {
System.out.println("选择错误,请重新输入");
}
}else {
System.out.println("选择错误,请重新输入");
}
}while (!isChoice1);
return choiceNum;
}
/**
* 菜单选项
*/
public void showLandPage(){
System.out.println("\t\t1、存钱");
System.out.println("\t\t2、取钱");
System.out.println("\t\t3、查询账户余额");
System.out.println("\t\t4、查询交易记录");
System.out.println("\t\t5、转账");
System.out.println("\t\t6、修改密码");
System.out.println("\t\t7、注销账户");
System.out.println("\t\t8、退出");
System.out.println("请选择要进行的业务:");
}
}
test层TestBankProject测试类
package com.jxd.test;
import com.jxd.dao.IBankProjectDao;
import com.jxd.dao.IOperationRecordDao;
import com.jxd.dao.impl.BankProjectImpl;
import com.jxd.dao.impl.OperationRecord;
import com.jxd.model.Account;
import com.jxd.service.IBankProjectService;
import com.jxd.service.impl.BankProjectServiceImpl;
import java.util.Scanner;
/**
* @Desecription TODO
* @Auther ZhengShouke
* @Date 2020/8/4 19:42
*/
public class TestBankProject {
public static void main(String[] args) {
IBankProjectService bankProjectService = new BankProjectServiceImpl();
IBankProjectDao bankProjectDao = new BankProjectImpl();
IOperationRecordDao operationRecordDao = new OperationRecord();
Scanner input = new Scanner(System.in);
boolean isGo = false;
do {
//显示登录菜单
ShowMenu showMenu = new ShowMenu();
switch (showMenu.showHome()){
case 1:
//开户
showMenu.openAccount();
break;
case 2:
do {
//登录
Account account = showMenu.signIn();
do {
int choiceNum = showMenu.choiceLandPage();
boolean isTure = false;//是否执行成功
switch (choiceNum){
case 1://存钱
isTure = bankProjectDao.saveMoney(account);
if (isTure){
operationRecordDao.saveMoney(account);
}
System.out.println("请继续选择操作:");
continue;
case 2://取钱
isTure = bankProjectDao.withdrawMoney(account);
if (isTure){
operationRecordDao.withdrawMoney(account);
}
System.out.println("请继续选择操作:");
continue;
case 3://查询账户余额
bankProjectDao.checkBalance(account);
System.out.println("请继续选择操作:");
continue;
case 4://查询交易记录
operationRecordDao.queryTransRecord(account);
System.out.println("请继续选择操作:");
continue;
case 5://转账
bankProjectService.transAccount(account);
System.out.println("请继续选择操作:");
continue;
case 6://修改密码
if (bankProjectDao.changePassword(account)){
operationRecordDao.changePassword(account);
System.out.println("密码修改成功");
}
System.out.println("请继续选择操作:");
continue;
case 7://注销账户
if (bankProjectDao.cancellation(account)){
System.out.println("注销成功,谢谢您的使用");
System.exit(0);
}else {
System.out.println("请继续选择操作:");
continue;
}
case 8://退出
System.out.println("谢谢使用,再见!!!");
System.exit(0);
break;
}
isTure = false;
break;
}while (true);
}while (true);
case 3:
System.exit(0);
break;
}
}while (!isGo);
}
}
util工具层
DBUtil工具类
package com.jxd.util;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Desecription 数据库连接和关闭工具类
* @Auther ZhengShouke
* @Date 2020/8/3 16:07
*/
public class DBUtil {
//连接数据库的四大参数
private static String driver = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user = "zsk";
private static String password = "zsk";
/**
* 获得数据库连接
* @return 数据库连接
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}
return connection;
}
/**
* 关闭资源
* @param resultSet 结果集
* @param statement 执行sql对象
* @param connection 数据库连接对象
*/
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection){
try {
if (resultSet != null){
resultSet.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
public static Timestamp Timestamp(){
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
java.util.Date date = new Date(System.currentTimeMillis());
String openDate = formatter.format(date);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss ");
java.util.Date dates = null;
try {
dates = sf.parse(openDate);
} catch (Exception e) {
e.printStackTrace();
}
java.sql.Timestamp dateSQL = new java.sql.Timestamp(dates.getTime());
return dateSQL;
}
}
原创,转载请标明
源码地址
提取码:1z3n