[java->ATM小系统]--用户转账功能实现

该文章介绍了如何使用tkinter创建一个ATM系统的前端界面,并详细讲解了实现用户转账功能的步骤,包括验证用户数量、账户余额检查、输入对方卡号及转账金额,以及转账过程中的错误处理。代码示例展示了具体的实现逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


个人名片:
🐅作者简介:一名大二在校生,热爱生活,爱好敲码!
‍ 💅个人主页 🥇:holy-wangle
➡系列内容: 🖼️ tkinter前端窗口界面创建与优化
✨个性签名: 🍭不积跬步,无以至千里;不积小流,无以成江海

案例文章

0. 完整代码存放位置
1. 系统准备、首页设计
2. 用户开户功能实现
3. 用户登录功能实现
4. 用户操作页设计、查询账户、退出账户功能实现
5. 用户存款功能实现
6. 用户取款功能实现
7.用户转账功能实现
8. 用户密码修改、销户功能实现

7.用户转账功能实现

具体实现:

1.先验证atm系统里面用户的数量是否大于2
2.验证通过后输入输入对应的需要转账的卡号并且验证需要转账用户的信息确认转账
3.输入转账金额,转账成功

代码实现:

/**
     * 转账
     * @param acc 当前用户信息
     * @param sc 扫描器
     * @param accounts 存储用户信息集合
     */

    private static void transferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("===============转账操作===============");
        // 1.判断用户是否大于2
        if (accounts.size() < 2 )
        {
            System.out.println("抱歉,用户账户数量不足二,无法进行转账操作!请前往注册!");
            return;
        }
        // 2.判断当前用户账户是否有钱
        if (acc.getAccountMoney() == 0 || acc.getAccountMoney() < 0)
        {
            System.out.println("抱歉您的账户余额为0元,无法进行转账操作!");
            return;
        }
        // 3.真正转账开始
        while (true) {
            System.out.println("请输入您转账的卡号:");
            String cardId = sc.next();

            // 判断卡号是否存在
            Account account = getSureCardId(cardId,accounts);
            if (account == null)
            {
                System.out.println("抱歉,您输入的对方卡号不正确!");
            }
            else
            {
                if (account.getCardId().equals(acc.getCardId()))
                {
                    System.out.println("抱歉,您不能向您自己的账户转账!");
                    continue;
                }
                else
                {
                    //账户存在,验证姓氏
                    String tip = "*" + account.getUserNames().substring(1);
                    System.out.println("请输入[" + tip + "]的姓氏");
                    String name = sc.next();
                    // 验证是否输入成功
                    if (account.getUserNames().startsWith(name))
                    {
                        System.out.println("验证成功!");
                        System.out.println("请输入您要转账的金额:");
                        double transferMoney = sc.nextDouble();
                        if (transferMoney > acc.getAccountMoney())
                        {
                            System.out.println("抱歉您的余额不足!您当前余额为:" + acc.getAccountMoney());
                            return;
                        }
                        acc.setAccountMoney(acc.getAccountMoney() - transferMoney);
                        account.setAccountMoney(account.getAccountMoney() + transferMoney);
                        System.out.println("转账成功!您当前余额为:" + acc.getAccountMoney());
                        return;
                    }
                    else
                    {
                        System.out.println("对不起,您输入错误!");
                    }

                }
            }
        }
    }

好了,今天的分享就这么多了,有什么不清楚或者我写错的地方,请多多指教!

私信,评论我呗!!!!!!

关注我下一篇不迷路哦!


这是用Java编写的一个简单的银行转账系统,包括取款,存款,转账功能,其中用到了数据库的连接,采用Eclipse编写,包含数据库的设计文件。非常适合有一定基础的Java初学者使用。 package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.*; /** * * @author gujunjia */ public class DataBase { static Connection conn; static PreparedStatement st; static ResultSet rs; /** * 加载驱动 */ public static void loadDriver() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } } /** * 创建数据库的连接 * * @param database * 需要访问的数据库的名字 */ public static void connectionDatabase(String database) { try { String url = "jdbc:mysql://localhost:3306/" + database; String username = "root"; String password = "gujunjia"; conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * 关闭数据库连接 */ public static void closeConnection() { if (rs != null) { // 关闭记录集 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { // 关闭声明 try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { // 关闭连接对象 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 本类主要实现整个系统的界面 * * @author gujunjia */ public class MainFrame extends JFrame implements ActionListener, FocusListener { /** * */ private static final long serialVersionUID = 1L; public static String userId; JTextField userIdText; JPasswordField passwordText; JButton registerButton; JButton logInButton; public MainFrame() { super("个人银行系统"); this.setSize(400, 500); this.setLocation(getMidDimension(new Dimension(400, 500))); getAppearance(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 获取屏幕的中间尺寸 * * @param d * Dimension类型 * @return 一个Point类型的参数 */ public static Point getMidDimension(Dimension d) { Point p = new Point(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); p.setLocation((dim.width - d.width) / 2, (dim.height - d.height) / 2); return p; } /** * 布局 * * @return Container */ public Container getAppearance() { Container container = this.getContentPane(); container.setLayout(new GridLayout(4, 0)); JLabel label1 = new JLabel("个人银行系统"); label1.setFont(new Font("楷体", Font.BOLD, 40)); JLabel label2 = new JLabel("账号:"); label2.setFont(new Font("楷体", Font.PLAIN, 15)); JLabel label3 = new JLabel("密码:"); label3.setFont(new Font("楷体", Font.PLAIN, 15)); userIdText = new JTextField(20); userIdText.addFocusListener(this); passwordText = new JPasswordField(20); passwordText.addFocusListener(this); JPanel jp1 = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp1.add(label1); jp2.add(label2); jp2.add(userIdText); jp3.add(label3); jp3.add(passwordText); registerButton = new JButton("注册"); registerButton.addActionListener(this); registerButton.setFont(new Font("楷体", Font.BOLD, 15)); logInButton = new JButton("登录"); logInButton.addActionListener(this); logInButton.setFont(new Font("楷体", Font.BOLD, 15)); jp4.add(registerButton); jp4.add(logInButton); container.add(jp1); container.add(jp2); container.add(jp3); container.add(jp4); return container; } public void actionPerformed(ActionEvent e) { Object btn = e.getSource(); if (btn == registerButton) { new Register(); } else if (btn == logInButton) { String id = userIdText.getText().trim(); String password = new String(passwordText.getPassword()); Bank bank = new Bank(); if (id.equals("") || password.equals("")) { JOptionPane.showMessageDialog(null, "请输入账号和密码"); } else { String dPassword = bank.getPassword(id); if (password.equals(dPassword)) { userId = id; this.dispose(); new UserGUI(); } else { JOptionPane.showMessageDialog(this, "密码或用户名错误", "错误", JOptionPane.ERROR_MESSAGE); } } } } @Override public void focusGained(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { userIdText.setText(""); userIdText.setFont(new Font("宋体", Font.BOLD, 15)); } else if (text == passwordText) { passwordText.setText(""); } } @Override public void focusLost(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { if (userIdText.getText().equals("")) { userIdText.setText("请输入账号"); userIdText.setFont(new Font("楷体", Font.ITALIC, 15)); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值