Java如何实现本地存储随机账号ID并在存取款操作时校验
Java银行管理程序实现本地账号持久化校验方案
实现思路
Java没有内置类似JavaScript localStorage的本地存储API,轻量场景下可以用Properties类读写本地配置文件实现持久化能力,无需引入额外依赖,适配当前项目需求。核心逻辑如下:
- 开户成功后将生成的账号ID、用户信息写入本地
.properties配置文件 - 执行取款、存款操作前,先读取本地存储的账号ID,和用户输入的内容做校验,匹配通过才允许执行后续业务代码
- 只要不删除本地配置文件,重启程序后历史账号信息依然有效
完整修改后代码
import java.util.Scanner; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; class AccountService { protected Scanner sc = new Scanner(System.in); // 配置文件路径,存在当前运行目录 private static final String CONFIG_PATH = "bank_account.properties"; // 公共工具方法:读取本地存储的账号ID protected Long getSavedAccountId() { Properties prop = new Properties(); try (FileInputStream fis = new FileInputStream(CONFIG_PATH)) { prop.load(fis); return Long.parseLong(prop.getProperty("accountId")); } catch (IOException | NumberFormatException e) { return null; } } // 公共工具方法:保存账号信息到本地 protected void saveAccountInfo(String name, String address, Long accountId) { Properties prop = new Properties(); prop.setProperty("name", name); prop.setProperty("address", address); prop.setProperty("accountId", String.valueOf(accountId)); try (FileOutputStream fos = new FileOutputStream(CONFIG_PATH)) { prop.store(fos, "Bank Account Info"); } catch (IOException e) { System.out.println("账号信息保存失败:" + e.getMessage()); } } // 公共工具方法:账号校验 protected boolean verifyAccount() { Long savedId = getSavedAccountId(); if (savedId == null) { System.out.println("未检测到开户信息,请先开户"); return false; } System.out.println("请输入您的账号ID:"); long inputId = sc.nextLong(); sc.nextLine(); // 处理换行符 if (inputId == savedId) { System.out.println("账号校验通过"); return true; } else { System.out.println("账号错误,无操作权限"); return false; } } } class CreateAcc extends AccountService{ CreateAcc(){ System.out.println("Welcome to Bank.\nDo you want to make us your life partner ?(Yes/No)"); String decision = sc.nextLine(); switch (decision){ case "Yes": break; case "No": return; default: System.out.println("Invalid Input. Check the format in which yes and no are to be written "); return; } System.out.println("Please enter your full name "); String name= sc.nextLine(); System.out.println("Please enter your full address "); String address= sc.nextLine(); System.out.println("Please wait while we are processing with your account"); try{Thread.sleep(1000);}catch(InterruptedException e){System.out.println(e);} System.out.println("This is our recorded information:\nYour name:\t"+name.toUpperCase()+"\nYour address:\t"+address.toUpperCase()); System.out.println("Please wait while we are generating your Account number"); try{Thread.sleep(2000);}catch(InterruptedException e){System.out.println(e);} int min=5000; int max=10000000; // 改用long类型存储账号,避免double精度问题 long accountId = (long) (Math.random()*(max-min+1)+min); // 保存账号到本地 saveAccountInfo(name, address, accountId); System.out.println("Your account id is "+accountId+"\n Keep it carefully as you may need this for logging into your account in future"); System.out.println("Thank you for visiting us"); } } class Withdrawal extends AccountService{ Withdrawal(){ // 先校验账号 if (!verifyAccount()) { return; } // 后续取款业务代码写在这里 System.out.println("欢迎进入取款功能"); } } class Deposit extends AccountService{ Deposit(){ // 先校验账号 if (!verifyAccount()) { return; } // 后续存款业务代码写在这里 System.out.println("欢迎进入存款功能"); } } public class BankManager { public static void main(String [] agrs){ Scanner sc = new Scanner(System.in); while (true) { System.out.println("\n=====银行管理系统====="); System.out.println("1. 开户"); System.out.println("2. 取款"); System.out.println("3. 存款"); System.out.println("4. 退出"); System.out.println("请输入操作编号:"); int choice = sc.nextInt(); sc.nextLine(); switch (choice) { case 1: new CreateAcc(); break; case 2: new Withdrawal(); break; case 3: new Deposit(); break; case 4: System.out.println("感谢使用,程序退出"); System.exit(0); default: System.out.println("无效输入,请重新选择"); } } } }
注意事项
- 账号生成时改用long类型存储,避免原来用double可能出现的精度丢失问题
- 生成的
bank_account.properties配置文件会保存在程序运行的当前目录,删除该文件会清空所有已存的账号信息 - 原代码不合理的继承结构已调整,改用公共父类封装通用工具方法,逻辑更清晰
- 缩短了原代码的sleep等待时间,方便调试,可根据需求自行修改
内容的提问来源于stack exchange,提问作者Satyaki Roy ghatak
相关产品推荐
相关产品推荐

