当前位置:文档之家› 柜员机模拟程序

柜员机模拟程序

课程论文现代软件工程与实践题目: ATM柜员机模拟程序学院(系):信息工程学院专业班级:学生姓名:指导教师:2011 年 12月 4日摘要通过设计ATM柜员模拟程序,加深自己对Java编程尤其是对Java图形用户界面编程的了解,将自己的学到的编程知识融会贯通,同时培养自身面向对象软件开发的思维,初步了解软件开发的一般流程,提高用Java实际动手能力并增强自己对面向对象的了解。

通过这次编程设计,加深了对Java图形界面编程中各个模块和组件的应用了解,以及类和类之间的关系,界面排版的处理等。

设计要求是,当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行(规则可以按照实际系统做需求):(1)查询余额:初始余额为10000元。

(2)ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。

(3)ATM存款:不能出现负存款。

(4)修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。

该程序设置了7个类,1个主类6个辅助类,6个辅助类都是内部类的形式,辅助类包括五个实现ATM的功能,一个存放客户资料,之所以用内部类来实现是为了在各个界面转换时不至于要创建多个对象,每一个辅助类的对象只要在主类中创建一次就可以在各个内部类中引用。

该程序利用的功能是利用Java事件监听技术来实现的,各种功能单独的创建一个类来实现,不会互相混淆,更加容易理清楚思路。

设计的程序完美的实现了设计要求,具有查询、取款、存款、修改密码等功能。

目录1 前言1.1 设计任务1.1.1 需求说明随着市场经济的繁荣发展,人们的物质生活得到极大的提高,手中的剩余资金也日益增多。

受传统观念影响,绝大多数人喜欢把资金投进银行,而在需要使用现金的时候再到银行提取现金。

这就导致银行业务的急剧增加。

虽然银行网点越来越多,银行的工作效率也随着信息化有较明显的增长,但是依旧不能满足广大用户的需要。

而一味的增加营业网点,从理论上虽然能稍微的缓解银行的压力,但是治标不治本,而且每增加一个网点,不仅要付出昂贵的资金成本,还需要长期投入人力资本,成本非常之昂贵。

这时候就迫切的需要一种自动的,易于普及的,低成本的设备来解决这一问题。

于是ATM 机系统应运而生了。

本文正是通过设计一个ATM模拟模拟程序来实现ATM机系统的基本功能,充分运用自己所学的Java知识进行设计编程。

1.2 设计内容(1)程序要求(2)算法分析(3)流程图(4)源程序(5)调试及运行结果(6)体会2 程序要求本ATM柜员机的主要功能如下所述:要求使用图形用户界面。

当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:●查询余额:初始余额为10000元。

●ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。

●ATM存款:不能出现负存款。

修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。

3 算法分析通过程序要求,我们可以清楚的知道本次设计的ATM柜员模拟程序需要实现四大功能,即查询余额、取款、存款、修改密码。

初次之外,作为ATM系统,还需要最基本的两大功能,即登陆和退出。

根据上述的功能分析,我们可以吧整个ATM系统分为登陆模块、查询模块、取款模块、存款模块、修改密码模块。

除此之外,还有在登陆系统后的功能选择模块,退出功能则镶嵌在各个需要的模块中。

由于各个模块的功能实现是通过界面显示出来的,则程序设计又可以理解为几大界面的设计:登陆界面、主界面、查询余额界面、取款界面、存款界面、修改密码界面。

除了几大界面之外,还有系统的初始化,主要是初始卡号、密码的设定等。

在程序的大体框架确定之后,我们需要了解各个模块所要实现和需要具有的功能。

根据现有的银行ATM系统,我们可以很快就可以了解本次设计的ATM系统应该具有的操作。

(1)登陆界面用户输入正确的账号和密码就可以登录到该系统的主界面中去,进行相应的操作:取款、存款、查询、修改密码。

(2)主界面主界面包括查询、存款、取款、修改密码、退出的功能按钮,按下相应的按钮,可以进行相应的操作。

(3)查询界面在主界面中按下查询按钮可以进行查询操作。

通过查询功能我们可以查询账户的余额,在查询过程中我们可以选择取款的业务,除此之外,查询界面之中还有返回和退出的操作。

返回的操作会使用户回到主界面,退出的操作会使用户直接回到登陆界面。

(4)取款界面取款界面既可以通过主界面进入,也可以通过查询、存款等操作过程中的界面进入。

取款功能在对话框中输入你想要取出的金额,但是次金额不可以比账户的余额大,且必须是100的整数倍,取款完毕,会弹出是否显示余额的提示,选“是”的话就会进入查询界面,选“否”的话,则回到主界面。

(5)存款界面在主界面按下存款按钮可以进行存款操作,在对话中输入你需要存入的金额,所输入的数字也要是100的整数倍,而且不可以是负数。

存款完毕,会弹出是否显示余额的提示,选“是”的话就会进入查询界面,选“否”的话,则回到主界面。

(6)修改密码选择修改密码按钮可以进行修改密码房,但是要先输入就密码正确并且两次输入新密码一致才可以修改密码。

修改完成后,会返回主界面。

4 流程图图1 程序流程图5 程序源代码及说明5.1 程序源代码import javax.swing.*;importimportpublic 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; //初始账户余额10000private 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);}});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()LEADING).addGroup(layout.createSequentialGroup().addComponent(cunkuan)RELATED, 105, Short.MAX_VALUE).addComponent(reset))TRAILING, layout.createSequentialGroup().addComponent(qukuan)RELATED, 143, Short.MAX_VALUE).addComponent(quit)).addComponent(checked)).addContainerGap()));layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(46, 46, 46)BASELINE).addComponent(cunkuan).addComponent(reset)).addGap(51, 51, 51)BASELINE).addComponent(quit).addComponent(qukuan)).addGap(57, 57, 57).addComponent(checked).addGap(39, 39, 39)));pack();}private void quitActionPerformed(ActionEvent evt) { load.setVisible(true);this.setVisible(false);}private void resetActionPerformed(ActionEvent evt) { setpw.setVisible(true);this.setVisible(false);}private void checkedActionPerformed(ActionEvent evt) { dis.setVisible(true);this.setVisible(false);}private void qukuanActionPerformed(ActionEvent evt) { take.setVisible(true);this.setVisible(false);}private void cunkuanActionPerformed(ActionEvent evt) { input.setVisible(true);this.setVisible(false);}}/*登录界面*/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(newpublic void actionPecommitActionPerformed(evt);}});newgetContentPane().setLayout(layout);layout.setHorizontalGroup(LEADING).addGroup(layout.createSequentialGroup()LEADING).addGroup(layout.createSequentialGroup().addContainerGap(50, Short.MAX_VALUE) TRAILING).addComponent(jL1).addComponent(jL2)).addGap(31, 31, 31)LEADING, false).addComponent(Jpassword) DEFAULT_SIZE, 143, Short.MAX_VALUE))).addGroup(layout.createSequentialGroup().addGap(122, 122, 122).addComponent(commit))).addContainerGap(72, Short.MAX_VALUE)));layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(85, 85, 85)BASELINE)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE).addComponent(jL1)).addGap(41, 41, 41)BASELINE).addComponent(jL2)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE))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,"密码与账户不匹配,请重新输入","出错提示",1);accountnum.setText("");Jpassword.setText("");}}}/*取款界面*/class Take extends JFrame {public JButton back;public JButton clear;public JButton commit;private JLabel jL1;public JTextField jT1;public JButton quit;String str="";public Take() {initComponents();this.setLocationRelativeTo(null);this.setVisible(false);}private void initComponents() {jL1 = new JLabel();jT1 = new JTextField();commit = new JButton();back = new JButton();quit = 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);}});back.setText("返回");back.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent evt) {backActionPerformed(evt);}});quit.setText("退出");quit.addActionListener(new ActionListener() {public voidquitActionPerformed(evt);}});clear.setText("清除");clear.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent evt) {clearActionPerformed(evt);}});newgetContentPane().setLayout(layout);layout.setHorizontalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(41, 41, 41)LEADING).addGroup(layout.createSequentialGroup().addComponent(jL1).addGap(33, 33, 33) PREFERRED_SIZEPREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(clear) RELATED, 147, Short.MAX_VALUE).addComponent(quit)).addGroup(layout.createSequentialGroup().addComponent(commit) RELATED, 147, Short.MAX_VALUE).addComponent(back))) PREFERRED_SIZE)));layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(103, 103, 103)BASELINE).addComponent(jL1)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)).addGap(61, 61, 61)BASELINE).addComponent(commit).addComponent(back)).addGap(29, 29, 29)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(Integer.parseInt(jT1.getText()));int showConfirmDialog(null YES_NO_OPTIONQUESTION_MESSAGE);if YES_OPTION){dis.set(String.valueOf(myaccount.get()));str="";jT1.setText(str);dis.setVisible(true);this.setVisible(false);}else{Jmain.setVisible(true);this.setVisible(false);}}else{JOptionPane.showMessageDialog(null,"取款金额不能为负数"+"\n取款金额不能为0"+"\n取款金额必须是100的倍数"+"\n金额一次不能超过5000"+"\n请重新输入你要取的金额数","出错提示",1);str="";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(newpublic voidclearActionPerformed(evt);}});newgetContentPane().setLayout(layout);layout.setHorizontalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(47, 47, 47)LEADING).addGroup(layout.createSequentialGroup().addGap(12, 12, 12).addComponent(commit).addGap(111, 111, 111).addComponent(clear)).addGroup(layout.createSequentialGroup().addComponent(jL1).addGap(27, 27, 27)PREFERRED_SIZEPREFERRED_SIZE))).addContainerGap(50, Short.MAX_VALUE)));layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(88, 88, 88)BASELINE).addComponent(jL1)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)).addGap(87, 87, 87)BASELINE).addComponent(clear).addComponent(commit)).addContainerGap(88, Short.MAX_VALUE)));pack();}// </editor-fold>private void clearActionPerformed(ActionEvent evt) {str="";jT1.setText(str);}private voidif(Integer.parseInt(jT1.getText())%100==0){int a=Integer.parseInt(jT1.getText());if(a<0)JOptionPane.showMessageDialog(null,"存款不能为负数","出错",1);elsemyaccount.reccount(a);dis.set(String.valueOf(myaccount.get()));int showConfirmDialog(null YES_NO_OPTIONQUESTION_MESSAGE);if YES_OPTION){str="";jT1.setText(str);dis.setVisible(true);this.setVisible(false);}else{Jmain.setVisible(true);this.setVisible(false);}}else{JOptionPane.showMessageDialog(null,"存款金额必须为100的整数倍!"+"\n请确认你的金额","出错提示",1);jT1.setText("");}}}/*显示余额界面*/class Display extends JFrame {public JButton back;private JLabel jL1;public JTextField jT1;public JButton quit;public JButton qukuan;public Display() {initComponents();jT1.setText(String.valueOf(myaccount.get()));this.setLocationRelativeTo(null);this.setVisible(false);}public void set(String str){jT1.setText(str);}private void initComponents() {jL1 = new JLabel();jT1 = new JTextField();quit = new JButton();back = new JButton();qukuan = new JButton();setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);jL1.setText("您的余额为:");jT1.setEditable(false);quit.setText("退出");quit.addActionListener(newpublic voidquitActionPerformed(evt);}});back.setText("返回");back.addActionListener(newpublic voidbackActionPerformed(evt);}});qukuan.setText("取款");qukuan.addActionListener(newpublic voidqukuanActionPerformed(evt);}});newgetContentPane().setLayout(layout);layout.setHorizontalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(jL1) RELATEDDEFAULT_SIZE, Short.MAX_VALUE) PREFERRED_SIZEPREFERRED_SIZE).addContainerGap(36, Short.MAX_VALUE)) TRAILING, layout.createSequentialGroup().addContainerGap(215, Short.MAX_VALUE) TRAILING).addComponent(back).addComponent(qukuan).addComponent(quit)).addContainerGap()));layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(93, 93, 93)BASELINE).addComponent(jL1)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)).addGap(54, 54, 54).addComponent(qukuan)RELATED).addComponent(back)RELATED).addComponent(quit).addContainerGap()));pack();}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 = newEXIT_ON_CLOSE);oldpw.setText("旧密码:");newpw.setText("新密码:");commitpw.setText("确认密码:");commit.setText("确认");commit.addActionListener(newpublic voidcommitActionPerformed(evt);}});quit.setText("退出");quit.addActionListener(newpublic voidquitActionPerformed(evt);}});newgetContentPane().setLayout(layout);layout.setHorizontalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(46, 46, 46)LEADING).addComponent(oldpw).addComponent(newpw).addComponent(commitpw)).addGap(25, 25, 25)LEADING, false).addGroup(layout.createSequentialGroup().addComponent(commit) RELATEDDEFAULT_SIZE, Short.MAX_VALUE).addComponent(quit))DEFAULT_SIZE, 180, Short.MAX_VALUE).addComponent(jPwold).addComponent(jPwcommit)).addContainerGap(48, Short.MAX_VALUE)) );layout.setVerticalGroup(LEADING).addGroup(layout.createSequentialGroup().addGap(53, 53, 53)BASELINE).addComponent(oldpw)PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)).addGap(36, 36, 36)BASELINE).addComponent(newpw) PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)).addGap(40, 40, 40)BASELINE).addComponent(commitpw) PREFERRED_SIZEDEFAULT_SIZEPREFERRED_SIZE)) RELATED, 47, Short.MAX_VALUE)BASELINE).addComponent(commit).addComponent(quit)).addGap(36, 36, 36)));pack();}private void commitActionPerformed(ActionEvent evt) {boolean flag=false;//标志密码不能出现全部相同的数字if(!jPwold.getText().equals(myaccount.getpw())){JOptionPane.showMessageDialog(null,"你输入的密码与老密码不匹配!","错误提示",1);//1是符号二是标题}else if(jPwnew.getText().length()<6)JOptionPane.showMessageDialog(null,"新密码长度必须大于或等于6位","错误提示",1);else{//用一个循环来查询密码是否全部一样for(int i=0;i<jPwnew.getText().length();i++){if(jPwnew.getText().charAt(0)==jPwnew.getText().charAt(i))flag=true;else{flag=false;break;}}//密码全部一样则给出提示if(flag)JOptionPane.showMessageDialog(null,"密码不能全都相同","错误提示",1);//以上情况都没出现则修改成功else if(jPwold.getText().equals(myaccount.getpw()) && jPwnew.getText().equals(jPwcommit.getText())){myaccount.setpw(jPwnew.getText());JOptionPane.showMessageDialog(null,"密码修改成功,请记住你的新密码","",1);Jmain.setVisible(true);this.setVisible(false);}}}private voidload.setVisible(true);this.setVisible(false);}}}5.2 源代码说明5.2.1 源代码中自定义类的说明:代码中出现的Account类、MainFrame类、Load类、Take类、Input类、Setpew类和Display类都继承了Java类JFrame。

相关主题