ATM系统(存取款以及转账功能)

ATM机系统

**ATM自动提款机**
 一级菜单
		    1. 注册(银行卡账号及密码)   2.登陆   3.退卡
二级菜单
1,存款  2.取款  3.查询余额   4.转账  5.返回上级菜单  4.转账(校验 1,不能自己转自己 2.转账账号是否存在)
-------(转账应注意的)--------
    存在:
     	输入转账金额
                校验金额(余额是否够)
                	true
                    	当前账户余额做减操作
                    	目标账户做余额加值操作
               	   false
                 		当前账户余额不足
     不存在:
            账户不存在
数据结构(账户 密码  余额)
方法:存款    取款    查询余额    转账     辅助类方法

账户代码

public class Acount {
    String acount;//账号
    String passworld;//密码
    int balance;//余额
    //存款方法
    boolean deposit(int sum){
        this.balance += sum;
        return true;
    }
    //取款方法
    boolean draw(int sum){
        this.balance -= sum;
        return true;
    }
    //查询当前用户的余额
    int select(Acount account1){
        return account1.balance;
    }
    //转账
    void transfer(Acount account1,Acount account2,int sum){
        account1.balance -= sum;
        account2.balance += sum;
        System.out.println("转账成功");
    }
}

测试类代码

package com.huayu.test;
import java.util.Scanner;
import java.util.Arrays;
public class Demo {
    public static void main(String[] args) {
        //记录登陆的账户及密码
        String login = "";
        String pass = "";
        //用于记录登陆的用户
        Acount login_user = new Acount();

        Scanner scanner = new Scanner(System.in);
        //定义首循环标签
        boolean main_menu = true;
        int count = 0;
        //定义数组存贮账号及密码三个
        String[][] count_array = new String[3][2];
        //定义数组存贮每次注册后的账户
        Acount[] user = new Acount[3];
        //创建用户,注册时为其一一赋值
        Acount acount = new Acount();
        while(main_menu){
            System.out.println("---------------1. 注册  2.登陆   3.退卡----------------------");
            switch (scanner.nextInt()){
                case 1:
                    if(count<3){
                        acount = new Acount();
                        //存贮注册过的每个用户,转账时使用
                        user[count] = acount;
                        System.out.println("----------------请输入你要注册的账号-------------------");
                        acount.acount = scanner.next();
                        System.out.println("----------------请输入你要设置的密码---------------------");
                        acount.passworld = scanner.next();
                        //如果账号未重复则可以进行保存,否则提示错误
                        boolean b_flag = true;
                        for(int i=0;i<count_array.length;i++){
                            if(acount.acount.equals(count_array[i][0])){
                                System.out.println("-----------账号重复,注册失败------------------");
                                b_flag = false;
                            }
                        }
                        if(b_flag){
                            //对账号及密码进行存贮
                            count_array[count][0] = acount.acount;
                            count_array[count][1] = acount.passworld;
                            count++;
                        }
                    }else{
                        System.out.println("------------------注册账号已达上限----------------------");
                    }
                    break;
                case 2:
                   // System.out.println(Arrays.toString(count_array));
//                    for(int i=0;i<count;i++){
//                        System.out.print("账号为:"+count_array[count][0]);
//                        System.out.print("密码为:"+count_array[count][1]+"\n");
//                    }

                    if(count == 0){
                        System.out.println("------------------请先注册---------------------");
                    }else{
                        boolean flag = false;
                        System.out.println("请输入你的账号");
                        String s_acount = scanner.next();
                        System.out.println("请输入你的密码");
                        String s_pass = scanner.next();
                        for (int i =0;i<count_array.length;i++){
                            if(s_acount.equals(count_array[i][0])&&s_pass.equals(count_array[i][1])){
                                flag = true;
                                //记录登陆的账户及密码
                                login = s_acount;
                                pass = s_pass;
                                //用户和账号是一一对应的,所以以i为链接点判断是哪个用户
                                login_user = user[i];
                                System.out.println("登陆成功");
                            }
                        }
                        if(!flag){
                            System.out.println("登陆失败,请重新登陆");
                        }
                        if(flag){
                            boolean sec_menu = true;
                            //定义存款金额
                            int money = 0;
                            //定义取款金额
                            int i = 0;

                            while(sec_menu){
                                //存款或取款或余额都是基于  登陆账户  而言
                                System.out.println("--------------1,存款   2.取款     3.查询余额    4.转账    5.返回上级菜单---------------");
                                switch (scanner.nextInt()){
                                    case 1:
                                        //存款
                                        System.out.println("请输入存款金额");
                                        money = scanner.nextInt();
                                        if(money>0){
                                            login_user.deposit(money);
                                            System.out.println("存款成功");
                                        }
                                        break;
                                    case 2:
                                        System.out.println("请输入取款金额");
                                        i = scanner.nextInt();
                                        if(i<=login_user.balance){
                                            login_user.draw(i);
                                            System.out.println("取款成功");
                                        }else{
                                            System.out.println("余额不足");
                                        }
                                        break;
                                    case 3:
                                        System.out.println("当前账户余额为:"+login_user.select(login_user));
                                        break;
                                    case 4:
                                        boolean s = false;
                                        //转出账户即为当前登陆的账户 不能操作别人账户
//                                        System.out.println("请输入要转出的账户");
//                                        int out_account = scanner.nextInt();
                                        System.out.println("请输入要转入的账户");
                                        String in_account = scanner.next();
                                        if(login==in_account){
                                            System.out.println("您输入的账户为您本人的账户");
                                        }else{
                                            for(int n=0;n<count_array.length;n++){
                                                //会多次判断,重复输出
//                                                if(in_account.equals(count_array[n][0])){
//
//                                                }else{
//                                                    System.out.println("您输入的账户不存在");
//                                                }
                                                if(in_account.equals(count_array[n][0])){
                                                    //输入的账户存在
                                                    s = true;
                                                    //定义变量记录n值,方便转账时以此为连接对应
                                                }
                                            }
                                            if(!s){
                                                System.out.println("您输入的账户不存在");
                                            }
                                            //账户存在,开始转账操作
                                           if(s){
                                               System.out.println("请输入要转账的金额");
                                               int transfer_money = scanner.nextInt();
                                               //循环判断输入账户所对应用户
                                               //注册了几个账号及有几个用户,就循环几次
                                               for(int u = 0;u<count;u++){
                                                   if(in_account.equals(user[u].acount)){
                                                       acount.transfer(login_user,user[u],transfer_money);
                                                   }
                                               }
                                           }
                                        }
                                        break;
                                    case 5:
                                        sec_menu = false;
                                        break;
                                    default:
                                        System.out.println("输入错误,请重新输入");
                                }
                            }
                        }
                    }
                    break;
                case 3:
                    main_menu = false;
                    System.out.println("-------------------欢迎下次光临----------------------");
                    break;
                default:
                    System.out.println("输入有误请重新输入");
            }
        }
    }
}
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是用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)); } } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值