#Java# ATM机系统(创建账号功能和保存账号功能实现)

ATM机系统具体构思及框架在https://blog.youkuaiyun.com/2301_80284166/article/details/137436771

1.功能分析

#创建账户方法的内容和要求:

  1. 根据不同地区,1 到 6位有不同的固定的银行代号数字;
  2. 7 到 15 位为随机给出的数字(Random,随机数);
  3. 第 16 位数由前十五位数按特定方式得出(暂时没有16位,只有15位);
  4. 用户输入身份证号码;
  5. 用户输入手机号码;
  6. 密码为用户自定义。

方法createAccount( Area  area) 实现我们的ATM机的创建账号功能,其中  Area 类是用来记录用户所处哪个地区,其中有 obtainNumber()方法 返回该地区的邮政编码。

#保存账号方法的内容和要求:

方法SaveAccountInformation()实现了保存账户信息,使用I/O类库中的方法对文件进行操作,将用户的信息存入文件,以便下一次能够找到。

2.代码实现

方法createAccount( Area area):

   @Override
    public void createAccount( Area area) {
        Scanner s = new Scanner(System.in);

        // 给出账号
        System.out.println("输入本人身份证号码:");
        String id = s.next();
        // 给出手机号
        System.out.println("输入本人手机号:");
        String mobileNumber = s.next();
        StringBuilder ac = new StringBuilder();
        ac.append(area.obtainNumber());
        SecureRandom num = new SecureRandom();
        for( int i = 0; i < 11; i++) {
            int t = num.nextInt(10);
            ac.append(t);
        }
        String accountNumber = ac.toString();
        System.out.println("请记住银行账号为:" + accountNumber);
        // 输入密码
        System.out.println("输入账号密码:");
        String cipher =s.next();
        // 创建一个账号,并且初始化账号
        // 保存账号
        saveAccountInformation(new Account( accountNumber, cipher, 0.0, 0.0, 0, id, mobileNumber));
    }

在具体实现中,我们创建账号,需要用到用户的身份证号码和手机号,在用户输入完身份证号码和手机号后,我们会通过用户传入的地区 area中方法obtainNumber() 返回特定的6位数为账号的前六位,然后通过随机类 SecureRandom得到9个随机数组成账号的后九位数,这样用户就得到了新的账号。

在用户输入完密码后,我们会通过 账号保存方法 saveAccountInformation() 将新账号信息保存到指定路径下。

方法SaveAccountInformation( Account account):

@Override
    public void saveAccountInformation( Account account) {
        // 为这个账户创建一个文本文档,存放账户信息
        String address = "E:\\Account\\" + account.getAccountNumber() + ".txt";
        File save = new File(address);

        // 如果文件已经存在,直接覆盖掉之前的信息保存
        if( save.exists() ) {
            try (FileOutputStream fileOut = new FileOutputStream(address) ) {
                ObjectOutputStream oos = new ObjectOutputStream(fileOut);
                oos.writeObject(account);
                oos.close();
                fileOut.close();

                System.out.println("账户信息保存成功");
            } catch (IOException e) {
                throw new RuntimeException("账户信息保存失败");
            }
        }

        // 如果文件不存在,者创建一个新文件存放账户信息
        else {
            try {
                if( save.createNewFile() ) {
                    try (FileOutputStream fileOut = new FileOutputStream(address) ) {
                        ObjectOutputStream oos = new ObjectOutputStream(fileOut);
                        oos.writeObject(account);
                        oos.close();
                        fileOut.close();

                        System.out.println("账户信息保存成功");
                    } catch (IOException e) {
                        throw new RuntimeException("账户信息保存失败");
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

将传入的账号保存在指定路径 address 下,先判断该文件是否存在,如果不存在说明该账号是新创建的账号,我们新创建一个文件去存放该账号信息,如果存在则直接覆盖掉之前的账号信息。

存储账号是我们通过将对象序列化,然后再存储在指定路径。

关于对象序列化,可看我主页 《简单理解Java文件操作与对象序列化》文章。在这里我就不过多介绍了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值