Java项目--ATM机基于swing程序

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

ATM(全称自动柜员机)是一种自动化银行服务设备,可供客户进行非现场交易和储蓄取款操作。JavaATM机是基于Java语言编写的模拟ATM机程序,旨在模拟真实ATM机的功能,让用户能够体验使用ATM机的感觉。本文将介绍JavaATM机的功能和使用方法。


提示:以下是本篇文章正文内容,下面案例可供参考

一、ATM机是什么?

ATM机是自动取款机(Automated Teller Machine)的简称,也叫现金自动提款机。它是一种可以自动完成账户查询、现金存取、转账等银行业务的机器,大多数银行和金融机构都提供ATM服务。ATM机可以方便地为客户提供24小时无时无刻的服务。用户可以使用银行卡及个人密码来操作ATM机进行各种银行交易。

二、项目设计

1.目的

通过实践操作,学会编程结构清晰、风格良好、数据结构适当的java语言程序,加深对java语言基础知识的理解和掌握,进一步巩固java语言编程实践能力,锻炼思维逻辑和解决综合性实际问题的能力。

2.内容

代码如下(示例):

import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
import java.awt.*;
public class ATM extends JFrame implements ActionListener {
	//登录页面
    JButton a=new JButton("欢迎使用湖北银行ATM机");
    JButton b=new JButton("退出系统");
    JButton c=new JButton("注册账号");
    JButton d=new JButton("登录账号");
    JLabel l=new JLabel("");
    JPanel l1=new JPanel();
    JPanel j1=new JPanel();
    //操作页面
    JButton a1 = new JButton("查询余额");
    JButton a2 = new JButton("取款功能");
    JButton a3 = new JButton("存款功能");
    JButton a4 = new JButton("转账功能");
    JButton a5 = new JButton("修改密码");
    JButton a6 = new JButton("退卡功能");
    JPanel l2 = new JPanel();
    JPanel j2 = new JPanel();
    // 创建一个二维数组用来保存账户信息
    String[][] accounts = new String[10][5];
    int accountCount = 0;
    // 创建一个二维数组用来保存金额
    double[][] balance = new double[10][5];
    double accountBalance = 0.0;
    public ATM() {
    //登录页面
        this.setTitle("欢迎使用湖北银行ATM机");
        // 添加按钮事件监听器
        a.addActionListener(this);
        b.addActionListener(this);
        c.addActionListener(this);
        d.addActionListener(this);
        l1.setLayout(new BorderLayout());
        l1.add(a,BorderLayout.NORTH);
        l1.add(b,BorderLayout.SOUTH);
        l1.add(c,BorderLayout.EAST);
        l1.add(d,BorderLayout.WEST);
        l1.add(l,BorderLayout.CENTER);
        this.add(l1, BorderLayout.CENTER);
    
        this.setBounds(300,250,400,200); // 设置窗口大小和位置
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 单击窗口关闭按钮时结束程序
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
    	if(e.getSource()==a) {
            JOptionPane.showMessageDialog(null,"欢迎使用湖北银行ATM机!", "提示",JOptionPane.PLAIN_MESSAGE);
        }
    	else if(e.getSource()==b){
            System.exit(0); // 直接结束程序
        }
        else if(e.getSource()==c) {
            // 注册账号和密码,并保存到数组中
            String accountNumber = JOptionPane.showInputDialog("请输入要注册的账号:");
            String password = JOptionPane.showInputDialog("请输入要设置的密码:");

            accounts[accountCount][0] = accountNumber;
            accounts[accountCount][1] = password;
            accountCount++;
            JOptionPane.showMessageDialog(null,"账号注册成功!", "提示",JOptionPane.PLAIN_MESSAGE);
        } 
        else if(e.getSource()==d) {
            // 登录验证
            String accountNumber = JOptionPane.showInputDialog("请输入账号:");
            String password = JOptionPane.showInputDialog("请输入密码:");
            boolean isLoginSuccess = false;
            for(int i=0;i<accountCount;i++) {
                if(accounts[i][0].equals(accountNumber) && accounts[i][1].equals(password)) {
                    isLoginSuccess = true;
                    break;
                }
            }
            if(isLoginSuccess) {
                // 显示操作界面,添加按钮和事件监听器
                l1.setVisible(false);
              //操作页面
                JLabel title = new JLabel("欢迎使用湖北银行ATM机", SwingConstants.CENTER);
                title.setFont(new Font("微软雅黑", Font.BOLD, 30));
                JLabel blank = new JLabel("");
                JLabel blank1 = new JLabel("");
                JPanel p1 = new JPanel();
                p1.add(title);
                JPanel j1 = new JPanel();
                j1.setLayout(new BorderLayout());
                a1.addActionListener(this);
                a2.addActionListener(this);
                a3.addActionListener(this);
                // 将“查询余额”、“取款”和“存款”按钮添加到左侧面板l2中
                l2.setLayout(new BoxLayout(l2, BoxLayout.Y_AXIS));
                l2.add(a1);
                l2.add(a2);
                l2.add(a3);

                a4.addActionListener(this);
                a5.addActionListener(this);
                a6.addActionListener(this);
                // 将“转账”、“修改密码”和“退卡”按钮添加到右侧面板j2中
                j2.setLayout(new BoxLayout(j2, BoxLayout.Y_AXIS));
                j2.add(a4);
                j2.add(a5);
                j2.add(a6);
                
                // 将左侧面板l2添加到操作界面的WEST位置
                j1.add(l2, BorderLayout.WEST);
                // 将右侧面板j2添加到操作界面的EAST位置
                j1.add(j2, BorderLayout.EAST);

                this.add(blank,BorderLayout.SOUTH);
                this.add(blank1,BorderLayout.NORTH);
                this.add(p1, BorderLayout.CENTER);
                this.add(j1, BorderLayout.SOUTH);
                //查询余额的相关操作
                a1.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   JOptionPane.showMessageDialog(null, "您当前的余额为"+accountBalance+"元。");
                	   }
                	});
                //取款的相关操作
                a2.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   String input = JOptionPane.showInputDialog("请输入您要取款的金额:");
                           int amount = Integer.parseInt(input);
                           if (amount <= 0 || amount%50!=0) {
                               JOptionPane.showMessageDialog(null, "无效的金额,请重新输入。");
                           } else if (amount > 10000) {
                               JOptionPane.showMessageDialog(null, "取款金额不能超过10000元。");
                               
                           } else if (amount > accountBalance) {
                               JOptionPane.showMessageDialog(null, "余额不够,请重新输入。"); 
                           } 
                           else {
                        	   accountBalance-=amount;
                               JOptionPane.showMessageDialog(null, "成功取出" + amount + "元。您的账户余额为:"+accountBalance );
                           }
                	   }
                	});
                // 存款的相关操作
                a3.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   String input = JOptionPane.showInputDialog("请输入您要存款的金额:");
                           int amount = Integer.parseInt(input);
                           if (amount <= 0 || amount%50!=0) {
                               JOptionPane.showMessageDialog(null, "无效的金额,请重新输入。");
                           } else {
                        	   accountBalance+=amount;
                               JOptionPane.showMessageDialog(null, "成功存入" + accountBalance + "元。");
                           }
                	   }
                	});
                // 转账的相关操作
                a4.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   String input = JOptionPane.showInputDialog("请输入对方账户:");
                           if (input.equals("")) {
                               JOptionPane.showMessageDialog(null, "未输入账户号码。");
                           } else {
                               String input2 = JOptionPane.showInputDialog("请输入转账金额:");
                               int amount = Integer.parseInt(input2);
                               if (amount <= 0 || amount%50!=0) {
                                   JOptionPane.showMessageDialog(null, "无效的金额,请重新输入。");
                               } else if (amount > 10000) {
                                   JOptionPane.showMessageDialog(null, "转账金额不能超过10000元。");
                               } else {
                            	   accountBalance-=amount;
                                   JOptionPane.showMessageDialog(null, "成功向账户" + input + "转账" + amount + "元。您的账户余额为:"+accountBalance);
                               }
                           }
                	   }
                	});
                // 修改密码的相关操作
                a5.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   String input = JOptionPane.showInputDialog("请输入原密码:");
                		   if (input.equals(password)) {
                               String newPassword = JOptionPane.showInputDialog("请输入新密码:");
                               JOptionPane.showMessageDialog(null, "密码修改成功。");
                           } else {
                               JOptionPane.showMessageDialog(null, "原密码错误。");
                           }
                       } 
                	});
                //退卡的相关操作
               	a6.addActionListener(new ActionListener() {
                	   public void actionPerformed(ActionEvent e) {
                		   JOptionPane.showMessageDialog(null, "退卡中...", "提示信息", JOptionPane.INFORMATION_MESSAGE);
                           System.exit(0);
                	   }
                	});
            } else {
                JOptionPane.showMessageDialog(null,"登录失败,请检查账号和密码是否正确!", "提示",JOptionPane.ERROR_MESSAGE);
            }
        } 
        
    }
    public static void main(String[] args) {
        ATM ATM=new ATM();
    }

}

【运行结果】


总结

实现功能清单:ATM机程序应该实现以下功能:

  • 登录
  • 取款
  • 存款
  • 查询余额
  • 转账
  • 修改密码
  • 退出

Java ATM机程序需要综合使用多项技术和编程概念,可以帮助开发者更好地掌握Java开发的核心思想和实践经验。

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在奋斗的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值