版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、程序实现其实并不是很难,主要是思路,对程序的各个功能的设计,各个按钮的处理事件及按钮按照逻辑显示出来和隐藏。服务器采用多线程并行机制,针对每个用户登录创建一个线程。程序代码 客户端:import javax.swing.*;import .*;import java.io.*;import java.util.*;import java.awt.event.*;import java.awt.*;import java.sql.*;import java.math.*;import javax.swing.*;class ATMWindows extends JFrame implements
2、 ActionListenerFont f;Socket clint=null;DataOutputStream out=null;DataInputStream in=null;Connection con;Statement pre;ResultSet rs;int i,money;boolean b=true;/b的取值有0,1,共2个值分别告诉程序该吧数字键盘的输入复制给username,password,String susername=null;String spassword=null;Box box1,box2,box3,box11,box12;JPanel panel1,pa
3、nel2;JLabel tishi;JTextField username;JPasswordField password;JButton n,queding,chaxun,qukuan,tuichu;ATMWindows(String s)GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();String t=ge.getAvailableFontFamilyNames();f=new Font(t67,Font.BOLD,16);n=new JButton10;box1=Box.createVert
4、icalBox();box2=Box.createHorizontalBox();box3=Box.createVerticalBox();panel1=new JPanel();panel2=new JPanel();tishi=new JLabel("欢迎使用XX银行!请输入卡号。");tishi.setFont(f);username=new JTextField(20);password=new JPasswordField(20);panel1.setLayout(new GridLayout(3,3);panel2.setLayout(new GridLayou
5、t(4,1);for(i=1;i<n.length;i+)ni=new JButton(""+i);panel1.add(ni);ni.addActionListener(this);n0=new JButton("0");panel1.add(n0);n0.addActionListener(this);n0.setPreferredSize(new Dimension(20,20);queding=new JButton("确定");chaxun=new JButton("查询");/用户输入账号和密码后
6、,点击查询按钮登陆qukuan=new JButton("取款");tuichu=new JButton("退出");qukuan.show(false);chaxun.show(false);/初始化窗口后,账号和密码还没输入,不能用查询按钮登陆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);panel
7、2.add(chaxun);panel2.add(qukuan);panel2.add(tuichu);box1.setBounds(50,10,300,60);n0.setBounds(50,290,200,70);panel1.setBounds(50,80,200,210);panel2.setBounds(250,80,100,280);add(box1);add(panel1);add(panel2);add(n0);tuichu.addActionListener(this);queding.addActionListener(this);chaxun.addActionListe
8、ner(this);qukuan.addActionListener(this);setLayout(null);setTitle(s);setBounds(200,200,400,400);validate();setResizable(false);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);public void actionPerformed(ActionEvent e)if(e.getSource()=tuichu)/用户退出,窗口不关闭,需要从新输入账号和密码b=true;/用户推出之后,从数
9、字键输入到usernamebox1.add(username);box1.remove(password);box1.validate();username.setText(null);money=0;susername=null;spassword=null;tishi.setText("欢迎使用XX银行!请输入卡号。");chaxun.show(false);queding.show(true);qukuan.show(false);/用户退出,查询和取款按钮不可见if(clint!=null)tryout.writeInt(-1);/服务器输入数字-1,表示客户已经退
10、出,服务器可以关闭该客户的进程catch(IOException e1)System.out.println(e1);else if(e.getSource()=queding)if(susername=null)susername=username.getText();username.setText(null);tishi.setText("请输入密码");b=false;/从数字键输入到passwordbox1.remove(username);box1.add(password);box1.validate();password.setText(null);else
11、 if(spassword=null)spassword=password.getText();chaxun.show(true);b=true;/从数字键输入到usernamequeding.show(false);tishi.setText("请点击查询按钮登陆!");password.setText(null);elseint yue;tryBigInteger n=new BigInteger(username.getText();yue=Value();if(yue>=0&&Value()>0)/余额大于
12、等于0,表示用户可以取款out.writeInt(Value();money=in.readInt();if(money<=yue)tishi.setText("取款成功,您的余额为:"+money);queding.show(false);username.setText(null);box1.validate();else if(money=-3)/服务器连接数据库错误,取款失败money=yue;tishi.setText("抱歉!服务器错误,取款失败。");username.setText(null);box1.validate(
13、);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();elsetishi.setText("输入错误,不能大于您的余额"+money);qukuan.show(true);queding.show(false)
14、;chaxun.show(false);username.setText(null);validate();catch(IOException e1)tishi.setText("输入错误,不能大于您的余额"+money);qukuan.show(true);queding.show(false);chaxun.show(false);username.setText(null);validate();System.out.println(e1);else if(e.getSource()=qukuan)tishi.setText("请输入取款金额然后点击确定。&
15、quot;);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);else if(e.getSource()=chaxun)tryclint=new Socket("localhost",4333);in=new DataInputStream(clint.getInputStre
16、am();out=new DataOutputStream(clint.getOutputStream();tishi.setText("连接到服务器");catch(IOException e1)tishi.setText("无法连接到服务器!");tryout.writeUTF(susername);out.writeUTF(spassword);money=in.readInt();if(money>=0)tishi.setText("登陆成功,您当前余额为:"+money);chaxun.show(false);quku
17、an.show(true);box1.remove(password);box1.validate();return;else if(money=-1)tishi.setText("卡号错误请从新输入卡号和密码");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();
18、return;else if(money=-2)tishi.setText("密码错误请从新输入密码!");spassword=null;password.setText(null);chaxun.show(false);queding.show(true);b=false;/用户输入密码错误,数字键盘输入到passwordreturn;elsetishi.setText("抱歉!服务器错误,暂停服务。");queding.show(true);susername=null;spassword=null;box1.remove(password);box
19、1.add(username);box1.validate();username.setText(null);chaxun.show(false);return;catch(IOException e1)System.out.println(e1);elsefor(i=0;i<n.length;i+)if(b)if(e.getSource()=ni)username.setText(username.getText()+i);return;elseif(e.getSource()=ni)password.setText(password.getText()+i);return;publi
20、c class ATMpublic static void main(String args)ATMWindows atm=new ATMWindows("ATM");服务器:import javax.swing.*;import .*;import java.io.*;import java.util.*;import java.util.Date;import java.awt.event.*;import java.awt.*;import java.sql.*;import java.math.*;import javax.swing.*;import java.t
21、ext.SimpleDateFormat;public class ATMServerpublic static void main(String ags)ServerSocket server=null;Socket you=null;while(true)tryserver=new ServerSocket(4333);catch(IOException e)System.out.println("正在监听");tryyou=server.accept();catch(IOException e)System.out.println("正在等待客户"
22、);if(you!=null)new ServerThread(you).start();class ServerThread extends ThreadDB db=new DB();int b,money;ResultSet rs;Socket socket;DataOutputStream out=null;DataInputStream in=null;String susername=null;String spassword=null;ServerThread(Socket you)tryin=new DataInputStream(you.getInputStream();out
23、=new DataOutputStream(you.getOutputStream();catch(IOException e)System.out.println(e);public void run()trysusername=in.readUTF();spassword=in.readUTF();System.out.println(susername);if(db.connect()=1)/数据库连接成功b=db.select(susername,spassword);db.close();out.writeInt(b);if(b>=0)/用户账号和密码正确,登陆成功,等待取款w
24、hile(true)money=in.readInt();if(money>=0)/客户端输入大于等于0时,准备取款,update数据库db.connect();money=db.select(susername,spassword)-money;if(db.update(money,susername)>0)/数据库update成功,返回操作后的余额给客户端System.out.println("数据库update成功,返回操作后的余额给客户端");out.writeInt(db.select(susername,spassword);else/数据库upda
25、te失败,返回当前余额给客户端out.writeInt(db.select(susername,spassword);db.close();else if(money=-1)/用户已在客户端退出db.close();System.out.println("此用户已经退出终端,线程自动结束。");break;else if(b=-3)db.close();System.out.println("数据库错误,用户登陆失败,自动关闭连接线程");else /用户验证失败db.close();System.out.println("账号或者密码错误,用
26、户登陆失败,自动关闭连接线程");elseout.writeInt(-3);/用-3表示数据库连接失败,通知客户端暂停使用catch(IOException e)System.out.println(e);class DBConnection con;PreparedStatement pre;ResultSet rs=null;String sql=null;DB()public int connect()tryClass.forName("sun.jdbc.odbc.JdbcOdbcDriver");catch(ClassNotFoundException e
27、)System.out.println(""+e);return 0;trycon=DriverManager.getConnection("jdbc:odbc:user","","");System.out.println("数据库连接成功");return 1;catch(SQLException e)System.out.println(e);return 0;public int select(String susername,String spassword)trypre=con.prepareStatement("SELECT * FRO
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 疾病诊疗费用控制管理方案
- 服务端接口稳定性压力测试方案
- 临时用电箱检修交接管理规范
- 犬猫脚垫修剪防侧滑护理方案
- 公司证照年检流程操作指引
- 客户关系维护管理办法细则
- 贵宾犬造型剪裁标准流程规范
- 施工升降机维保周期记录规范
- 医院卫生统计数据质量自查报告(2篇)
- 东方红印刷厂合同违约责任协议合同三篇
- 2025年12月广东省景顺长城基金管理有限公司2026年校园招考2名工作人员信息笔试历年备考题库附带答案详解试卷2套
- 留置导尿操作流程
- 金华实验小学关于违规征订教辅材料的专项整治调查问卷
- 2025-2026学年北京市昌平区高三(上期)期末考试英语试卷(含答案)
- 2026年防爆电气设备事故案例分析
- 高一数学下册解三角形专项卷(人教版考点)
- 儿童康复辅具评估协议2025年服务
- 电镀行业危险化学品培训
- 2025年国际中文教师证书考试笔试测试卷及参考答案
- 临终关怀模拟教学中的知情同意与法律伦理
- 2024~2025学年河南省许昌市长葛市统编版三年级下册期中考试语文试卷
评论
0/150
提交评论