好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现银行ATM管理系统

本文实例为大家分享了java实现银行ATM管理系统的具体代码,供大家参考,具体内容如下

功能

账户类、首页设计

分析

① 每个用户一个账户对象,需要设计账户类,账户类至少包含(卡号、用户名、余额、取现额度、密码)

② 需要定义一个ArrayList的集合用于存储账户对象。

③ 需要展示欢迎页包含2个功能:注册开户、登录账户。

用户开户功能实现

① 开户功能应该独立定义成方法,并传入当前集合对象给该方法。

public static void register(ArrayList<Account> accounts) {…}

② 需要提示用户输入个人信息,开户的卡号是系统自动生成的8位数。

public static String createCardId(){…}

③ 注意:自动生成的卡号不能与其他用户的卡号重复。

④ 最终把用户开户的信息封装成Account对象,存入到集合中。

用户登录功能实现

分析

① 需要根据卡号去集合中查询对应的账户对象。

② 如果找到了账户对象,说明卡号存在,继续输入密码。

③ 如果密码也正确,则登录成功。

用户操作页设计、查询账户、退出账户功能

分析

① 用户登录成功后,需要进入用户操作页。

② 查询就是直接展示当前登录成功的账户对象的信息。

③ 退出账户是需要回到首页的

用户存款、取款功能设计

分析

① 存款和取款都是拿到当前用户的账户对象。

② 通过调用账户对象的set方法修改其余额。

用户转账功能设计

分析

① 转账功能要分析对方账户是否存在的问题。

② 还要分析自己的余额是否足够的问题。

用户密码修改功能、销户功能

分析

① 修改密码就是把当前对象的密码属性使用set方法进行更新。

② 销户是从集合对象中删除当前对象,并回到首页。

输出流

?

1

2

3

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

首页设计

?

1

2

3

4

5

6

7

8

9

10

11

12

public class Bank {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        ArrayList<Account> list = new ArrayList<>();

 

        //首页设计

        while ( true ){ //while死循环

 

        System.out.println( "========欢迎您进入万和银行ATM系统========" );

        System.out.println( "1.登录账户" );

        System.out.println( "2.注册开户" );

        System.out.println( "请输入命令1、2选择对应的操作" );

首页界面选择

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//首页界面选择

String choice =sc.next();

    switch (choice){ //switch语句使输入的指令进入不同的功能

                case "1" :

                    System.out.println( "========欢迎您进入万和银行用户登录界面========" );

                    login(list);

                    break ;

                case "2" :

                    System.out.println( "========欢迎您进入万和银行用户办卡界面========" );

                    register(list);

                    break ;

                case "后台统计" : //为后台统计数据使用,主界面直接输入[后台统计]可以显示系统所有成员信息

                    System.out.println( "========后台统计========" );

                    htlook(list);

                    break ;

                default :

                    System.out.println( "您输入的数字有误,请重新输入" );

          }

     }

}

1、用户登录功能

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

//  1、用户登录功能

public static void login(ArrayList<Account> list) {

            Scanner sc = new Scanner(System.in); //创建键盘录入对象

 

            System.out.println( "请输入您的卡号" );

            String idcard = sc.next();

 

            int index = getIndex(list, idcard); //调用方法判断后台是否有该卡号

            if (index == - 1 ) {

                System.out.println( "不存在该卡号,请重新输入" );

            } else {

                for ( int i = 0 ; i < list.size(); i++) {

                    System.out.println( "请您输入您的密码" );

                    String password = sc.next();

 

                    if (password.equals(list.get(index).getPassword())){ //如果输入的卡号的索引index与该索引下的密码一致则登录成功

                        System.out.println( "万和" + list.get(index).getUsername() + "贵宾,欢迎您进入系统,您的卡号:" + list.get(index).getId());

                        System.out.println();

 

                        while ( true ){ //卡号密码成功,进入用户界面

                            System.out.println( "==========欢迎您进入万和银行用户操作界面==========" );

                            System.out.println( "1   查询" );

                            System.out.println( "2   存款" );

                            System.out.println( "3   取款" );

                            System.out.println( "4   转账" );

                            System.out.println( "5   修改密码" );

                            System.out.println( "6   退出" );

                            System.out.println( "7   注销当前账户" );

                            System.out.println( "请输入您要输入的功能" );

 

                            String choice = sc.next(); //用户输入

 

                            switch (choice){

                                case "1" :

                                    System.out.println( "==========欢迎您进入万和银行用户详情界面=========" );

                                    querymessage(list, index); //查询个人信息方法

                                    break ;

 

                                case "2" :

                                    System.out.println( "==========欢迎您进入万和银行用户存款界面=========" );

                                    inmoney(list, sc, index); //存款

                                    break ;

 

                                case "3" :

                                    System.out.println( "==========欢迎您进入万和银行用户取款界面=========" );

                                    outmoney(list, sc, index); //取款

                                    break ;

                                case "4" :

                                    System.out.println( "==========欢迎您进入万和银行用户转账界面=========" );

                                    transfer(list, sc, index); //转账功能

                                    break ;

                                case "5" :

                                    System.out.println( "==========欢迎您进入万和银行用户密码修改界面=========" );

                                    revisePassword(list, sc, index); //修改密码

                                    return ;

                                case "6" :

                                    System.out.println( "退出" );

                                    return ; //结束整个方法,回到主界面

                                case "7" : //用户注销

                                    System.out.println( "==========欢迎您进入万和银行用户注销界面=========" );

                                        System.out.println( "您确定要注销该账户?" );

                                        System.out.println( "1   确定" );

                                        System.out.println( "按任意键返回上一页" );

                                        System.out.println( "请输入您要输入的选项" );

                                        String choice0 = sc.next();

                                        switch (choice0){

                                            case "1" :

                                                Account ac = list.get(index);

                                                while ( true ){

                                                    System.out.println( "请输入当前账户的密码" );

                                                    String key = sc.next();

                                                    if (key.equals(ac.getPassword())){

                                                        list.remove(index);

                                                        System.out.println( "注销成功,将返回主界面" );

                                                        return ;

                                                    }

                                                }

                                            default :

                                                break ;

                                        }

                                        break ;

                            }

                        }

                    } else

                        {

                            System.out.println( "您输入的密码有误,请确认" );

                    }

                }

            }

        }

转账功能

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

//转账功能

private static void transfer(ArrayList<Account> list, Scanner sc, int index) {

    Account acc = list.get(index); //将集合list在index索引下的数据传入acc

        int num = list.size(); //获取集合长度

        if (num < 2 ){ //如果集合长度小于二,不能转账

            System.out.println( "当前系统不足两个,不能转账" );

        } else {

            if (acc.getBalance() < 100 ){ //余额小于100,不能转账

                System.out.println( "余额不足" );

            } else {

                while ( true ){

                    System.out.println( "请输入要转账的账号" );

                    String anotherId = sc.next();

                    int anotherIdIndex = getIndex(list,anotherId); //判断账号在系统中是否存在

                    if (anotherIdIndex == - 1 ){ //如果返回-1,不存在

                        System.out.println( "您输入的账户不存在,请重新确认" );

                    } else {

                        Account acc1 = list.get(anotherIdIndex); //将刚刚输入的索引下的数据传入acc1,留作接下来修改

                        String name = acc1.getUsername();

                        char firstName = name.charAt( 0 ); //获取用户名的姓氏

                        String xname = name.replace(firstName, '*' ); //将用户名的姓氏变为*输出

                        System.out.println( "您当前要为" + xname + "转账" );

                        System.out.println( "请您输入姓氏确认:" );

                        String  firstname = sc.next();

                        char chr = firstname.charAt( 0 ); //输入的姓氏和系统的姓氏对比

                        if (chr == firstName){ //如果相等,都是char形式才能比较

                            System.out.println( "输入正确" );

                            while ( true ){

                                System.out.println( "请输入要转入的金额:" );

                                double sendmoney = sc.nextDouble();

 

                                if (sendmoney < list.get(index).getEnchashment()){

                                    System.out.println( "您当前转账超过了当次限额!" );

                                }

                                else if (sendmoney > list.get(index).getBalance()){

                                    System.out.println( "您的余额不足" );

                                } else {

                                    acc.setBalance(list.get(index).getBalance() - sendmoney); //对自己的金额扣钱

                                    acc.setBalance(list.get(anotherIdIndex).getBalance() + sendmoney); //对别人的账户打款

                                    System.out.println( "转账成功" );

                                    return ;

                                }

                            }

                        }

                    }

                }

            }

        }

    }

修改密码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//修改密码

private static void revisePassword(ArrayList<Account> list, Scanner sc, int index) {

    Account acc = list.get(index);

        while ( true ){

            System.out.println( "请您输入当前账户密码:" );

            String nowpassword = sc.next();

            if (nowpassword.equals(acc.getPassword())){ //看原来的密码是否输入正确

                System.out.println( "请您输入新的密码:" );

                String newpassword = sc.next();

                list.get(index).setPassword(newpassword); //set功能将新的密码写入系统

                System.out.println( "密码修改成功请您重新登录" );

                return ;

            } else {

                System.out.println( "当前账户密码不正确" );

            }

        }

    }

取款

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//取款

private static void outmoney(ArrayList<Account> list, Scanner sc, int index) {

    Account acc = list.get(index);

        if (list.get(index).getBalance() < 100 ){

            System.out.println( "余额不足100元,无法取出,请存入金额!" );

        } else {

            while ( true ){

                System.out.println( "请输入取款的金额" );

                double outmoney = sc.nextDouble();

 

                if (outmoney < list.get(index).getEnchashment()){

                    System.out.println( "您当前取款超过了当次限额!" );

                } else if (list.get(index).getBalance()- outmoney < 0 ){

                    System.out.println( "您的账户余额不足" );

                } else {

                    acc.setBalance(list.get(index).getBalance() - outmoney);

                    System.out.println( "您已取款成功!" );

                    break ;

                }

            }

        }

    }

查询个人信息方法

?

1

2

3

4

5

6

7

8

9

10

11

//查询个人信息方法

    private static void querymessage(ArrayList<Account> list, int index) {

        System.out.println( "您的账户信息如下:" );

        for ( int i1 = 0 ; i1 < list.size(); i1++) { //遍历

            Account acc = list.get(index); //将数据导进来

            System.out.println( "卡号:" + acc.getId());

            System.out.println( "姓名:" + acc.getUsername());

            System.out.println( "余额:" + acc.getBalance());

            System.out.println( "当次取现额度:" + acc.getEnchashment());

        }

    }

存款

?

1

2

3

4

5

6

7

8

//存款

    private static void inmoney(ArrayList<Account> list, Scanner sc, int index) {

        System.out.println( "请您输入存款的金额" );

        double inmoney = sc.nextDouble();

        Account acc = list.get(index);

        acc.setBalance(list.get(index).getBalance() + inmoney) ; //集合list中的值为index的索引下的余额balance 加 刚刚存入的金额inmoney

        System.out.println( "您已经存款成功" );

    }

2、用户开户功能

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

// 2、用户开户功能

public static void register(ArrayList<Account> list){

        Scanner sc = new Scanner(System.in);

 

        System.out.println( "请您输入您的姓名:" );

        String username = sc.next();

 

        String password;

        while ( true ){

            System.out.println( "请您输入您的密码:" );

            String password1 = sc.next();

            System.out.println( "请您再次确认密码:" );

            String password2 = sc.next();

            if (password1.equals(password2)){

                password = password2;

                break ;

            } else {

                System.out.println( "两次密码不一致,请重新输入" );

            }

        }

        System.out.println(password);

 

        System.out.println( "请设置当日取现额度:" );

        double enchashment = sc.nextDouble();

 

        //自动生成八位数卡号并检测是否重复

        Random r = new Random();

        String s = new String();

        while ( true ) {

            for ( int i = 0 ; i < 8 ; i++) {

                int num = r.nextInt( 10 );

                s += num;

            }

            int index = getIndex(list, s); //调用getIndex方法返回-1则没有重复

            if (index == - 1 ){

                break ;

            }

        }

 

        //设置账户余额

        double balance = 0 ;

        Account acc = new Account(s,username,enchashment,password,balance);

        list.add(acc);

        System.out.println( "万和" + username + "贵宾,您的账户已经开卡成功,您的卡号是:" + s);

        }

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//获取集合中的用户的索引and判断集合中是否有该对象

private static int getIndex(ArrayList<Account> list, String s) {

        int index = - 1 ; //假设新生成的卡号,在集合中不存在

        for ( int i = 0 ; i < list.size(); i++) { //遍历集合,获取每一个对象,准备进行查找

            Account ac = list.get(i); //类名 对象名 = 集合名.get(i);

            String idcard = ac.getId(); //获取每一个对象的学号叫id

            if (idcard.equals(s)){ //判断获取过的id和新生成的s是否一样,如果相同赋值给index

                index = i; //存在,让index变量记录对象的索引值

            }

        }

        return index;

    }

    //查看信息(后台统计)

    public static void htlook(ArrayList<Account> list){

        for ( int i = 0 ; i < list.size(); i++) {

            Account acc = list.get(i);

            System.out.println(acc.getId()+ "    " + acc.getEnchashment() + "   " + acc.getUsername() + "    " + acc.getPassword() + "   " + acc.getBalance());

        }

    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/jiangyiwei9999/article/details/123604096

查看更多关于java实现银行ATM管理系统的详细内容...

  阅读:19次