网页 图片 视频 音乐 问问 百科 新闻 地图 更多» 登录
搜搜问问
搜索答案
问问首页
问题库 知识频道 用户频道 问问活动 问问团队
问问商城
全部问题 >电脑/数码>编程
分享: 腾讯微博 QQ空间 新浪微博 | 收藏
Evolution□ 2009-04-21
谁帮我用JAVA写一个简单的模拟ATM的程序
谁帮我用JAVA写一个简单的模拟ATM的程序最好要有思路,或直接给个网站也行..谢谢了
满意答案
Sy.Memoryer 3级 2009-04-21
public class BankSys { //private int uId; //这写好像用不上,你自己看着加吧 //private String uName; //private String address; private double minMon=0.50; private double money=1000.50; public static void main(String[] args) { String answer; System.out.println(\"欢迎进入存取款系统!\"); System.out.println(\"---------------------\"); do{ System.out.println(\"1.取款 \2.查询余额\3.存款\"); Scanner sc=new Scanner(System.in); BankSys bank=new BankSys(); int num=sc.nextInt(); switch (num) { case 1: bank.get(); break; case 2: bank.search(); break; case 3: bank.deposit(); break; default: break; } System.out.println(\"是否继续?(y/n)\"); answer=sc.next(); }while(answer.equals(\"y\")); System.out.println(\"程序结束\"); } public void deposit(){ Scanner sc=new Scanner(System.in); System.out.println(\"请输入存款金额:\"); double input=sc.nextInt(); System.out.println(\"您的帐户原有余额为:\"+money+\"元\"); System.out.println(\"已存入:\"+input+\"元\"); money+=input;
System.out.println(\"您的帐户最终存款余额为:\"+money+\"元\"); } public void search(){ System.out.println(\"您的帐户余额为:\"+money+\"元\"); } public void get(){ Scanner sc=new Scanner(System.in); System.out.println(\"请输入取款金额:\"); double input=sc.nextInt(); if (money3用JAVA编写一个自动取款机(ATM)模拟程序 求一个Java模拟时钟程序 java模拟 java程序求助 我需要那个用java做的atm,谢谢你~ 更多相关知识>>java模拟atm查询余„模拟atm柜员机程序atm取款机java程序atm程序 java用java写atm界面代„atm是什么意思java atm取款机系统java程序Top何夏冰 随风 安靜の聽 助提意见
5.1 程序源代码 import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; public class ATMmodel { Account myaccount=new Account(); Load load=new Load(); MainFrame Jmain=new MainFrame(); Take take=new Take(); Input input=new
Input(); Display dis=new Display(); Setpw setpw=new Setpw(); public ATMmodel()
{ JOptionPane.showMessageDialog(null,\"欢迎使用ATM柜员模拟系统\"); } public static void main(String[] args) { ATMmodel atm=new ATMmodel(); } /*创建账户*/ class Account { private int count=10000; //初始账户余额10000 private String
account=\"123456\"; //初始账号 private String password=\"123456\"; //初始密码 public Account() { } public void setpw(String password){ this.password=password; } public void deccount(int money){ count-=money; } public void reccount(int money){ count+=money; } public int
get(){ return count; } public String getaccount(){ //账号 return account; } public String getpw(){ return password; } } /* 主界面*/ class MainFrame extends JFrame { public JButton checked; public JButton cunkuan; public JButton quit; public JButton qukuan; public JButton reset; public MainFrame() { initComponents(); this.setLocationRelativeTo(null); //
this.setVisible(false); } /* 控件初始化*/
private void initComponents() { cunkuan = new JButton(); qukuan = new JButton(); checked = new JButton(); reset = new JButton(); quit = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle(\"ATM柜员模拟程序\"); setName(\"mianframe\");
setResizable(false); cunkuan.setText(\"存款\"); cunkuan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ cunkuanActionPerformed(evt); } }); 面向对象课程设计─《ATM柜员机模拟程序》 6 qukuan.setText(\"取款\"); qukuan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ qukuanActionPerformed(evt); } }); checked.setText(\"查询\"); checked.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ checkedActionPerformed(evt); } }); reset.setText(\"修改密码\");
reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ resetActionPerformed(evt); } }); quit.setText(\"退出\"); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ quitActionPerformed(evt); } }); GroupLayout layout = new GroupLayout(getContentPane());
//GroupLayout它将组件按层次分组,以决定它们在 Container 中的位置 getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(cunkuan) .addPreferredGap(javax.swing.LayoutSt
yle.ComponentPlacement.RELATED, 105,
Short.MAX_VALUE) .addComponent(reset)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(qukuan) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143,
Short.MAX_VALUE) .addComponent(quit)) 面向对象课程设计─《ATM柜员机模拟程序》 8 /*登录界面*/ class Load extends JFrame{ public JPasswordField Jpassword; public JTextField accountnum; public JButton commit; private JLabel jL1; private JLabel jL2; public Load() { initComponents();
this.setLocationRelativeTo(null); this.setVisible(true); } private void initComponents() { jL1 = new JLabel(); jL2 = new JLabel(); accountnum = new JTextField();
commit = new JButton(); Jpassword = new JPasswordField(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle(\"ATM柜员模拟程序\"); setResizable(false); jL1.setText(\"账号:\"); jL2.setText(\"密码:\"); commit.setText(\"确定\"); commit.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(java.awt.event.ActionEvent evt)
{ commitActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(50,
Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jL1) .addComponent(jL2)) .addGap(31, 31, 31) 面向对象课程设计─《ATM柜员机模拟程序》
9 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false) .addComponent(Jpassword) .addComponent(accountnum, javax.swing.GroupLayout.DEFAULT_SIZE, 143,
Short.MAX_VALUE))) .addGroup(layout.createSequent
ialGroup() .addGap(122, 122,
122) .addComponent(commit))) .addContainerGap(72, Short.MAX_VALUE)) );
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(85, 85,
85) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(accountnum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jL1)) .addGap(41, 41,
41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jL2) .addComponent(Jpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72,
Short.MAX_VALUE) .addComponent(commit) .addGap(64, 64, 64)) ); pack(); } private void commitActionPerformed(ActionEvent evt)
{ if(accountnum.getText().equals(myaccount.getaccount()) && Jpassword.getText().equals(myaccount.getpw()))
{ Jmain.setVisible(true); this.setVisible(false); accountnum.setText(\"\"); Jpassword.setText(\"\"); }
else{ JOptionPane.showMessageDialog(null,\"密码与账户不匹配,请重新输入\出错提示\面向对象课程设计─《ATM柜员机模拟程序》 12 .BASELINE) .addComponent(commit) .addComponent(back)) .addGap(29, 29,
29) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(clear) .addComponent(quit)) .addContainerGap(65, Short.MAX_VALUE)) ); pack(); }
private void clearActionPerformed(ActionEvent evt) { str=\"\"; jT1.setText(str); } private void
quitActionPerformed(ActionEvent evt) { load.setVisible(true); this.setVisible(false); } private void
backActionPerformed(ActionEvent evt) { Jmain.setVisible(true); this.setVisible(false); } private void commitActionPerformed(ActionEvent evt)
{ if(Integer.parseInt(jT1.getText())>0 && Integer.parseInt(jT1.getText())%100==0 &&
Integer.parseInt(jT1.getText())<=5000){ myaccount.deccount(I
nteger.parseInt(jT1.getText())); int
result=javax.swing.JOptionPane.showConfirmDialog(null,\"是否显示余额?\ESTION_MESSAGE);
if(result==javax.swing.JOptionPane.YES_OPTION)
{ dis.set(String.valueOf(myaccount.get())); str=\"\"; jT1.setText(str); dis.setVisible(true);
this.setVisible(false); } else
{ Jmain.setVisible(true); 面向对象课程设计─《ATM柜员机模拟程序》 13
this.setVisible(false); } }
else{ JOptionPane.showMessageDialog(null,\"取款金额不能为负数\"+\"\\n取款金额不能为0\"+\"\\n取款金额必须是100的倍数\"+\"\\n金额一次不能超
过5000\"+\"\\n请重新输入你要取的金额数\出错提示\jT1.setText(str); } } } /*存款界面*/ class Input extends JFrame { private JButton clear; private JButton commit; private JLabel jL1; private JTextField jT1; String str=\"\"; public Input() { initComponents();
this.setLocationRelativeTo(null); this.setResizable(false); this.setVisible(false); } private void initComponents()
{ jL1 = new JLabel(); jT1 = new JTextField(); commit = new JButton(); clear = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jL1.setText(\"存款金额:\"); commit.setText(\"确认\"); commit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt)
{ commitActionPerformed(evt); } }); clear.setText(\"清除\");
clear.addActionListener(new java.awt.event.ActionListener() { 面向对象课程设计─《ATM柜员机模拟程序》 17 }); javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(27, 27,
27) .addComponent(jL1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE) .addComponent(jT1, javax.swing.GroupLayout.PREFERRED_SIZE, 143,
javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(36,
Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup() .addContainerGap(215, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(back) .addComponent(qukuan) .addComponent(quit)) .addContainerGap()) );
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(93, 93,
93) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jL1) .addComponent(jT1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(54, 54,
54) .addComponent(qukuan) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(back) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(quit) .addContainerGap()) ); pack(); } 面向对象课程设计─《ATM柜员机模拟程序》 18 private void qukuanActionPerformed(ActionEvent evt) { take.setVisible(true);
this.setVisible(false); } private void
quitActionPerformed(ActionEvent evt) { load.setVisible(true); this.setVisible(false); } private void
backActionPerformed(ActionEvent evt) { Jmain.setVisible(true);
this.setVisible(false); } } /**修改密码 */ class Setpw extends
JFrame { public JButton commit; public JLabel commitpw; public JPasswordField jPwcommit; public JPasswordField jPwnew; public JPasswordField jPwold;
public JLabel newpw; public JLabel oldpw; public JButton quit; public Setpw() { initComponents();
this.setResizable(false); this.setLocationRelativeTo(null); //this.setVisible(false); } private void initComponents() { oldpw = new JLabel(); newpw = new JLabel(); commitpw = new JLabel(); jPwold = new JPasswordField(); jPwnew = new JPasswordField(); jPwcommit = new JPasswordField(); commit = new javax.swing.JButton(); quit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
oldpw.setText(\"旧密码:\"); newpw.setText(\"新密码:\"); 面向对象课程设计─《ATM柜员机模拟程序》 21 flag=true;
else{ flag=false; break; } } //密码全部一样则给出提示 if(flag) JOptionPane.showMessageDialog(null,\"密码不能全都相同\错误提示\以上情况都没出现则修改成功 else if(jPwold.getText().equals(myaccount.getpw()) && jPwnew.getText().equals(jPwcommit.getText())) { myaccount.setpw(jPwnew.getText());
JOptionPane.showMessageDialog(null,\"密码修改成功,请记住你的新密码\
this.setVisible(false); } } } private void quitActionPerformed(java.awt.event.ActionEvent evt)
{ load.setVisible(true); this.setVisible(false); } } } 5.2 源代码说明 5.2.1 源代码中自定义类的说明: 代码中出现的Account类、MainFrame类、Load类、Take类、Input类、Setpew类和Display类都继承了Java类JFrame。 (1)Class Accout创建客户,初始化客户信息功能以及保存客户信息,例如:卡号、密码、原有余额、取款后卡里的余额以及存款后卡里的余额。此类包含的成员有:成员变量account、count、password和方法构造方法Account()。 面向对象课程设计─《ATM柜员机模拟程序》 22 (2)Class MainFrame构造主界面,提供业务选择,让用户可以在这个界面选择存款、取款、查询余额、修改密码。它包含的成员有:构造方法MainFrame()和初始化主界面的方法 initComponents()以及成员变量checked、cunkuan、quite、qukuan、reset。 (3)Class Load 实现登录功能和登录界面,如果用户名和密码匹配就可以登录到主界面否则报错。它的成员有:Jpassword、accountnum、committ、JL1、JL2和构造方法Load()和初始化方法initComponents()。 (4)Class Take实现取款功能,按返回键返回主界面,按清除键可以删除输入数字,并且取出的金额必须是100的整数倍,一次取出的金额不能超过5000,取完款后可以显示余额。该类的成员有:back、clear、commit、quite、JL1、JT1和构造方法Take()和初始化的initComponents()。 (5)Class input实现存款功能,存款的金额必须是100的整数倍,否则报错提示重新输入。成员有:clear、commit、jL1、jT1、str以及构造函数Inputhe()和初始化方法initComponents() (6)Class Display功能为显示当前余额,在显示余额时JTextField的内容不可以编辑,成员有:成员变量back、jL1、quit、qukuan和构造方法Display()和方法数set(String str)和初始化的方法initComponents()。 (7)class Setpw创建此类是为了实现修改密码的功能,设置的密码必须是六位以上的字符,并且所设置的密码不能是同一个字符组成,否则报错。成员有:成员变量commit、commitpw、jPwcommit、jPwnew、jPwold、newpw、oldpw、quit和构造方法Setpw()和初始化方法initComponents()。 5.2.2源代码中的事件监听器 (1) MainFrame类的监听器 在该类中设置了五个监听器,监听鼠标单击按钮,分别监听退出、修改密码、查询、取款、存款等按钮的事件。 (2) Load类的监听器 在该类中只有一个监听器,监听鼠标单击“确定”按钮时的事件。 (3) Take类的监听器 在该类中设置了四个监听器,同样是监听鼠标单击按钮事件,分别监听“清除”、“退出”、“返回”、“确定”按钮的事件。 (4 )input类的监听器 在这个类中共设置了两个监听器,它们也是监听鼠标单击按钮事
件。它们监听“清除”、“确定”按钮事件。 (5 )display类的监听器 在display中设置了三个监听器,都属于监听鼠标单击按钮事件,分别监听“退出”、
面向对象课程设计─《ATM柜员机模拟程序》 23 “返回”、“取款”按钮事件。 (6 )Setpw类的监听器 在这个类中设置了两个监听器,它们是监听鼠标单击按钮事件,分别监听“确定”、“退出”按钮事件。 6 调试及运行结果 (1)打开MyEcplise,编译运行程序后,将会看到如图2所示的欢迎界面。点击确定之后,会进入如图3所示的登陆界面。 图2 欢迎登陆页面
进入到登陆界面后,需要输入账号和密码,当账号和密码正确时,才会进入到主界面。账号和密码不符合时,会进入如图4所示的错误提示界面。初始账号和密码为“123456”。 图4 登陆出错提示界面 (2)当输入的密码和卡号正
确,单击确定后就会进入到主界面,选择服务,如图
5
面向对象课程设计─《ATM柜员机模拟程序》 28 图11 查询界面 (6)点击主界面中的修改密码按钮,进入到如图12所示的修改密码界面。新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码,弹出如图13所示的提示界面,点击确定后会回到主界面。否则,密码修改不成功,点击确定按钮,界面没有反应。 图12 修改密码界面 面向对象课程设计─《ATM柜员机模拟程序》 29 图13 修改密码成功提示界面 经过上面的操作,既测试了程序的实现效果,又对系统的使用进行了详细的说明。从上面的运行结果可以看出,程序基本上实现了所有的设计要求,而且运行正常。 7 心得体会 通过设计ATM柜员模拟程序,加深自己对Java编程尤其是对Java图形用户界面编程的了解,将自己的学到的编程知识融会贯通,同时培养自身面向对象软件开发的思维,初步了解软件开发的一般流程,提高用Java实际动手能力并增强自己对面向对
象的了解。通过这次编程设计,加深了对Java图形界面编程中各个模块和组件的应用了解,以及类和类之间的关系,界面排版的处理等。 设计要求是,当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行(规则可以按照实际系统做需求):(1)查询余额:初始余额为10000元。(2)ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。(3)ATM存款:不能出现负存款。(4)修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。 该程序设置了7个类,1个主类6个辅助类,6个辅助类都是内部类的形式,辅助类包括五个实现ATM的功能,一个存放客户资料,之所以用内部类来实现是为了在各个界面转换时不至于要创建多个对象,每一个辅助类的对象只要在主类中创建一次就可以在各个内部类中引用。该程序利用的功能是利用Java事件监听技术来实现的,各种功
因篇幅问题不能全部显示,请点此查看更多更全内容