具体要求: 1.在MySQL数据库中建立名为bank的数据库,在bank库中建立银行储户表account存储储户信息。 2. 在eclipse下建立项目。 3.在上述你创建的项目内,针对account表编写实体类Account。 4. 编写数据访问接口AccountDao及其实现类AccountDaoImpl;业务接口AccountService及其实现类AccountServiceImpl。
实现以下业务需求: 1)储户开户功能,应输入cardID,姓名,密码,初次开户余额为交易额-10(卡费)。 2)储户根据cardID及password登录系统,只有账号和密码都正确才算登录成功,否则应给出“该储户不存在”或“密码错误”等提示。 3)成功登录系统后完成存款操作,存款成功后应提示“存款成功”! 4)成功登录系统后完成取款操作,当取款金额>账户余额时,应提示 “余额不足”。取款成功应提示“取款成功!” 5)成功登录系统后完成转账操作,转账给他人,应输入他人的cardID,如果账户id有误(不存在),应提示“该储户不存在!”;应保证你的转帐金额<=你的余额,注意两人账户余额的更新,转账成功后,应提示“你向XXX(账户姓名)成功转账XXX(具体金额)元”。 5.编写用户操作类AccountTest,可以给出具体的选项操作完成以上功能,如(1.开户2.存款3.取款4.转账5.退卡)。无论每个储户来银行办理什么具体业务,都首先应该欢迎,可打印“欢迎光临××银行”,当储户退卡离开,都应该表示欢迎其下次再来,可打印“感谢您的光临,欢迎您下次再来!”
Account:
import java
.util
.Scanner
;
public class Account {
private int cardID
;
private String name
;
private String password
;
private double balance
=0;
private double many
=0;
public int getCardID() {
return cardID
;
}
public void setCardID(int cardID
) {
this.cardID
= cardID
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public String
getPassword() {
return password
;
}
public void setPassword(String password
) {
this.password
= password
;
}
public double getBalance() {
return balance
;
}
public void setBalance(double balance
) {
this.balance
= balance
;
}
public double getMany() {
return many
;
}
public void setMany(double many
) {
this.many
= many
;
}
public Account() {
super();
}
public Account(int cardID
, String name
, String password
, double balance
) {
super();
this.cardID
= cardID
;
this.name
= name
;
this.password
= password
;
this.balance
= balance
;
}
}
AccountDao:
import po
.Account
;
public interface AccountDao {
int addAccount(Account account
);
Account
depositAccount(int cardID
,double many
);
Account
withdrawalAccount(int cardID
,double many
);
Account
transferAccount(int cardID
,double many
,int cardID2
);
Account
foundAccount(int cardID
);
Account
lookAccount(int cardID
);
}
AccountDaoImpl:
package jnxyjsj
.lyh
.dao
;
import java
.sql
.Connection
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import com
.mysql
.jdbc
.PreparedStatement
;
import po
.Account
;
public class AccountDaoImpl implements AccountDao{
Connection con
=null
;
PreparedStatement psmt
=null
;
PreparedStatement psmt2
=null
;
PreparedStatement psmt3
=null
;
ResultSet rs
=null
;
@Override
public int addAccount(Account account
) {
int i
=0;
try {
con
=DButil
.getConnection();
String sql
="insert into account(cardID,name,password,balance) values(?,?,?,-10)";
psmt
=con
.prepareStatement(sql
);
psmt
.setInt(1, account
.getCardID());
psmt
.setString(2, account
.getName());
psmt
.setString(3, account
.getPassword());
i
=psmt
.executeUpdate();
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
finally {
if(psmt
!=null
) {
try {
psmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
if(con
!=null
) {
try {
con
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
}
return i
;
}
@Override
public Account
depositAccount(int cardID
, double many
) {
try {
con
=DButil
.getConnection();
String sql
="update account set balance=balance+? where cardID=?";
psmt
=con
.prepareStatement(sql
);
psmt
.setDouble(1,many
);
psmt
.setInt(2,cardID
);
int i
=psmt
.executeUpdate();
if(i
>0) {
System
.out
.println("存款成功!");
}else {
System
.out
.println("存款失败!");
}
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
finally {
if(psmt
!=null
) {
try {
psmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
if(con
!=null
) {
try {
con
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
}
return null
;
}
@Override
public Account
withdrawalAccount(int cardID
, double many
) {
try {
con
=DButil
.getConnection();
String sql
="select balance from account where cardID=?";
psmt
=con
.prepareStatement(sql
);
psmt
.setInt(1, cardID
);
rs
=psmt
.executeQuery();
double a
=0;
while(rs
.next()) {
a
=rs
.getDouble("balance");
}
if(a
>=many
) {
String sql2
="update account set balance=balance-? where cardID=?";
psmt2
=con
.prepareStatement(sql2
);
psmt2
.setDouble(1, many
);
psmt2
.setInt(2, cardID
);
int i
=psmt2
.executeUpdate();
if(i
>0) {
System
.out
.println("取款成功!");
}else {
System
.out
.println("取款失败!");
}
}else {
System
.out
.println("您的余额不足!");
}
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
finally {
if(psmt
!=null
) {
try {
psmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(psmt2
!=null
) {
try {
psmt2
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(con
!=null
) {
try {
con
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
@Override
public Account
transferAccount(int cardID
, double many
, int cardID2
) {
try {
con
=DButil
.getConnection();
String sql
="select balance from account where cardID=?";
psmt
=con
.prepareStatement(sql
);
psmt
.setInt(1, cardID
);
rs
=psmt
.executeQuery();
double a
=0;
while(rs
.next()) {
a
=rs
.getDouble("balance");
}
if(a
>=many
) {
String sql2
="update account set balance=balance-? where cardID=?";
con
.setAutoCommit(false);
psmt2
=con
.prepareStatement(sql2
);
psmt2
.setDouble(1, many
);
psmt2
.setInt(2, cardID
);
int from
=psmt2
.executeUpdate();
String sql3
="update account set balance=balance+? where cardID=?";
psmt3
=con
.prepareStatement(sql3
);
psmt3
.setDouble(1, many
);
psmt3
.setInt(2, cardID2
);
int to
=psmt3
.executeUpdate();
if(from
==1 && to
==1) {
System
.out
.println("您向账户: "+cardID2
+" 成功转账: "+many
+"元!");
con
.commit();
}else {
System
.out
.println("转账失败!");
con
.rollback();
}
}else {
System
.out
.println("余额不足!");
}
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
finally {
if(con
!=null
) {
try {
con
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(rs
!=null
) {
try {
rs
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(psmt
!=null
) {
try {
psmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(psmt2
!=null
) {
try {
psmt2
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(psmt3
!=null
) {
try {
psmt3
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
@Override
public Account
foundAccount(int cardID
) {
Account account
=null
;
try {
con
=DButil
.getConnection();
String sql
="select * from account where cardID=?";
psmt
=con
.prepareStatement(sql
);
psmt
.setInt(1, cardID
);
rs
=psmt
.executeQuery();
if(rs
.next()) {
account
=new Account();
account
.setCardID(rs
.getInt(1));
account
.setName(rs
.getString(2));
account
.setPassword(rs
.getString(3));
account
.setBalance(rs
.getDouble(4));
}
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
return account
;
}
@Override
public Account
lookAccount(int cardID
) {
try {
con
=DButil
.getConnection();
String sql
="select * from account where cardID=?";
psmt
=con
.prepareStatement(sql
);
psmt
.setInt(1, cardID
);
System
.out
.println("ID账号: "+rs
.getInt("cardID")+"\t"+" 姓名: "+rs
.getString("name")+"\t"+" 账户余额: "+rs
.getDouble("balance"));
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (SQLException e
) {
e
.printStackTrace();
}
finally {
if(psmt
!=null
) {
try {
psmt
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
if(con
!=null
) {
try {
con
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
}
AccountService:
import po
.Account
;
public interface AccountService {
boolean add(int cardID
,String name
,String password
);
boolean login(int cardID
,String password
);
boolean deposit(int cardID
,double many
);
boolean withdrawal(int cardID
,double many
);
boolean transfer(int cardID
,double many
,int cardID2
);
boolean look(int cardID
);
boolean exist(int cardID2
);
}
AccountServiceImpl:
import dao
.AccountDao
;
import dao
.AccountDaoImpl
;
import po
.Account
;
public class AccountServiceImpl implements AccountService{
AccountDao dao
=new AccountDaoImpl();
@Override
public boolean add(int cardID
, String name
, String password
) {
Account account
=dao
.foundAccount(cardID
);
if(account
==null
) {
account
=new Account(cardID
,name
,password
,-10);
int i
=dao
.addAccount(account
);
if(i
>0) {
System
.out
.println("开户成功!");
}else {
System
.out
.println("开户失败!");
}
}else {
System
.out
.println("该账户已经被注册!");
}
return false;
}
@Override
public boolean login(int cardID
, String password
) {
Account account
=dao
.foundAccount(cardID
);
if(account
!=null
) {
if(account
.getPassword().equals(password
)) {
return true;
}else {
System
.out
.println("密码错误!");
}
}else {
System
.out
.println("该用户不存在!");
}
return false;
}
@Override
public boolean deposit(int cardID
, double many
) {
Account account
=dao
.depositAccount(cardID
,many
);
return true;
}
@Override
public boolean withdrawal(int cardID
, double many
) {
Account account
=dao
.withdrawalAccount(cardID
,many
);
return true;
}
@Override
public boolean transfer(int cardID
, double many
, int cardID2
) {
Account account
=dao
.transferAccount(cardID
,many
,cardID2
);
return true;
}
@Override
public boolean look(int cardID
) {
Account account
=dao
.lookAccount(cardID
);
return true;
}
@Override
public boolean exist(int cardID2
) {
Account account
=dao
.foundAccount(cardID2
);
if(account
!=null
) {
return true;
}
System
.out
.println("该用户不存在!");
return false;
}
}
AccountTest:
import java
.util
.Scanner
;
import service
.AccountService
;
import service
.AccountServiceImpl
;
public class AccountTest {
public static void main(String
[] args
) {
System
.out
.println("--------欢迎光临济宁银行--------");
AccountService service
=new AccountServiceImpl();
Scanner scanner
=new Scanner(System
.in
);
while(true) {
System
.out
.println("1 : 开户");
System
.out
.println("2 : 登录");
System
.out
.println("3 : 退卡");
System
.out
.println("请按照提示输入相应数字以实现相应功能:");
int num
=scanner
.nextInt();
if(num
==1) {
System
.out
.println("请输入您的cardID: ");
int cardID
=scanner
.nextInt();
System
.out
.println("请输入您的密码: ");
String password
=scanner
.next();
System
.out
.println("请输入您的姓名: ");
String name
=scanner
.next();
service
.add(cardID
, name
, password
);
}
if(num
==2) {
System
.out
.println("请输入您的cardID: ");
int cardID
=scanner
.nextInt();
System
.out
.println("请输入您的密码: ");
String password
=scanner
.next();
if(service
.login(cardID
, password
)) {
System
.out
.println("登录成功!");
System
.out
.println("您的帐户信息如下:");
service
.look(cardID
);
while(true) {
System
.out
.println("1 : 存款");
System
.out
.println("2 : 取款");
System
.out
.println("3 : 转账");
System
.out
.println("4 : 退卡");
System
.out
.println("请按照提示输入相应数字以实现相应功能:");
int select
=scanner
.nextInt();
switch(select
) {
case 1: {
System
.out
.println("请输入存款金额:");
double many
=scanner
.nextDouble();
service
.deposit(cardID
, many
);
}break;
case 2: {
System
.out
.println("请输入取款金额:");
double many
=scanner
.nextDouble();
service
.withdrawal(cardID
, many
);
}break;
case 3: {
System
.out
.println("请输入被转账人cardID:");
int cardID2
=scanner
.nextInt();
if(service
.exist(cardID2
)) {
System
.out
.println("请输入转账金额:");
double many
=scanner
.nextDouble();
service
.transfer(cardID
, many
, cardID2
);
}
}break;
case 4:
System
.out
.println("----感谢您的光临,欢迎您下次再来!----");
System
.exit(0);
default:
System
.out
.println("您输入的内容有误,请重新输入!");
break;
}
}
}
}
if(num
==3) {
System
.out
.println("----感谢您的光临,欢迎您下次再来!----");
break;
}
}
}
}
DBUtil:
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.SQLException
;
public class DBUtil {
public static Connection
getConnection() throws ClassNotFoundException
, SQLException
{
Class
.forName("org.gjt.mm.mysql.Driver");
Connection connection
= DriverManager
.getConnection("jdbc:mysql://localhost/bank", "root", "");
return connection
;
}
}