java模拟实现ATM机功能

Java
182
0
0
2023-09-17

java模拟实现ATM机功能

案例:

模拟实现ATM机的功能:

功能描述:

  • 实现用户程登录及创建,实现查询余额,取出,存款,转账等

1、 测试 类:

 public class Atm {
    public static void main(String[] args) {
        Method me =new Method();
        me.start();
    } 

2、用户类:

 public class Usr {
    private  String  account  ;//账户
    private String cood;//密码
    private long  balance ;//余额

    public Usr(String account,String cood,long balance) {
        this.cood=cood;
        this.account=account;
        this.balance=balance;

    }

    public Usr() {
    }

    public void setCood(String cood) {
        this.cood=cood;
    }
    public String getCood() {
        return cood;
    }

    public void setAccount(String account) {
        this.account=account;
    }
    public String getAccount() {
        return account;
    }

    public void setBalance(long balance) {
        this.balance=balance;
    }
    public long getBalance() {
        return balance;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return "Usr{" +
                "account='" + account + ''' +
                ", cood='" + cood + ''' +
                ", balance=" + balance +
                '}';
    }
} 

3、方法类:

 import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Method {
    Random ran = new Random();
    ArrayList<Usr> list = new ArrayList<Usr>();
    Scanner sc = new Scanner(System.in);
    Usr usr = new Usr();

    public void method() {                        //创建4个初始账户
        Usr usr = new Usr("123456", "666666", 100000);
        Usr usr = new Usr("888888", "666666", 200000);
        Usr usr = new Usr("666666", "666666", 300000);
        Usr usr = new Usr("999999", "666666", 400000);
        list.add(usr);
        list.add(usr);
        list.add(usr);
        list.add(usr);
    }

    public void start() {
        method();
        System.out.println("这里创建了个供测试的账户分别是:");
        System.out.println(list.get());
        System.out.println(list.get());
        System.out.println(list.get());
        System.out.println(list.get());
        System.out.println("欢迎使用本公司ATM机,请选择您需要的服务");
        method();
    }

    public void method() {                         // 选择服务编号


        System.out.println(".登陆    2.创建账户    3.退出");
        int a = shuRu(, 3);
        switch (a) {
            case:
                // 有账户,直接登陆;
                dl();
                 break ;
            case:
                // 无账户,创建账户
                cj();
                break;
            case:
                break;
        }
    }

    public int shuRu(int min, int max) {        //输入
        int aa =;
        while (true) {
            String str = sc.nextLine();
            try {
                int a =  Integer .valueOf(str);
                if (a >= min && a <= max) {
                    aa = a;
                    break;
                } else {
                    System.out.println("请输入正确的服务编号");
                }
            } catch (Exception e) {
                System.out.println("输入不合法,请您输入正确的服务编号");
            }
        }
        return aa;
    }

    public int shuRu(int min) {        //输入
        int aa =;
        while (true) {
            String str = sc.nextLine();
            try {
                int a = Integer.valueOf(str);
                if (a >= min && a <= Integer.MAX_VALUE) {
                    aa = a;
                    break;
                } else {
                    System.out.println("请输入合理的金额,您最小的操作金额为元");
                }
            } catch (Exception e) {
                System.out.println("输入不合法");
            }
        }
        return aa;
    }

    public void cj() { // 创建账户
        System.out.println("请输入您的六位密码,您共有三次输入机会");
        String str = method020101();
        if (str == "") {
            System.out.println("您的输入机会以用完,请重新选择服务");
            method();
        } else {
            System.out.println("请确认您的六位密码,您共有三次确认机会");
            String str = method020101();
            if (str == "") {
                System.out.println("您的输入机会以用完,请重新选择服务");
                method();
            } else {
                boolean a = method(str1, str2);    //比较两次输入
                if (a) {
                    String str = sczh();
                    usr.setA cc ount(str3);
                    usr.setCood(str);
                    list.add(usr);
                    System.out.println("恭喜您,您的账户创建成功,账号为:" + str + "您的密码为:" + str1);
                    fw(str, str1);
                } else {
                    System.out.println("您两次输入的密码不同,请选择您需要的服务");
                    method();
                }
            }
        }
    }

    public String method() { // 输入密码
        String str = "";
        for (int a =; a >= 1; a--) {
            String str = sc.nextLine();
            try {
                int aa = Integer.valueOf(str);
                if (str.length() == 6) {
                    str = str;
                    a =;
                } else {
                    if (a >) {
                        System.out.println("请您输入位密码,您还有" + (a - 1) + "次机会");
                    } else {
                        break;
                    }
                }

            } catch (Exception e) {
                if (a >) {
                    System.out.println("输入格式不合法,请输入位数字,例如666666");
                }
            }

        }
        return str;
    }

    public boolean method(String str1, String str2) {                //比较两次输入
        boolean a;
        if (str.equals(str2)) {
            a = true;
        } else {
            a = false;
        }
        return a;
    }

    public String sczh() {                               //随机生成账户
        String str = "";
        for (int b =; b < 6; b++) {
            int a = ran.nextInt();
            str += a;
        }
        for (int a =; a < list.size(); a++) {
            if (str.equals(list.get(a).getAccount())) {
                System.out.println("该账户以存在,请重新选择服务编号");
                method();
                break;
            }
        }
        return str;
    }

    public void dl() {                                 //登录
        System.out.println("尊敬的客户,您好,请输入您的账户,您共有三次输入机会");
        String zz;
        String str;
        for (int a =; a >= 1; a--) {
            str = sc.nextLine();
            try {
                int aa = Integer.valueOf(str);
                if (str.length() ==) {
                    for (int cc =; cc < list.size(); cc++) {
                        if (str.equals(list.get(cc).getAccount())) {
                            System.out.println("请输入您的六位密码");
                            String str = method020101();
                            if (str.equals(list.get(cc).getCood())) {
                                zz = list.get(cc).getAccount();
                                System.out.println("登录成功,请选择您需要的服务");
                                fw(zz, str);
                                a =;
                                break;
                            } else {
                                System.out.println("密码输入错误,请重新选择服务编号");
                                method();
                                a =;
                                cc = list.size();
                                break;
                            }

                        } else if (!str.equals(list.get(cc).getAccount()) && cc == list.size() -) {
                            System.out.println("账户不存在,请重新输入");
                        }
                    }
                } else {
                    if (a >) {
                        System.out.println("输入错误,请重新输入您的账户,您还有" + (a -) + "次机会");
                    } else {
                        System.out.println("您的输入机会以用完,请重新选择服务");
                        method();
                        a =;
                        break;
                    }
                }
            } catch (Exception e) {
                if (a >) {
                    System.out.println("输入格式错误,请重新输入您的账户,您还有" + (a -) + "次机会");
                } else {
                    System.out.println("您的输入机会以用完,请重新选择服务");
                    method();
                    a =;
                    break;
                }
            }
        }
    }

    public void fw(String account, String cood) {
        System.out.println("尊敬的客户,您好,请选择您需要的服务:.修改密码   2.存款   3.取款   4.查询余额   5.转账   0.退出");
        int a = shuRu(, 5);
        int b = czzh(account, cood, a);
        switch (a) {
            case:
                fw(account, cood, b);
                break;
            case:
                fw(account, cood, b);
                break;
            case:
                fw(account, cood, b);
                break;
            case:
                fw(account, cood, b);
                break;
            case:
                fw(account, cood, b);
                break;
            case:
                System.out.println("服务结束,欢迎您的下次使用");
        }
    }

    public void fw(String account, String cood, int b) {
        System.out.println("您确定要修改您的密码,、确定   2、返回");
        int bb = shuRu(, 2);
        switch (bb) {
            case:
                System.out.println("请输入您的原密码");
                String str = method();
                boolean aa = method(str, cood);
                if (aa) {
                    System.out.println("请输入您的新密码");
                    String str = method020101();
                    System.out.println("请确认您的新密码");
                    String str = method020101();
                    boolean a = method(str1, str2);
                    if (a) {
                        list.get(b).setCood(str);
                        System.out.println("您的密码修改成功,您的新密码为:" + str);
                        fw(account, cood);

                    } else {
                        System.out.println("您两次输入的密码不同,请重新选择服务");
                        fw(account, cood);
                    }
                } else {
                    System.out.println("您的密码输入错误,请重新选择服务");
                    fw(account, cood);
                }
                break;
            case:
                fw(account, cood);
                break;
        }
    }

    public void fw(String account, String cood, int b) {//不可能这个账户不存在,所以没有进行判断
        System.out.println("请输入您要存入的金额");
        int a = shuRu();
        list.get(b).setBalance((list.get(b).getBalance()) + a);
        System.out.println("存款成功,您的余额为:" + list.get(b).getBalance());
        fw(account, cood);
    }

    public void fw(String account, String cood, int b) {
        System.out.println("请输入你要取出的金额");
        int a = shuRu();
        if (a <= list.get(b).getBalance()) {
            list.get(b).setBalance((list.get(b).getBalance()) - a);
            System.out.println("取款成功,您的余额为:" + list.get(b).getBalance());
            fw(account, cood);
        } else {
            System.out.println("您的余额不足,请确认余额");
            fw(account, cood);
        }
    }

    public void fw(String account, String cood, int b) {
        System.out.println("您的余额为:" + list.get(b).getBalance());
        fw(account, cood);
    }

    public void fw(String account, String cood, int b) {
        System.out.println("请输入对方的账户");
        String str = fw();
        boolean a = fw(str);
        if (a) {
            System.out.println("请输入您要转账的金额");
            int c = shuRu();
            if (c <= list.get(b).getBalance()) {
                list.get(b).setBalance(list.get(b).getBalance() - c);
                System.out.println("转账成功,您的余额为:" + list.get(b).getBalance());
                fw(account, cood);
            } else {
                System.out.println("您的余额不足,请确认您的余额后重试");
                fw(account, cood);
            }
        } else {
            System.out.println("账户不存在,请确认后再操作");
            fw(account, cood);
        }
    }

    public String fw() {
        String str;
        while (true) {
            String str = sc.nextLine();
            try {
                int aa = Integer.valueOf(str);
                if (str.length() == 6) {
                    str = str;
                    break;
                } else {
                    System.out.println("请输入正确的六位账户");
                }
            } catch (Exception e) {
                System.out.println("格式错误,请输入正整数");
            }
        }
        return str;
    }

    public boolean fw(String str) {
        boolean aa = false;
        for (int a =; a < list.size(); a++) {
            if (str.equals(list.get(a).getAccount())) {
                aa = true;
            }
        }
        return aa;
    }

    public int czzh(String account, String cood, int a) {                 //查找账户在集合中是第几号元素
        int aa = -;
        for (int b =; b < list.size(); b++) {
            if (account.equals(list.get(b).getAccount())) {
                aa = b;
            }
        }
        return aa;
    }

}

如有不同看法,欢迎留下您宝贵的意见!