1、导入对应的jar包 这里推荐jdbc6.java,创建对应的数据访问层(Dao)、业务层(Service)、控制层(Action)、测试层(Test)、以及网络层(Util)五个包,连接的是oracle数据库。由于目前哈没有学习javaweb项目部署,所以现在只能在控制台模拟登录选项。
MVC设计模型是一种使用 Model View Controller( 模型-视图-控制器)设计创建 Web 应用程序的模式。
2、网络层Util
package UTIL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class UTIL {
public static Statement getstatement()throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.147:1521:orcl", "scott", "tiger"); //创建连接的connection
Statement st=conn.createStatement();//将数据是送入数据库
return st;//接收返回
}
}
业务层Service
package service;
import ATM.ATM;
import ATMDao.ATMDao;
public class ATMservice {
public void cunmoney(String username,int money ) throws Exception{
ATMDao dao= new ATMDao();
ATM atm=dao.load(username);
atm.setBalance(atm.getBalance()+money);
dao.updateATM(atm.getUsername(),atm.getBalance());
}
public void qumoney(String username,int money) throws Exception {
ATMDao dao=new ATMDao();
if (money< querybalance(username)) {
System.out.println("可以正常取款");
ATM atm =dao.load(username);//查询取款人
atm.setBalance(atm.getBalance()-money);//取款后剩下的余额
dao.updateATM(atm.getUsername(), atm.getBalance());//更新取款人和剩余的钱
}
}
public void zhuanzhang(String zcusername,int money,String zrusername) throws Exception {
ATMDao dao =new ATMDao();
if (money <=querybalance(zcusername)) {
ATM atm =dao.load(zcusername);
atm.setBalance(atm.getBalance()-money);
dao.updateATM(zcusername,atm.getBalance());
ATM atm2=dao.load(zrusername);
atm2.setBalance(atm2.getBalance()+money);
dao.updateATM(zrusername, atm2.getBalance());
}
}
public int querybalance(String username ) throws Exception {
ATMDao dao= new ATMDao();
// TODO Auto-generated method stub
return dao.load(username).getBalance();
}
}
package service;
import ATM.ATM;
import ATMDao.LoginDao;
public class Loginservice {
public boolean loginservice(String username,String password) throws Exception {
boolean flag=false;
LoginDao dao=new LoginDao();
ATM atm = dao.LoginDao(username);
if(atm!=null) {
if (password.equals(atm.getPassword())) {
flag = true;
System.out.println("登陆成功");
}
}
return flag;
}
}
控制层Action
package Action;
import service.ATMservice;
import service.Loginservice;
public class ActionLogin {
public void Actionlogin(String username,String password) throws Exception {
Loginservice ls=new Loginservice();
boolean flag=ls.loginservice(username, password);
if (flag) {
System.out.println("登陆成功");
}else {
System.out.println("登录失败");
}
}
}
数据访问层Dao 里面用来写羡慕所用的sql语句,因为存取款转账相当于update操作,座椅暂时把他们归于一个方法中写,另外的查询方法另外写一个方法load
package ATMDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import ATM.ATM;
import UTIL.UTIL;
public class ATMDao {
public void updateATM(String username,int money ) throws Exception{
String sql="update tb_user set balance='"+money+"' where username ='"+username+"' ";
UTIL.getstatement().executeUpdate(sql);
}
public ATM load(String username ) throws Exception {
ATM atm = new ATM();
String sql="select * from tb_user where username='"+username+"'";
ResultSet rs=UTIL.getstatement().executeQuery(sql);
if (rs.next()) {
String uname=rs.getString("username");
String pwd=rs.getString("password");
int balance=rs.getInt("balance");
atm.setUsername(uname);
atm.setPassword(pwd);
atm.setBalance(balance);
}
return atm;
}
}
package ATMDao;
import ATM.ATM;
public class LoginDao {
public ATM LoginDao(String username) throws Exception {
ATMDao dao=new ATMDao();
return dao.load(username);
}
}
Test类
package Test;
import java.util.Scanner;
import Action.ActionLogin;
import service.ATMservice;
import service.Loginservice;
public class Test {
public static void main(String args[]) throws Exception {
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.next();
ActionLogin ac=new ActionLogin();
ac.Actionlogin(username, password);
System.out.println("欢迎进入"+username +"ATM机界面");
a:while(true ) {
ATMservice atMservice=new ATMservice();
System.out.println("请根据提示进行操作: 1.存款 2.取款 3.查询 4.转账");
int num=sc.nextInt();
switch (num ) {
case 1:
System.out.println("欢迎来到存款界面: ");
System.out.println("请输入您要存入的金额:");
int num1=sc.nextInt();
atMservice.cunmoney(username, num1 );
System.out.println("存款完毕,请选择其他业务 ");
System.out.println("是否继续 : 按1继续 按0退出");
int state1 = sc.nextInt();
if(state1 == 0){
break a;
}else{
continue;
}
case 2:
System.out.println("欢迎来到取款界面: ");
System.out.println("请输入您要取走的金额:");
int num2=sc.nextInt();
atMservice.qumoney(username, num2);
System.out.println("取款完毕,请选择其他业务 ");
System.out.println("是否继续 : 按1继续 按0退出");
int state2 = sc.nextInt();
if(state2 == 0){
break a;
}else{
continue;
}
case 3:
System.out.println("欢迎来到查询界面: ");
System.out.println("请输入您要查询的金额:");
int num3=sc.nextInt();
atMservice.querybalance(username);
System.out.println("是否继续 : 按1继续 按0退出");
int state3 = sc.nextInt();
if(state3 == 0){
break a;
}else{
continue;
}
case 4:
System.out.println("欢迎来到存款界面: ");
System.out.println("请输入您要存入的金额:");
int num4=sc.nextInt();
System.out.println("存款完毕,请选择其他业务 ");
break a;
}
}
// if (num ==1) {
//
// System.out.println("欢迎来到存款界面: ");
// System.out.println("请输入您要存入的金额:");
// int num1=sc.nextInt();
// System.out.println("存款完毕,请选择其他业务 1.存款 2.取款 3.查询 4.转账 ");
// break a;
// }
//
// if (num ==2) {
//
//
// System.out.println("欢迎来到取款界面: ");
// System.out.println("请输入您要取出的金额:");
// int num2=sc.nextInt();
// System.out.println("取款完毕,请选择其他业务 1.存款 2.取款 3.查询 4.转账");
//
// }
// if (num ==3) {
//
//
// System.out.println("欢迎来到存款界面: ");
// System.out.println("请输入您要存入的金额:");
// int num1=sc.nextInt();
// System.out.println("存款完毕,请选择其他业务");
//
// }
// if (num ==4) {
//
//
// System.out.println("欢迎来到存款界面: ");
// System.out.println("请输入您要存入的金额:");
// int num1=sc.nextInt();
// System.out.println("存款完毕,请选择其他业务");
//
// }
// }
// atMservice.cunmoney("张三", 1000);
// System.out.println(atMservice.querybalance("张三"));
//
//
// atMservice.qumoney("李四", 200);
// System.out.println(atMservice.querybalance("李四"));
//
//
// atMservice.zhuanzhang("李四", 50, "王五");
// System.out.println(atMservice.querybalance("李四"));
// System.out.println(atMservice.querybalance("王五"));
}
}
现在多学的基本上就这些,后期根据自己学习的进度再回更新javaweb项目,如有错误,欢迎指正。