




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
程序?qū)崿F(xiàn)其實(shí)并不是很難,主要是思路,對(duì)程序的各個(gè)功能的設(shè)計(jì),各個(gè)按鈕的處理事件及按鈕按照邏輯顯示出來(lái)和隱藏。服務(wù)器采用多線程并行機(jī)制,針對(duì)每個(gè)用戶登錄創(chuàng)建一個(gè)線程。
程序代碼
客戶端:
importjavax.swing.*;
import.*;
importjava.io.*;
importjava.util.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.sql.*;
importjava.math.*;
importjavax.swing.*;
classATMWindowsextendsJFrameimplementsActionListener
{
Fontf;
Socketclint=null;
DataOutputStreamout=null;
DataInputStreamin=null;
Connectioncon;
Statementpre;
ResultSetrs;
inti,money;
booleanb=true;//b的取值有0,1,共2個(gè)值分別告訴程序該吧數(shù)字鍵盤(pán)的輸入復(fù)制給username,password,
Stringsusername=null;
Stringspassword=null;
Boxbox1,box2,box3,box11,box12;
JPanelpanel1,panel2;
JLabeltishi;
JTextFieldusername;
JPasswordFieldpassword;
JButtonn[],queding,chaxun,qukuan,tuichu;
ATMWindows(Strings)
{
GraphicsEnvironment
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Stringt[]=ge.getAvailableFontFamilyNames();
f=newFont(t[67],Font.BOLD,16);
n=newJButton[10];
box1=Box.createVerticalBox();
box2=Box.createHorizontalBox();
box3=Box.createVerticalBox();
panel1=newJPanel();
panel2=newJPanel();
tishi=newJLabel("歡迎使用XX銀行!請(qǐng)輸入卡號(hào)。");
tishi.setFont(f);
username=newJTextField(20);
password=newJPasswordField(20);
panel1.setLayout(newGridLayout(3,3));
panel2.setLayout(newGridLayout(4,1));
for(i=1;i<n.length;i++)
{
n[i]=newJButton(""+i);
panel1.add(n[i]);
n[i].addActionListener(this);
}
n[0]=newJButton("0");
panel1.add(n[0]);
n[0].addActionListener(this);
n[0].setPreferredSize(newDimension(20,20));
queding=newJButton("確定");
chaxun=newJButton("查詢");//用戶輸入賬號(hào)和密碼后,點(diǎn)擊查詢按鈕登陸qukuan=newJButton("取款");
tuichu=newJButton("退出");
qukuan.show(false);
chaxun.show(false);//初始化窗口后,賬號(hào)和密碼還沒(méi)輸入,不能用查詢按
鈕登陸
box1.add(tishi);
tishi.setBounds(50,10,290,60);
box1.add(username);
username.setBounds(50,60,300,20);
password.setBounds(60,60,300,20);
panel2.add(queding);
panel2.add(chaxun);
panel2.add(qukuan);
panel2.add(tuichu);
box1.setBounds(50,10,300,60);
n[0].setBounds(50,290,200,70);
panel1.setBounds(50,80,200,210);
panel2.setBounds(250,80,100,280);
add(box1);
add(panel1);
add(panel2);
add(n[0]);
tuichu.addActionListener(this);
queding.addActionListener(this);
chaxun.addActionListener(this);
qukuan.addActionListener(this);
setLayout(null);
setTitle(s);
setBounds(200,200,400,400);
validate();
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==tuichu)//用戶退出,窗口不關(guān)閉,需要從新輸入賬號(hào)和
密碼
{
b=true;//用戶推出之后,從數(shù)字鍵輸入到username
box1.add(username);
box1.remove(password);
box1.validate();
username.setText(null);
money=0;
susername=null;
spassword=null;
tishi.setText("歡迎使用XX銀行!請(qǐng)輸入卡號(hào)。");
chaxun.show(false);
queding.show(true);
qukuan.show(false);//用戶退出,查詢和取款按鈕不可見(jiàn)if(clint!=null)
{
try
{
out.writeInt(-1);//服務(wù)器輸入數(shù)字-1,表示客戶已經(jīng)退出,
服務(wù)器可以關(guān)閉該客戶的進(jìn)程
}
catch(IOExceptione1)
{
System.out.println(e1);
}
}
}
elseif(e.getSource()==queding)
{
if(susername==null)
{
susername=username.getText();
username.setText(null);
tishi.setText("請(qǐng)輸入密碼");
b=false;//從數(shù)字鍵輸入到password
box1.remove(username);
box1.add(password);
box1.validate();
password.setText(null);
}
elseif(spassword==null)
{
spassword=password.getText();
chaxun.show(true);
b=true;//從數(shù)字鍵輸入到username
queding.show(false);
tishi.setText("請(qǐng)點(diǎn)擊查詢按鈕登陸!");
password.setText(null);
}
else
{
intyue;
try
{
BigIntegern=newBigInteger(username.getText());yue=Value();
if(yue>=0&&Value()>0)//余額大于等于0,表示用戶可以
取款
{
out.writeInt(Value());
money=in.readInt();
if(money<=yue)
{
tishi.setText("取款成功,您的余額為:"+money);queding.show(false);
username.setText(null);
box1.validate();
}
elseif(money==-3)//服務(wù)器連接數(shù)據(jù)庫(kù)錯(cuò)誤,取款失敗{
money=yue;
tishi.setText("抱歉!服務(wù)器錯(cuò)誤,取款失敗。");username.setText(null);
box1.validate();
qukuan.show(false);
queding.show(false);
validate();
}
else
{
;
queding.show(false);
tishi.setText("操作失敗,您的余額為:"+money);}
username.setText(null);
box1.remove(username);
box1.validate();
qukuan.show(true);
validate();
}
else
{
tishi.setText("輸入錯(cuò)誤,不能大于您的余額"+money);qukuan.show(true);
queding.show(false);
chaxun.show(false);
username.setText(null);
validate();
}
}
catch(IOExceptione1)
{
tishi.setText("輸入錯(cuò)誤,不能大于您的余額"+money);qukuan.show(true);
queding.show(false);
chaxun.show(false);
username.setText(null);
validate();
System.out.println(e1);
}
}
}
elseif(e.getSource()==qukuan)
{
tishi.setText("請(qǐng)輸入取款金額然后點(diǎn)擊確定。");box1.remove(password);
box1.add(username);
box1.validate();
box1.remove(username);
box1.add(username);
box1.validate();
username.setText(null);
qukuan.show(false);
queding.show(true);
}
elseif(e.getSource()==chaxun)
{
try
{
clint=newSocket("localhost",4333);
in=newDataInputStream(clint.getInputStream());out=newDataOutputStream(clint.getOutputStream());tishi.setText("連接到服務(wù)器");
}
catch(IOExceptione1)
{
tishi.setText("無(wú)法連接到服務(wù)器!");
}
try
{
out.writeUTF(susername);
out.writeUTF(spassword);
money=in.readInt();
if(money>=0)
{
tishi.setText("登陸成功,您當(dāng)前余額為:"+money);chaxun.show(false);
qukuan.show(true);
box1.remove(password);
box1.validate();
return;
}
elseif(money==-1)
{
tishi.setText("卡號(hào)錯(cuò)誤請(qǐng)從新輸入卡號(hào)和密碼");susername=null;
spassword=null;
box1.remove(password);
box1.add(username);
box1.validate();
password.setText(null);
username.setText(null);
queding.show(true);
chaxun.show(false);
validate();
return;
}
elseif(money==-2)
{
tishi.setText("密碼錯(cuò)誤請(qǐng)從新輸入密碼");
spassword=null;
password.setText(null);
chaxun.show(false);
queding.show(true);
b=false;//用戶輸入密碼錯(cuò)誤,數(shù)字鍵盤(pán)輸入到passwordreturn;
}
else
{
tishi.setText("抱歉!服務(wù)器錯(cuò)誤,暫停服務(wù)。");queding.show(true);
susername=null;
spassword=null;
box1.remove(password);
box1.add(username);
box1.validate();
username.setText(null);
chaxun.show(false);
return;
}
}
catch(IOExceptione1)
{
System.out.println(e1);
}
}
else
{
for(i=0;i<n.length;i++)
{
if(b)
{
if(e.getSource()==n[i])
{
username.setText(username.getText()+i);return;
}
}
else
{
if(e.getSource()==n[i])
{
password.setText(password.getText()+i);return;
}
}
}
}
}
}
publicclassATM
{
publicstaticvoidmain(Stringargs[])
{
ATMWindowsatm=newATMWindows("ATM");
}
}
服務(wù)器:
importjavax.swing.*;
import.*;
importjava.io.*;
importjava.util.*;
importjava.util.Date;
importjava.awt.event.*;
importjava.awt.*;
importjava.sql.*;
importjava.math.*;
importjavax.swing.*;
importjava.text.SimpleDateFormat;
publicclassATMServer
{
publicstaticvoidmain(Stringags[])
{
ServerSocketserver=null;
Socketyou=null;
while(true)
{
try
{
server=newServerSocket(4333);
}
catch(IOExceptione)
{
System.out.println("正在監(jiān)聽(tīng)");
}
try
{
you=server.accept();
}
catch(IOExceptione)
{
System.out.println("正在等待客戶");
}
if(you!=null)
{
newServerThread(you).start();
}
}
}
}
classServerThreadextendsThread
{
DBdb=newDB();
intb,money;
ResultSetrs;
Socketsocket;
DataOutputStreamout=null;
DataInputStreamin=null;
Stringsusername=null;
Stringspassword=null;
ServerThread(Socketyou)
{
try
{
in=newDataInputStream(you.getInputStream());
out=newDataOutputStream(you.getOutputStream());
}
catch(IOExceptione)
{
System.out.println(e);
}
}
publicvoidrun()
{
try
{
susername=in.readUTF();
spassword=in.readUTF();
System.out.println(susername);
if(db.connect()==1)//數(shù)據(jù)庫(kù)連接成功
{
b=db.select(susername,spassword);
db.close();
out.writeInt(b);
if(b>=0)//用戶賬號(hào)和密碼正確,登陸成功,等待取款{
while(true)
{
money=in.readInt();
if(money>=0)//客戶端輸入大于等于0時(shí),準(zhǔn)備取款,update
數(shù)據(jù)庫(kù)
{
db.connect();
money=db.select(susername,spassword)-money;
if(db.update(money,susername)>0)//數(shù)據(jù)庫(kù)update成功,返回操作后的余額給客戶端
{
System.out.println("數(shù)據(jù)庫(kù)update成功,返回操作
后的余額給客戶端");
out.writeInt(db.select(susername,spassword));}
else//數(shù)據(jù)庫(kù)update失敗,返回當(dāng)前余額給客戶端{(lán)
out.writeInt(db.select(susername,spassword));}
db.close();
}
elseif(money==-1)//用戶已在客戶端退出
{
db.close();
System.out.println("此用戶已經(jīng)退出終端,線程自動(dòng)結(jié)
束。");
break;
}
try
}
}
elseif(b==-3)
{
db.close();
System.out.println("數(shù)據(jù)庫(kù)錯(cuò)誤,用戶登陸失敗,自動(dòng)關(guān)閉連接線
程");
}
else//用戶驗(yàn)證失敗
{
db.close();
System.out.println("賬號(hào)或者密碼錯(cuò)誤,用戶登陸失敗,自動(dòng)關(guān)閉
連接線程");
}
}
else
{
out.writeInt(-3);//用-3表示數(shù)據(jù)庫(kù)連接失敗,通知客戶端暫停使用}
}
catch(IOExceptione)
{
System.out.println(e);
}
}
}
classDB
{
Connectioncon;
PreparedStatementpre;
ResultSetrs=null;
Stringsql=null;
DB()
{
}
publicintconnect()
{
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundExceptione)
{
System.out.println(""+e);
return0;
}
try
{
con=DriverManager.getConnection("jdbc:odbc:user","","");System.out.println("數(shù)據(jù)庫(kù)連接成功");
return1;
}
catch(SQLExceptione)
{
System.out.println(e);
return0;
}
}
publicintselect(Stringsusername,Stringspassword)
{
try
{
pre=con.prepareStatement("SELECT*FROMu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 股份贈(zèng)予協(xié)議書(shū)
- 資金終止協(xié)議書(shū)
- 合同法拖欠貨款協(xié)議書(shū)
- 合同一次性補(bǔ)償協(xié)議書(shū)
- 環(huán)衛(wèi)企業(yè)協(xié)議書(shū)
- 綁定業(yè)務(wù)協(xié)議書(shū)
- 夫妻房產(chǎn)歸個(gè)人協(xié)議書(shū)
- 紅酒包銷協(xié)議書(shū)
- 智能存儲(chǔ)柜轉(zhuǎn)讓協(xié)議書(shū)
- 郵件自提協(xié)議書(shū)
- 2024年上海青浦區(qū)下半年區(qū)管企業(yè)統(tǒng)一招聘30人筆試參考題庫(kù)附帶答案詳解
- 2025年上海青浦區(qū)高三語(yǔ)文二模試題卷附答案解析
- 存款保險(xiǎn)知識(shí)培訓(xùn)總結(jié)
- 2025房地產(chǎn)經(jīng)紀(jì)人協(xié)理-《房地產(chǎn)經(jīng)紀(jì)綜合能力》考前通關(guān)必練題庫(kù)-含答案
- 陪診師考試模擬測(cè)試及試題答案
- 中國(guó)車(chē)路云一體化發(fā)展研究報(bào)告
- 超星爾雅學(xué)習(xí)通《公文寫(xiě)作規(guī)范(黑龍江大學(xué))》2025章節(jié)測(cè)試附答案
- 2025年青桐鳴高三語(yǔ)文3月大聯(lián)考作文題目解析及相關(guān)范文:道理是直的道路是彎的
- 腫瘤免疫治療綜述
- 《基于Android客戶端的助老APP的設(shè)計(jì)與實(shí)現(xiàn)》8400字(論文)
- 2025-2030年中國(guó)威士忌酒行業(yè)運(yùn)行動(dòng)態(tài)及前景趨勢(shì)預(yù)測(cè)報(bào)告
評(píng)論
0/150
提交評(píng)論