首先还是要复习一下,常见的项目包结构
conf 配置文件 类似jdbc.properties
dao orm中的数据访问层
在本类上一个接口,加上一个继承接口的类,主要是接口具有耦合性搞的优点
entity 实体对象
service 也是一个接口,一个类。
SQL 项目相关的sql
util 工具类
view 表现层代码
先看看表现层代码,相当于菜单
package view;
import java.util.Scanner;
import javax.swing.plaf.synth.Region;
import entity.Account;
import service.AccountService;
import service.AccountServiceImpl;
public class AccountView {
private static Scanner sc = new Scanner(System.in);
static AccountService accountService = new AccountServiceImpl();
public static void main(String[] args) {
while(true){
System.out.println("*************欢迎进入银行系统***********");
System.out.println("1、存钱*************************2、取钱");
System.out.println("3、查询当前余额********************4、转账");
System.out.println("5、注册账户********************6、更改密码");
System.out.println("7、注销账户********************0、退出登录");
System.out.println("请选择:");
int n = sc.nextInt();
switch(n){
case 1:
//存钱
savemoney();
break;
case 2:
//取钱
drawmoney();
break;
case 3:
//查询当前余额
queryBalance();
break;
case 4:
//转账
transfor();
break;
case 5:
//注册账户
regist();
break;
case 6:
//更改密码
changeWord();
break;
case 7:
//注销账户
cancelAccount();
break;
case 0:
//退出登录
System.out.println("大哥,欢迎下次再来哈!");
System.exit(0);
break;
default:
throw new RuntimeException("老哥,你的输入有错误!");
}
}
}
//注册
public static void regist(){
System.out.println("输入您的姓名:");
String name = sc.next();
System.out.println("输入的您的密码");
String password = sc.next();
System.out.println("大哥再确认下吧");
String pass2 = sc.next();
Account account = new Account(null,name,password,0.0);
try{
accountService.regist(account,pass2);
System.out.println("注册成功");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//查询余额
public static void queryBalance(){
System.out.println("请输入您的账号:");
int cardid = sc.nextInt();
System.out.println("请输入的您的密码");
String password = sc.next();
try{
double balance = accountService.queryBalance(cardid,password);
System.out.println("您的当前余额为" + balance);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//存钱
public static void savemoney(){
System.out.println("请输入您的账号: ");
int id = sc.nextInt();
System.out.println("请输入您的密码: ");
String password = sc.next();
System.out.println("大哥,存款存多少呢:");
double balance = sc.nextDouble();
try{
accountService.saveMoney(id,password,balance);
System.out.println("存款成功!");
}catch(Exception e ){
System.out.println(e.getMessage());
}
}
//取钱
public static void drawmoney(){
System.out.println("请输入您的账号: ");
int id = sc.nextInt();
System.out.println("请输入您的密码: ");
String password = sc.next();
System.out.println("大哥,您要取多少钱呢:");
double balance = sc.nextDouble();
try{
accountService.drawmoney1(id,password,balance);
System.out.println("取钱成功!");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//注销账户
public static void cancelAccount(){
System.out.println("大哥,您要注销账户?yes or no :");
String ye = sc.next();
if(ye.equals("yes")){
System.out.println("请输入您的账号: ");
int id = sc.nextInt();
System.out.println("请输入您的密码: ");
String password = sc.next();
try{
accountService.cancelAccountt(id, password);
System.out.println("您的账号已注销!");
}catch(Exception e){
System.out.println(e.getMessage());
}
}else{
System.out.println("大哥,您的账号已取消创建。");
}
}
//更改密码
public static void changeWord(){
System.out.println("大哥,您是否要进行更改密码操作?yes or no :");
String ye = sc.next();
if(ye.equals("yes")){
System.out.println("请输入您的账号: ");
int id = sc.nextInt();
System.out.println("请输入您的原密码: ");
String password = sc.next();
System.out.println("请输入您的新密码: ");
String nepassword = sc.next();
try{
accountService.changepassword(id, password,nepassword);
System.out.println("您的账号已更改密码!");
}catch(Exception e){
System.out.println(e.getMessage());
}
}else{
System.out.println("大哥,您的账号已取消更改密码");
}
}
//转账功能
public static void transfor(){
System.out.println("请输入您的账号:");
int fromcardid = sc.nextInt();
System.out.println("请输入的您的密码");
String password = sc.next();
System.out.println("钱转给谁呢?请输入她的账号:");
int tocardid = sc.nextInt();
System.out.println("转账的金额");
double money = sc.nextDouble();
try{
accountService.transforyou(fromcardid,password,tocardid,money);
System.out.println("转账完成");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
接着业务逻辑层代码
接口代码:
package service;
import entity.Account;
public interface AccountService {
//业务功能
//开户功能
public void regist(Account account,String pass2);
//查询余额
public double queryBalance(int cardid,String password);
//转账
public void transforyou(int fromcardid,String password,int tocardid,double money);
//存款功能
public void saveMoney(int cardid,String password,double balance);
//取款
public void drawmoney1(int cardid,String password,double balance);
//注销账户
public void cancelAccountt(int cardid,String password);
//更改密码
public void changepassword(int id, String password, String nepassword);
}
核业务逻辑层心代码:
package service;
import java.sql.Connection;
import java.sql.SQLException;
import javax.management.RuntimeErrorException;
import dao.AccountDao;
import dao.AccountdDaoImpl;
import entity.Account;
import util.jdbcUtil3;
public class AccountServiceImpl implements AccountService {
AccountDao accountdDao = new AccountdDaoImpl();
Connection conn = null;
@Override
public void regist(Account account, String pass2){
String name = account.getName();
if(name==null){
throw new RuntimeException("用户名为空");
}
//判断两次密码是否一致
String password = account.getPassword();
if(!password.equals(pass2)){
throw new RuntimeException("两次密码不一致");
}
//创建连接,设置手动提交,控制事务
try {
conn = jdbcUtil3.getConnection();
conn.setAutoCommit(false);
//插入记录
accountdDao.insertAccount(account);
//提交
conn.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException("回滚异常");
}
}finally{
//关闭连接
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("关闭连接异常");
}
}
}
@Override
public double queryBalance(int cardid, String password) {
double balance = 0.0;
Account account = accountdDao.queryAccountByCardid(cardid);
//查看账号是否存在
if(account==null){
throw new RuntimeException("账户不存在");
}
if(!account.getPassword().equals(password)){
throw new RuntimeException("密码输入错误!");
}
try {
conn = jdbcUtil3.getConnection();
balance = account.getBalance();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException("查询连接错误");
}finally{
try {
jdbcUtil3.release(null, null, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭连接异常!");
}
}
return balance;
}
@Override
public void transforyou(int fromcardid, String password, int tocardid, double money) {
//判断我方用户是否存在
Account account1 = accountdDao.queryAccountByCardid(fromcardid);
Account account2 = accountdDao.queryAccountByCardid(tocardid);
if(account1 ==null){
throw new RuntimeException("您好,你的账户为空!");
}
//对方账号是否存在
if(account2==null){
throw new RuntimeException("你好,你要转账的账户不存在!");
}
//判断密码上是否输入正确
if(!account1.getPassword().equals(password)){
throw new RuntimeException("密码不一致");
}
//我方余额是否大于转账金额
if(account1.getBalance()<money){
throw new RuntimeException("余额不足");
}
//可以转账,
account1.setBalance(account1.getBalance()-money);
account2.setBalance(account2.getBalance()+money);
try{
conn = jdbcUtil3.getConnection();
//手动提交
conn.setAutoCommit(false);
accountdDao.updateAccount(account1);
accountdDao.updateAccount(account2);
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("回滚有异常");
}
}finally{
try {
jdbcUtil3.release(null, null, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("释放资源异常。。");
}
}
}
@Override
public void saveMoney(int cardid, String password, double balance) {
//通过账号获取这个账户
Account accountt = accountdDao.queryAccountByCardid(cardid);
//判断账户是否存在
if(accountt==null){
throw new RuntimeException("账户不存在");
}
//检查密码是否正确
if(!accountt.getPassword().equals(password)){
throw new RuntimeException("密码输入不正确!");
}
//可以存款
double bala = balance + accountt.getBalance();
try {
conn = jdbcUtil3.getConnection();
conn.setAutoCommit(false);
accountdDao.savemoneybyCardid(bala,cardid);
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException("回滚异常");
}
}finally{
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("释放资源异常!");
}
}
}
@Override
public void drawmoney1(int cardid, String password, double balance) {
//妈耶,代码基本没啥差别啊
Account accountt = accountdDao.queryAccountByCardid(cardid);
//判断账户是否存在
if(accountt==null){
throw new RuntimeException("账户不存在");
}
//检查密码是否正确
if(!accountt.getPassword().equals(password)){
throw new RuntimeException("密码输入不正确!");
}
//可以存款
double bala = accountt.getBalance() - balance;
try {
conn = jdbcUtil3.getConnection();
conn.setAutoCommit(false);
accountdDao.drawmoneybyCardid(bala,cardid);
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException("回滚异常");
}
}finally{
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("释放资源异常!");
}
}
}
@Override
public void cancelAccountt(int cardid, String password) {
//通过账号获取这个账户
Account accountt = accountdDao.queryAccountByCardid(cardid);
//判断账户是否存在
if(accountt==null){
throw new RuntimeException("账户不存在!");
}
//检查密码是否正确
if(!accountt.getPassword().equals(password)){
throw new RuntimeException("密码输入不正确!");
}
//进行账号注销
try {
conn = jdbcUtil3.getConnection();
conn.setAutoCommit(false);
accountdDao.deleteCardid(cardid);
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException("回滚异常");
}
}finally{
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("释放资源异常!");
}
}
}
@Override
public void changepassword(int id, String password, String nepassword) {
//通过账号获取这个账户
Account accountt = accountdDao.queryAccountByCardid(id);
//判断账户是否存在
if(accountt==null){
throw new RuntimeException("您的账户不存在!");
}
//检查密码是否正确
if(!accountt.getPassword().equals(password)){
throw new RuntimeException("亲,您输入的密码不正确哦!");
}
//进行更改密码操作
try {
conn = jdbcUtil3.getConnection();
conn.setAutoCommit(false);
accountdDao.updateCardid(id,nepassword);
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new RuntimeException("回滚异常");
}
}finally{
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("释放资源异常!");
}
}
}
}
数据访问层 dao 接口和代码
package dao;
import entity.Account;
public interface AccountDao {
//数据插入 注册账号
public void insertAccount(Account account);
//根据卡号查看账户信息 从而查询余额
public Account queryAccountByCardid(int cardid);
//更新信息。进行转账
public void updateAccount(Account account1);
//存款
public void savemoneybyCardid(double balance,int card);
//取款
public void drawmoneybyCardid(double bala, int cardid);
//注销账户
public void deleteCardid(int cardid);
//更改密码操作
public void updateCardid(int id, String nepassword);
}
package dao;
import entity.Account;
import rowmapper.AccountRowMapper;
import rowmapper.RowMapper;
import util.JdbcTemplate;
public class AccountdDaoImpl<T> implements AccountDao {
JdbcTemplate template = new JdbcTemplate();
//多态来创建对象
public void insertAccount(Account account) {
template.update("insert into account(name,password,balance) values(?,?,?)", account.getName(),account.getPassword(),account.getBalance());
}
@Override
public Account queryAccountByCardid(int cardid) {
Account account =(Account) template.queryForObject("select * from account where cardid=? ",new AccountRowMapper(),cardid);
return account;
}
@Override
public void updateAccount(Account account1) {
template.update("update account set balance=? where cardid=?",account1.getBalance(),account1.getCardid());
}
@Override
public void savemoneybyCardid(double balance,int card) {
template.update("update account set balance = ? where cardid =?",balance,card);
}
@Override
public void drawmoneybyCardid(double bala, int cardid) {
template.update("update account set balance = ? where cardid =?", bala , cardid);
}
@Override
public void deleteCardid(int cardid) {
template.update("delete from account where cardid=? ", cardid);
}
@Override
public void updateCardid(int id, String nepassword) {
template.update("update account set password = ? where cardid = ?", nepassword,id);
}
}
局部演示图
先睡了,明天更新。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114776.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...