主方法用着演示执行的效果
package main;
import service.* ;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
ServiceATM atm = new ServiceATM() ;
Scanner input = new Scanner(System.in);
System.out.println("请您输入名字\n\t开户输入———>开户");
String name = input.nextLine();
if (name.equals("开户")){
System.out.println("请输入名字");
String newName = input.nextLine();
System.out.println("请输入密码");
String newPssword = input.nextLine();
System.out.println("请输入存储金额");
String newBalance = input.nextLine();
System.out.println( atm.addUser(newName,newPssword,newBalance));
}else {
System.out.println("请输入密码");
String password = input.nextLine();
String result = atm.login(name,Integer.parseInt(password));
System.out.println(result);
if (result.equals("登录成功")){
System.out.println("欢迎来到冯老板银行系统");
ck:while(true){
System.out.println("查询余额请输入1, 转账请输入2,存款请输入3,取款请输入4,销户请输入5,退出请输入0");
String value = input.nextLine();
switch(value){
case "1" :
System.out.println("您的余额为:"+atm.queryBalance(name) );
break ;
case "2" :
System.out.println("请输入收款人名字");
String inName = input.nextLine();
System.out.println("请输入转账金额");
String money = input.nextLine();
System.out.println(atm.turnMoney(name,inName,money));
break ;
case "3":
System.out.println("请输入存款金额");
String addMoney = input.nextLine();
System.out.println(atm.addBalance(name,addMoney));
break ;
case "4":
System.out.println("请输入取款金额");
String getMoney = input.nextLine();
System.out.println(atm.getBalance(name,getMoney));
break ;
case "5":
System.out.println(atm.deleteUser(name));
case "0":
break ck;
default:
System.out.println("对不起您输入的序号有误请重新输入");
}
}
}
}
}
}
dao层操作数据
package dao;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import domain.Atm;
import java.sql.*;
public class DaoATM {
//读写数据层
//设计一个方法将数据库中存储的用户信息读取出来
//将读取出来的一个用户的数据存储在一个对象中
private String forName = "com.mysql.jdbc.Driver" ;
private String url = "jdbc:mysql://localhost:3306/ccc?characterEncoding=utf8&verifyServerCertificate=false&useSSL=true" ;
private String user = "root" ;
private String password = "root" ;
//读数据
public Atm getUser (String name){
Atm atm =null ;
Connection connection = null ;
PreparedStatement preparedStatemen = null ;
ResultSet resultSet = null ;
String sql = "SELECT ANAME,PASSWORD,BALANCE FROM ATM WHERE ANAME = '"+name+"'" ;
try {
//1.导包
//2.加载类驱动
Class.forName(forName) ;
//3.获取连接
connection = DriverManager.getConnection(url,user,password);
//4.创建状态参数
preparedStatemen = connection.prepareStatement(sql);
//5.执行操作
resultSet = preparedStatemen.executeQuery();
if (resultSet.next()){//如果查询到一条记录 将查询到数据存入atm对象中
atm =new Atm (resultSet.getString("aname"),resultSet.getInt("password"),resultSet.getFloat("balance"));
}
return atm ; //将存有一个一个用户信息的atm对象返回
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(resultSet!=null){
resultSet.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatemen!=null){
preparedStatemen.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null){
connection.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return atm ;
}
//修改数据
public void setUser (Atm atm){
Connection connection = null ;
PreparedStatement preparedStatemen = null ;
String sql = "UPDATE ATM SET PASSWORD=?,BALANCE=? WHERE ANAME=? " ;
try {
//1.导包
//2.加载类驱动
Class.forName(forName) ;
//3.获取连接
connection = DriverManager.getConnection(url,user,password);
//4.创建状态参数
preparedStatemen = connection.prepareStatement(sql);
//通PreparedStatement类中的set给sql语句中?号赋值
preparedStatemen.setInt(1,atm.getPassword());
preparedStatemen.setFloat(2,atm.getBalance());
preparedStatemen.setString(3,atm.getName());
//5.执行操作
preparedStatemen.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(preparedStatemen!=null){
preparedStatemen.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null){
connection.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除数据
public void deleteUser (String name){
Connection connection = null ;
PreparedStatement preparedStatemen = null ;
ResultSet resultSet = null ;
String sql = "DELETE FROM ATM WHERE ANAME = '"+name+"'" ;
try {
//1.导包
//2.加载类驱动
Class.forName(forName) ;
//3.获取连接
connection = DriverManager.getConnection(url,user,password);
//4.创建状态参数
preparedStatemen = connection.prepareStatement(sql);
//5.执行操作
preparedStatemen.execute() ;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(resultSet!=null){
resultSet.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatemen!=null){
preparedStatemen.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null){
connection.close() ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//新增数据
public void addUser (Atm atm) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatemen = null;
ResultSet resultSet = null;
String sql = "INSERT INTO ATM VALUES(?,?,?)";
try {
//1.导包
//2.加载类驱动
Class.forName(forName);
//3.获取连接
connection = DriverManager.getConnection(url, user, password);
//4.创建状态参数
preparedStatemen = connection.prepareStatement(sql);
preparedStatemen.setString(1, atm.getName());
preparedStatemen.setInt(2, atm.getPassword());
preparedStatemen.setFloat(3, atm.getBalance());
//5.执行操作
preparedStatemen.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (preparedStatemen != null) {
preparedStatemen.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Service层负责数据处理
package service;
import dao.DaoATM;
import domain.Atm;
import java.sql.SQLException;
public class ServiceATM {
//数据处理层 需要dao层的支持
private DaoATM dao = new DaoATM() ;
//设计一个登陆方法
public String login(String name,Integer password ){
//通过名字向数据库获取一条记录
Atm atm = dao.getUser(name);
//对象不等于空证明名字正确
if (atm!=null&&atm.getPassword().equals(password)){
return "登录成功" ;
}
return "名字或密码错误" ;
}
//设计一个查询余额方法
public Float queryBalance(String name){
return this.dao.getUser(name).getBalance();
}
//设计一个存款方法
public String addBalance(String name,String money){
Atm user = this.dao.getUser(name);
String result = "存款失败" ;
try{
if (Float.parseFloat(money)>0&&user!=null){
user.setBalance(Float.parseFloat(money)+user.getBalance());
dao.setUser(user);
result = "存款成功" ;
}
}catch (NumberFormatException nfe){
result = "请输入正确金额" ;
}
return result ;
}
//设计一个方法取款
public String getBalance(String name,String money){
Atm user = this.dao.getUser(name) ;
String result = "取款失败" ;
try{
float newMoney = Float.parseFloat(money);
if (user.getBalance()>=newMoney&&newMoney>0){
user.setBalance(user.getBalance()-newMoney);
dao.setUser(user);
result = "取款成功" ;
}
}catch (NumberFormatException nfe){
result = "请输入正确金额" ;
}
return result ;
}
//设计一个方法转账
public String turnMoney(String outName,String inName,String money){
Atm inUser = dao.getUser(inName);
String result = "转账失败" ;
if (inUser!=null&&this.getBalance(outName,money).equals("取款成功")){
this.addBalance(inName,money);
result = "转账成功" ;
}
return result ;
}
//设计一个方法销户
public String deleteUser(String name){
Atm user = dao.getUser(name);
String result = "销户失败" ;
if (user.getBalance()==0){
dao.deleteUser(name);
result = "销户成功" ;
}
return result ;
}
//设计一个方法开户
public String addUser(String name,String password,String money){
String result = "开户成功" ;
try{
Atm atm = new Atm(name,Integer.parseInt(password),Float.parseFloat(money));
dao.addUser(atm);
}catch(NumberFormatException nfe){
result = "填写信息有误" ;
} catch (SQLException e) {
result = "用户名已存在" ;
}
return result ;
}
}
用来存储数据的实体类
package domain;
public class Atm {
private String name ;
private Integer password ;
private Float balance ;
public Atm(String name, String password, Float newBalance){}
public Atm(String name, Integer password, Float balance){
this.name = name ;
this.password = password ;
this.balance = balance ;
}
public void setName(String name){
this.name = name ;
}
public void setPassword(Integer password ){
this.password = password ;
}
public void setBalance(Float balance){
this.balance = balance ;
}
public String getName(){
return this.name;
}
public Integer getPassword(){
return this.password ;
}
public Float getBalance(){
return this.balance;
}
}