import java.sql.*;
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供膠州網(wǎng)站建設(shè)、膠州做網(wǎng)站、膠州網(wǎng)站設(shè)計(jì)、膠州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、膠州企業(yè)網(wǎng)站模板建站服務(wù),10年膠州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
import java.util.*;
public class ATM1 {
String code;
int pass;
double money;
int i=1;
//檢查登錄
public void checkLogin(){
int i=1;
while(i<=3){
System.out.print("請(qǐng)輸入您的卡號(hào):");
Scanner sc=new Scanner(System.in);
String code_=sc.nextLine();
System.out.print("請(qǐng)輸入您的密碼:");
int pass_=sc.nextInt();
try{
//加載驅(qū)動(dòng)
Class.forName("com.MySQL.jdbc.Driver");
//建立連接
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
//創(chuàng)建sql傳送對(duì)象
Statement stmt = conn.createStatement();
//將sql語(yǔ)句通過(guò)sql傳送對(duì)象傳送到數(shù)據(jù)庫(kù)并執(zhí)行,返還結(jié)果集
String sql = "select * from account where code='"+code_+"'and pass="+pass_;
ResultSet rs = stmt.executeQuery(sql);
//當(dāng)賬號(hào)、密碼與sql中的賬號(hào)與密碼相對(duì)應(yīng)的時(shí)候
//把sql中的相關(guān)數(shù)據(jù)傳送到目前變量以便進(jìn)行數(shù)據(jù)操作
if(rs.next()){
code = rs.getString(1);
pass = rs.getInt(2);
money = rs.getDouble(3);
loadSys();
}
rs.close();
stmt.close();
conn.close();
}
//捕獲異常
catch (Exception e){
System.out.println(e);
}
//出現(xiàn)三次錯(cuò)誤之后提示
i++;
if( i == 3){
System.out.print("您已經(jīng)輸錯(cuò)三次密碼,該卡已經(jīng)被鎖,請(qǐng)及時(shí)到相關(guān)網(wǎng)點(diǎn)咨詢!");
}
}
}
//保存數(shù)據(jù)
//注意;下面的關(guān)鍵字( "pass、code、money")要與數(shù)據(jù)庫(kù)中的名字一樣,避免出現(xiàn)錯(cuò)誤
public void saveDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
Statement stmt = conn.createStatement();
String sql="update account set pass ="+pass+" where code='"+code+"'";
//dode為String型,所以要用‘’括起來(lái)
String sql1="update account set money="+money+" where code='"+code+"'";
stmt.executeUpdate(sql); //update操作進(jìn)行數(shù)據(jù)更新
stmt.executeUpdate(sql1);
stmt.close();
conn.close();
}
catch (Exception e){
System.out.println(e);
}
}
//歡迎界面
public static void welcome(){
System.out.println("!*****************************************!");
System.out.println("!*************歡迎使用華夏銀行*************!");
System.out.println("!*****************************************!");
}
//系統(tǒng)主界面
public void loadSys(){
System.out.println(".------------------------------------.");
System.out.println("1 查詢余額 存款 2");
System.out.println("3 取款 修改密碼 4");
System.out.println("5 退出 ");
System.out.println(".------------------------------------.");
System.out.print("請(qǐng)輸入相應(yīng)的功能選項(xiàng)數(shù)字:");
Scanner sz=new Scanner(System.in);
int num=sz.nextInt();
switch(num){
case 1:
chaxun();
break;
case 2:
cunkuan();
break;
case 3:
qukuan();
break;
case 4:
xiugai();
break;
case 5:
quit();
break;
default:
System.out.println("您輸入的數(shù)字有誤!");
loadSys();
}
}
//查詢余額
public void chaxun(){
System.out.println("您卡上的余額為:" +money);
loadSys();
}
//存款
public void cunkuan(){
System.out.print("請(qǐng)輸入存款金額:");
Scanner ck=new Scanner(System.in);
double money1=ck.nextDouble();
money=money+money1;
saveDb();
System.out.println("您卡上的余額為:" +money);
System.out.println("請(qǐng)收好您的卡!");
loadSys();
}
//取款
public void qukuan(){
System.out.print("請(qǐng)輸入取款金額:");
Scanner qk=new Scanner(System.in);
double money2=qk.nextDouble();
if(money2>money){
System.out.println("您的余額不足!");
qukuan();
}else{
money=money-money2;
saveDb();
System.out.println("您卡上的余額為:" +money);
System.out.println("請(qǐng)收好您的現(xiàn)金!");
loadSys();
}
}
//修改密碼
public void xiugai(){
int cs = 0;
System.out.print("請(qǐng)輸入原密碼:");
Scanner mm=new Scanner(System.in);
int pass1=mm.nextInt();
System.out.print("請(qǐng)輸入新密碼:");
int pass2=mm.nextInt();
System.out.print("請(qǐng)?jiān)俅屋斎胄旅艽a:");
int pass3=mm.nextInt();
if(pass==pass1&& pass2==pass3){
System.out.println("修改密碼成功!");
pass=pass2;
saveDb();
loadSys();
}else{
if(cs<2){
System.out.print("您輸入的密碼有誤!");
cs++;
xiugai();
}else{
quit();
}
}
}
//退出
public void quit(){
System.exit(1);
}
//主函數(shù)
public static void main(String args[]){
/*try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/atmdb","root","root");
Statement stmt = conn.createStatement();
String sql = "select * from account";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
String code = rs.getString(1);
int pass = rs.getInt(2);
double money = rs.getDouble(3);
System.out.println(code+"|"+pass+"|"+money);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
System.out.println(e);
}*/
ATM1 atm = new ATM1();
atm.welcome();
atm.checkLogin();
}
}