IT学习资源网2023-01-07
已验证
基于JAVA控制台实现银行管理系统_S7501
摘要:
详细描述:
JAVA控制台实现简单的银行卡基本功能,包含银行卡开户 取款 存款 转账及退卡等操作,使用mysql数据库
项目结构如:
主菜单如:
主功能代码如:
package cn.cnitstudy.view;
import java.util.LinkedList;
import java.util.Scanner;
import cn.cnitstudy.po.Account;
import cn.cnitstudy.service.AccountService;
import cn.cnitstudy.service.AccountServiceImpl;
public class AccountTest {
public static void main(String[] args) {
AccountTest ui = new AccountTest();
ui.loadmenu();
}
//主菜单
public void menu(){
System.out.println("******欢迎光临银行管理系统******");
System.out.println("1.开户2.存款3.取款4.转账5.退卡");
System.out.println("请选择输入操作序号进行操作:");
}
public void loadmenu(){
Scanner in = new Scanner(System.in);
AccountService accountService = new AccountServiceImpl();
// 展示主界面
menu();
while (true) {
// 根据输入值进入界面
try {
int type = in.nextInt();
switch (type) {
case 1:
//进入开户
newAccount(in);
break;
case 2:
//存款
cunkuanAccount(in);
break;
case 3:
//取款
qukuanAccount(in);
break;
case 4:
//转账
zhuanzhangAccount(in);
break;
//退出
case 5:
System.out.println("感谢您的光临,欢迎您下次再来!");
return;
default:
System.out.println("输入错误,请重新输入");
menu();
}
} catch (Exception e) {
System.out.println("输入错误,请重新输入");
loadmenu();
break;
}
}
}
//开户
public void newAccount(Scanner in){
AccountService accountService = new AccountServiceImpl();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入身份证号:");
String incardID = scanner.next();
if(accountService.selectByCardID(incardID)!=null) {
System.out.println("存在重复的身份证号:请换一个号码录入");
newAccount(in);
}
System.out.println("请输入姓名:");
String inname = scanner.next();
System.out.println("请输入密码:");
String inpassword = scanner.next();
Account account = new Account();
account.setBalance(-10.0);
account.setCardID(incardID);
account.setName(inname);
account.setPassword(inpassword);
accountService.insertAccount(account);
System.out.println("开户成功");
loadmenu();
}
//登录
public String login(Scanner in){
AccountService accountService = new AccountServiceImpl();
System.out.println("请输入身份证号和密码:");
String cardID = in.next();
String password = in.next();
if(cardID.equals("") || password.equals("")){
System.out.println("身份证号和密码不能为空!请重新操作");
login(in);
}else {
Account account =accountService.selectByName(cardID, password);
if(account!=null) {
return cardID;
}else {
System.out.println("身份证号或密码错误,请重新操作");
login(in);
}
}
return null;
}
//取款
public void qukuanAccount(Scanner in){
String cardID = login(in);
AccountService accountService = new AccountServiceImpl();
Account account =accountService.selectByCardID(cardID);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入取款金额:");
String balance = scanner.next();
Double b = Double.parseDouble(balance);
if(b>account.getBalance()) {
System.out.println("余额不足");
loadmenu();
}else {
account.setBalance(account.getBalance()-b);
accountService.updateAccount(account);
System.out.println("取款成功");
loadmenu();
}
}
//存款
public void cunkuanAccount(Scanner in){
String cardID = login(in);
AccountService accountService = new AccountServiceImpl();
Account account =accountService.selectByCardID(cardID);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入存款金额:");
String balance = scanner.next();
account.setBalance(Double.parseDouble(balance)+account.getBalance());
accountService.updateAccount(account);
System.out.println("存款成功");
loadmenu();
}
//转账
public void zhuanzhangAccount(Scanner in){
String cardID = login(in);
AccountService accountService = new AccountServiceImpl();
Account account =accountService.selectByCardID(cardID);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入收款账号:");
String incardID = scanner.next();
Account inaccount =accountService.selectByCardID(incardID);
if(inaccount==null) {
System.out.println("输入的收款账号错误或者不存在:");
zhuanzhangAccount(in);
}else {
System.out.println("请输入转账金额:");
String balance = scanner.next();
Double b = Double.parseDouble(balance);
if(b>account.getBalance()) {
System.out.println("余额不足,转账失败");
loadmenu();
}else {
account.setBalance(account.getBalance()-Double.parseDouble(balance));
accountService.updateAccount(account);
inaccount.setBalance(inaccount.getBalance()+Double.parseDouble(balance));
accountService.updateAccount(inaccount);
System.out.println("你向"+incardID+"("+inaccount.getName()+")成功转账"+balance+"元");
loadmenu();
}
}
}
}
数据库操作部分主要代码
@Override
public Account selectByName(String cardID, String password) {
// Connection 接口代表与特定的数据库连接 并在连接上下文中执行sql 语句
Connection conn= getConn();
String sql="select * from account where cardID = '" + cardID + "' and password = '" + password + "'";
//PreparedStatement类型执行动态的sql语句 Statement 执行静态语句
PreparedStatement pstmt;
try {
//执行sql语句
pstmt = conn.prepareStatement(sql);
// 执行给定的sql语句返回单个 ResultSet 对象
ResultSet rs = pstmt.executeQuery();
// getMetaData() 检索此 ResultSet对象列的数量,类型和属性。 getColumnCount() 返回ResultSet对象列的数量
if(rs.next()){
Account account = new Account();
account.setCardID(rs.getString("cardID"));
account.setName(rs.getString("name"));
account.setBalance(rs.getDouble("balance"));
return account;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//根据id查询账户
@Override
public Account selectByCardID(String cardID) {
// Connection 接口代表与特定的数据库连接 并在连接上下文中执行sql 语句
Connection conn= getConn();
String sql="select * from account where cardID = '" + cardID + "' ";
//PreparedStatement类型执行动态的sql语句 Statement 执行静态语句
PreparedStatement pstmt;
try {
//执行sql语句
pstmt = conn.prepareStatement(sql);
// 执行给定的sql语句返回单个 ResultSet 对象
ResultSet rs = pstmt.executeQuery();
// getMetaData() 检索此 ResultSet对象列的数量,类型和属性。 getColumnCount() 返回ResultSet对象列的数量
if(rs.next()){
Account account = new Account();
account.setCardID(rs.getString("cardID"));
account.setName(rs.getString("name"));
account.setBalance(rs.getDouble("balance"));
return account;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//更新账户余额
@Override
public int updateAccount(Account b) {
// Connection 接口代表与特定的数据库连接 并在连接上下文中执行sql 语句
Connection conn= getConn();
int i=0;
String sql = "update account set balance='" + b.getBalance() + "' where cardID='" + b.getCardID() + "'";
// PreparedStatement 执行动态的sql语句 如增删改
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
//执行动态的sql语句 包含insert update delete
i = pstmt.executeUpdate();
if(i>0) {
//System.out.println("修改成功!");
}else {
//System.out.println("未找到需要修改的数据!");
}
// 关闭
pstmt.close();
//关闭数据库连接
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
//开户信息保存
@Override
public int insertAccount(Account b) {
b.setBalance(-10.0);
Connection conn = getConn();
int i=0;
// sql 语句的插入的数据属性
String sql="insert into account(cardID,name,password,balance) values(?,?,?,?)";
PreparedStatement pstmt;
try {
//执行 动态的sql 语句
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, b.getCardID());
pstmt.setString(2, b.getName());
pstmt.setString(3, b.getPassword());
pstmt.setDouble(4, b.getBalance());
i=pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
猜你喜欢
- 基于Java web(springboot+layui+mysql)实现的医院药品管理系统_C50351
- 一个美观大方的商城前端页面HTML模板免费分享_C50225
- 控制台实现JAVA新闻管理系统分角色管理_S7502
- 基于SSM+VUE+mysql实现药品管理系统源码_C50352
- 基于Java web(jsp+servlet+mysql)实现简单的用户信息增删改查管理_C50481
- 基于SSM+bootstrap实现用户信息分页显示C5017
- 基于SSM+Mysql实现人力资源管理系统管理系统_C50448
- 基于SSM+mysql实现高校在线报修管理系统毕业设计源码_C50380
- 基于javaweb数字产品交易商城平台源码下载_C5107
- 基于ssm实现的商品信息商品分类增删改查_C50248
相关代码
- 推荐 基于Java web(jsp+servlet+mysql)实现学生教师信息管理系统源码_C50436
- 推荐 基于Java web(jsp+servlet+mysql)实现在线学习管理系统源码_C50423
- 推荐 基于Springboot+vue+mysql实现的前后端分离保险在线预约管理系统_C50228
- 推荐 基于Java web(jsp+servlet+mysql)实现手机商城管理系统源码_C50454
- 推荐 基于springboot+mysql实现图书商城管理系统_C50273
- 推荐 基于SSM实现简单的登录注册及校园二手购物商城首页_C50235
- 推荐 基于SSM+mysql实现在线商城管理系统源码分享_C50218
- 推荐 JSP+mysql实现新生报到管理系统_C50303
- 推荐 基于Java web(jsp+servlet)实现的拍卖管理系统前端及商品发布页面_C50252
- 推荐 基于Java web(jsp+servlet+mysql)实现人事管理系统源码_C50499