In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to implement the ATM system with Java. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
1. System preparation, home page, user account opening function system preparation, home page design
The system prepares for content analysis:
Each user's account information is an object, and the account class needs to be provided.
You need to prepare a container to store and system all account object information
The home page only needs to include 2 functions: login and registration
Implementation steps:
Define an account class, which is used to create an account object later to encapsulate the user's account information
The information in the account class should at least contain (card number, name, password, balance, withdrawal limit)
Package com.wangxinhua;import java.util.ArrayList;import java.util.Scanner;public class ATMSystem {public static void main (String [] args) {/ / 1. Prepare the container object needed by the system. The user stores the account object ArrayList accounts = new ArrayList (); / / 2. Prepare the home page of the system, login, open an account showMain (accounts);} public static void showMain (ArrayList accounts) {System.out.println ("= Welcome to the home page ="); Scanner sc = new Scanner (System.in); while (true) {System.out.println ("Please enter what you want to do:"); System.out.println ("1. Login "); System.out.println (" 2. Open an account "); System.out.println (" you can enter commands: "); int command = sc.nextInt (); switch (command) {case 1: / / log in to break Case 2: / / account opening break; default: System.out.println ("the operation command you entered is not supported!") ;}
Summary
How does the system express the user's account information?
Define the account class Account, and define the attribute information that the system cares about.
What does the system use to store the account object information of all users?
ArrayList accounts = new ArrayList ()
Realization of user account opening function
Analysis.
The account opening function is actually to store the information of a new account object in the collection container of the system.
Steps to realize the account opening function
Define the method to complete account opening:
Public static void register (ArrayList accounts) {...}
Enter the name, secret and confirmation password on the keyboard (make sure the password is the same twice)
Generate account card number, card number must be automatically generated by the system 8 digits (must ensure that the card number is unique)
Create Account account class object user encapsulates account information (name, password, card number)
Save the Account account class object to the collection accounts.
Package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem {public static void main (String [] args) {/ / 1. Prepare the container object needed by the system. The user stores the account object ArrayList accounts = new ArrayList (); / / 2. Prepare the home page of the system, login, open an account showMain (accounts);} public static void showMain (ArrayList accounts) {System.out.println ("= Welcome to the home page ="); Scanner sc = new Scanner (System.in); while (true) {System.out.println ("Please enter what you want to do:"); System.out.println ("1. Login "); System.out.println (" 2. Open an account "); System.out.println (" you can enter commands: "); int command = sc.nextInt (); switch (command) {case 1: / / log in to break Case 2: / / account opening register (accounts,sc); break; default: System.out.println ("the operation command you entered is not supported!") ;} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I
< 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null;//查无此账户,说明卡号没有重复了! }} 总结 开户功能的实现需要哪几步操作,需要注意什么问题? 开户功能应该独立定义成方法,并传入当前集合对象给该方法。 录入开户信息(姓名,密码) 卡号要自动生成且唯一 把开户的信息封装成Account对象,存入到集合中去。 2.????用户登入,操作页展示,查询账户,退出账户用户登入功能实现 分析 定义方法: public static void login(ArrayListaccounts){...} 让用户键盘录入卡号,根据卡号查询账户对象。 如果没有找到了账户对象,说明卡号不存在,继续输入卡号 如果找到了账户对象,说明卡号存在,继续输入密码 如果密码不正确,提示继续输入密码 package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem{ public static void main(String[] args) {// 1.准备系统需要的容器对象,用户存储账户对象 ArrayList accounts = new ArrayList();// 2.准备系统的首页,登入,开户 showMain(accounts); } public static void showMain(ArrayList accounts) { System.out.println("=============欢迎进入首页==========="); Scanner sc = new Scanner(System.in); while(true) { System.out.println("请您输入您想做的操作:"); System.out.println("1.登录"); System.out.println("2.开户"); System.out.println("您可以输入命令了:"); int command = sc.nextInt(); switch(command) { case 1: //登录 login(accounts,sc); break; case 2: //开户 register(accounts,sc); break; default: System.out.println("您当前输入的操作命令不被支持!"); } } } /** * 完成用户登录 * @param accounts */ private static void login(ArrayList accounts,Scanner sc) { //必须系统中存在账户才可以登录 if (accounts.size() == 0) { //没有任何账户 System.out.println("当前系统中无任何账户,您需要先注册!"); return;//直接结束方法的执行! } //2.让用户键盘录入卡号,根据卡号查询账户对象 while (true) { System.out.println("请您输入登录的卡号:"); String cardId = sc.next(); //根据卡号查询账户对象 Account acc = getAccountBycardId(cardId,accounts);// 3.判断账户对象是否存在,存在说明卡号没问题 if (acc != null) { while (true) {// 4.让用户继续输入密码 System.out.println("请您输入登录的密码:"); String password = sc.next();// 5.判断密码是否正确 if (acc.getPassWord().equals(password)) { //密码正确,登入成功 //展示系统登录后的操作界面 System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());// } else { System.out.println("您的密码有误,请确认!"); } } } else { System.out.println("对不起,不存在该卡号的账户!"); } } } /** * 用户开户功能 * @param accounts 账户的集合对象 */ private static void register(ArrayList accounts, Scanner sc) { System.out.println("=========用户开户功能=========="); //键盘录入 姓名 密码 确认密码 System.out.println("请您输入开户名称:"); String name = sc.next(); String password = ""; while (true) { System.out.println("请您输入开户密码:"); password = sc.next(); System.out.println("请您输入确认密码:"); String okPassword = sc.next();// 判断两次输入的密码是否一致 if (okPassword.equals(password)) //字符串比较用equals { break; } else { System.out.println("两次密码必须一致~~~"); } } System.out.println("请您输入当次限额:"); double quotaMoney = sc.nextDouble();// 3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。 String cardId = creatCardId(accounts);// 4.创建一个账户对象封装账户的信息// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account(cardId,name,password,quotaMoney);// 5.把账户对象添加到集合中去 accounts.add(account); System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管"); } public static String creatCardId(ArrayList accouts) { while (true) {// 生成8位随机的数字代表卡号 String cardId = ""; Random r = new Random(); for (int i = 0; i < 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null;//查无此账户,说明卡号没有重复了! }}Summary
How is the login function implemented?
Query the corresponding account object in the collection according to the card number.
If the account object is found, it means that the card number exists. Continue to enter the password.
Login is successful if the password is correct
User operation page design, query account, exit account function
User operation page, query account, exit account function analysis
After the user logs in successfully, you need to go to the user actions page,
Query is to directly display the information of the account object that has successfully logged in.
To exit the account, you need to go back to the home page.
Package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem {public static void main (String [] args) {/ / 1. Prepare the container object needed by the system. The user stores the account object ArrayList accounts = new ArrayList (); / / 2. Prepare the home page of the system, login, account opening showMain (accounts);} public static void showMain (ArrayList accounts) / / meaning of showMain account opening home page / / ArrayList accounts usage definition function passed into the container accounts is passed parameter {System.out.println ("= Welcome to the home page ="); Scanner sc = new Scanner (System.in) While (true) {System.out.println ("Please enter the action you want to do:"); System.out.println ("1. Login "); System.out.println (" 2. Open an account "); System.out.println (" you can enter the command: "); int command = sc.nextInt (); switch (command) {case 1: / / login login (accounts,sc); break Case 2: / / account opening register (accounts,sc); break; default: System.out.println ("the operation command you entered is not supported!") Complete user login * @ param accounts * / private static void login (ArrayList accounts) Scanner sc) {/ / there must be an account in the system before you can log in to if (accounts.size () = = 0) {/ / there are no accounts System.out.println ("there are no accounts in the current system You need to register first! ") Return;// directly ends the execution of the method } / / 2. Let the user input the card number and query the account object while (true) {System.out.println ("Please enter the login card number:"); String cardId = sc.next (); / / query the account object Account acc = getAccountBycardId (cardId,accounts) according to the card number; / / 3. To determine whether the account object exists, it means that the card number is fine. If (acc! = null) {while (true) {/ / 4. Let the user continue to enter the password System.out.println ("Please enter the login password:"); String password = sc.next (); / / 5. Determine whether the password is correct if (acc.getPassWord (). Equals (password)) {/ / password is correct, login is successful / / display the operating interface System.out.println after login ("Congratulations "+ acc.getUserWord () +", successfully log in to the system. Your card number is "+ acc.getCardId ()." / / display the operation page showUserCommand (sc,acc); return;// continues to end the login method} else {System.out.println ("your password is incorrect, please confirm!") ;} else {System.out.println ("Sorry, there is no account for this card number!") ;}} private static void showUserCommand (Scanner sc,Account acc) {System.out.println ("= user action page ="); System.out.println ("1. Query account "); System.out.println (" 2. Deposit "); System.out.println (" 3. Withdraw money "); System.out.println (" 4. Transfer money "); System.out.println (" 5. Change password "); System.out.println (" 6. Quit "); System.out.println (" 7. Cancel the account "); while (true) {System.out.println (" Please enter the operation command: "); int command = sc.nextInt (); switch (command) {case 1: / / query account showAccount (acc); break Case 2: / / Deposit break; case 3: / / withdrawal break; case 4: / / transfer break Case 5: / / change password break; case 6: / / exit System.out.println ("Welcome next time!") ; return; / / the method to end the current showUserCommand (Scanner sc,Account acc) case 7: / / cancel the account break; default: System.out.println ("you typed it incorrectly!") ;} private static void showAccount (Account acc) {System.out.println ("= current account details ="); System.out.println ("Card number" + acc.getCardId ()); System.out.println ("name" + acc.getUserWord ()); System.out.println ("balance" + acc.getMoney ()) System.out.println ("current quota:" + acc.getQuotaMoney ());} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I
< 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null;//查无此账户,说明卡号没有重复了! }} < 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null; //查无此账户,说明卡号没有重复了! }} 总结 存款分析 存款就是拿到当前账户对象 然后让用户输入存款的金额 调用账户对象的setMoney方法将账户余额修改成存钱后的余额 存钱后需要查询一下账户信息,确认是否存钱成功了! 取款分析 取款需要先判断账户是否有钱 有钱则拿到自己账户对象 然后让用户输入取款金额 判断取款金额是否超过了当次限额,以及金额是否足够 满足要求则调用账户对象的setMoney方法完成金额的修改 package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem { public static void main(String[] args) {// 1.准备系统需要的容器对象,用户存储账户对象 ArrayList accounts = new ArrayList();// 2.准备系统的首页,登入,开户 showMain(accounts); } public static void showMain(ArrayList accounts) //showMain 开户首页的意思// ArrayList accounts 使用方法定义功能传入容器中 accounts是传参 { System.out.println("=============欢迎进入首页==========="); Scanner sc = new Scanner(System.in); while(true) { System.out.println("请您输入您想做的操作:"); System.out.println("1.登录"); System.out.println("2.开户"); System.out.println("您可以输入命令了:"); int command = sc.nextInt(); switch(command) { case 1: //登录 login(accounts,sc); break; case 2: //开户 register(accounts,sc); break; default: System.out.println("您当前输入的操作命令不被支持!"); } } } /** * 完成用户登录 * @param accounts */ private static void login(ArrayList accounts,Scanner sc) { //必须系统中存在账户才可以登录 if (accounts.size() == 0) { //没有任何账户 System.out.println("当前系统中无任何账户,您需要先注册!"); return;//直接结束方法的执行! } //2.让用户键盘录入卡号,根据卡号查询账户对象 while (true) { System.out.println("请您输入登录的卡号:"); String cardId = sc.next(); //根据卡号查询账户对象 Account acc = getAccountBycardId(cardId,accounts);// 3.判断账户对象是否存在,存在说明卡号没问题 if (acc != null) { while (true) {// 4.让用户继续输入密码 System.out.println("请您输入登录的密码:"); String password = sc.next();// 5.判断密码是否正确 if (acc.getPassWord().equals(password)) { //密码正确,登入成功 //展示系统登录后的操作界面 System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId()); //展示操作页面 showUserCommand(sc,acc); return;//继续结束登录方法 } else { System.out.println("您的密码有误,请确认!"); } } } else { System.out.println("对不起,不存在该卡号的账户!"); } } } private static void showUserCommand(Scanner sc,Account acc) { 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("请您输入操作命令:"); int command = sc.nextInt(); switch (command) { case 1: //查询账户 showAccount(acc); break; case 2: //存款 depositMoney(acc,sc); break; case 3: //取款 drawMoney(acc,sc); break; case 4: //转账 break; case 5: //修改密码 break; case 6: //退出 System.out.println("欢迎下次光临!!"); return; //结束当前showUserCommand(Scanner sc,Account acc)的方法 case 7: //注销账户 break; default: System.out.println("您输入有误!"); } } } /** * 取款操作 * @param acc * @param sc */ private static void drawMoney(Account acc, Scanner sc) { System.out.println("==========取款操作========="); //1.判断它的账户是否足够100元 if (acc.getMoney() >) {while (true) {System.out.println ("Please enter the amount of withdrawal:"); double money = sc.nextDouble (); / / 2. Judge whether the whole amount exceeds the current limit if (money > acc.getQuotaMoney ()) {System.out.println ("if you withdraw more than the current limit, do not withdraw so much, you can take it at most:" + acc.getQuotaMoney ()). } else {/ / 3. Judge whether the current balance is enough for you to withdraw money if (acc.getMoney () > = money) {/ / enough, you can withdraw money acc.setMoney (acc.getMoney ()-money); System.out.println ("Congratulations, withdraw money" + money + "succeeded! There is still a balance in the current account: "+ acc.getMoney (); return;// withdrew money and killed the withdrawal method} else {System.out.println (" insufficient balance! ") ;} else {System.out.println ("your own amount is not more than 100RMB, it's time to work hard") * @ param acc * / private static void depositMoney (Account acc,Scanner sc) {System.out.println ("= savings operation ="); System.out.println ("Please enter the amount of deposit:"); double money = sc.nextDouble () / / directly modify the amount to the money attribute of the account object to acc.setMoney (acc.getMoney () + money); System.out.println ("deposit complete!") ; showAccount (acc);} private static void showAccount (Account acc) {System.out.println ("= current account details ="); System.out.println ("Card number" + acc.getCardId ()); System.out.println ("name" + acc.getUserWord ()); System.out.println ("balance" + acc.getMoney ()) System.out.println ("current quota:" + acc.getQuotaMoney ());} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I
< 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null; //查无此账户,说明卡号没有重复了! }} 总结温习 取款需要先判断账户是否有钱 有钱则拿到自己账户对象 然后让用户输入取款金额 判断取款金额是否超过了当次限额,以及金额是否足够 满足要求则调用账户对象的setMoney方法完成金额的修改 4.????用户转账,修改密码,销户用户转账功能 分析 转账功能需要判断系统中是否有2个账户对象及以上 同时还要判断总结账户是否有钱 接下来需要输入对方卡号,判断对方账户是否存在 对方账户存在还需要认证对方户主的姓氏 满足要求则可以把自己账户对象的金额修改到对方账户对象中去 package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem { public static void main(String[] args) {// 1.准备系统需要的容器对象,用户存储账户对象 ArrayList accounts = new ArrayList();// 2.准备系统的首页,登入,开户 showMain(accounts); } public static void showMain(ArrayList accounts) //showMain 开户首页的意思// ArrayList accounts 使用方法定义功能传入容器中 accounts是传参 { System.out.println("=============欢迎进入首页==========="); Scanner sc = new Scanner(System.in); while(true) { System.out.println("请您输入您想做的操作:"); System.out.println("1.登录"); System.out.println("2.开户"); System.out.println("您可以输入命令了:"); int command = sc.nextInt(); switch(command) { case 1: //登录 login(accounts,sc); break; case 2: //开户 register(accounts,sc); break; default: System.out.println("您当前输入的操作命令不被支持!"); } } } /** * 完成用户登录 * @param accounts */ private static void login(ArrayList accounts,Scanner sc) { //必须系统中存在账户才可以登录 if (accounts.size() == 0) { //没有任何账户 System.out.println("当前系统中无任何账户,您需要先注册!"); return;//直接结束方法的执行! } //2.让用户键盘录入卡号,根据卡号查询账户对象 while (true) { System.out.println("请您输入登录的卡号:"); String cardId = sc.next(); //根据卡号查询账户对象 Account acc = getAccountBycardId(cardId,accounts);// 3.判断账户对象是否存在,存在说明卡号没问题 if (acc != null) { while (true) {// 4.让用户继续输入密码 System.out.println("请您输入登录的密码:"); String password = sc.next();// 5.判断密码是否正确 if (acc.getPassWord().equals(password)) { //密码正确,登入成功 //展示系统登录后的操作界面 System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId()); //展示操作页面 showUserCommand(sc,acc,accounts); return;//继续结束登录方法 } else { System.out.println("您的密码有误,请确认!"); } } } else { System.out.println("对不起,不存在该卡号的账户!"); } } } private static void showUserCommand(Scanner sc,Account acc,ArrayList accounts) { 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("请您输入操作命令:"); int command = sc.nextInt(); switch (command) { case 1: //查询账户 showAccount(acc); break; case 2: //存款 depositMoney(acc,sc); break; case 3: //取款 drawMoney(acc,sc); break; case 4: //转账 transferMoney(accounts,acc ,sc); break; case 5: //修改密码 break; case 6: //退出 System.out.println("欢迎下次光临!!"); return; //结束当前showUserCommand(Scanner sc,Account acc)的方法 case 7: //注销账户 break; default: System.out.println("您输入有误!"); } } } /** * 转账功能 * @param accounts * @param acc * @param sc */ private static void transferMoney(ArrayList accounts, Account acc, Scanner sc) { //1.判断系统中是否有2个账户及以上 if (accounts.size() < 2) { System.out.println("对不起,系统中无其他账户,您不可以转账!!"); return; } //2.判断自己的账户对象中是否有钱 if (acc.getMoney() == 0) { System.out.println("对不起,您自己都快吃土了,就别装逼了!!"); return; } //3.开始转账逻辑 while (true) { System.out.println("请您输入对方账户的卡号:"); String cardId = sc.next(); Account account = getAccountBycardId(cardId,accounts); //判断整个账户对象是否存在,存在说明对方卡号输入正确 if (account != null) { //判断这个账户对象是否是当前自己登录的账户 if (account.getCardId().equals(acc.getCardId())) { //也就是这里企图想给自己转账 System.out.println("您不能给自己转账!"); } else { //确认对方的姓氏 String name = "*" + account.getUserWord().substring(1); System.out.println("请您确认【" + name + "】的姓氏:"); String preName = sc.next(); //判断 if (account.getUserWord().startsWith(preName)) { //真正的转账才刚刚开始 System.out.println("请您输入转账的金额:"); double money = sc.nextDouble(); //判断这个金额是否超过了自己的金额 if (money >Acc.getMoney () {System.out.println ("Sorry, you want to transfer too much money, you can transfer money at most:" + acc.getMoney ()) } else {/ / started acc.setMoney (acc.getMoney ()-money); account.setMoney (account.getMoney () + money) System.out.println ("Congratulations, the transfer is successful. It has been transferred for" + account.getUserWord () + ":" + money); showAccount (acc); return. }} else {System.out.println ("Sorry, the information you certified is incorrect ~ ~") } else {System.out.println ("Sorry, there is something wrong with the transfer card number you entered!") ;} / * * withdrawal operation * @ param acc * @ param sc * / private static void drawMoney (Account acc, Scanner sc) {System.out.println ("= withdrawal operation ="); / / 1. Judge whether its account is sufficient for 100RMB if (acc.getMoney () > = 100RMB) {while (true) {System.out.println ("Please enter the amount of withdrawal:"); double money = sc.nextDouble (); / / 2. Judge whether the whole amount exceeds the current limit if (money > acc.getQuotaMoney ()) {System.out.println ("if you withdraw more than the current limit, do not withdraw so much, you can take it at most:" + acc.getQuotaMoney ()). } else {/ / 3. Judge whether the current balance is enough for you to withdraw money if (acc.getMoney () > = money) {/ / enough, you can withdraw money acc.setMoney (acc.getMoney ()-money); System.out.println ("Congratulations, withdraw money" + money + "succeeded! There is still a balance in the current account: "+ acc.getMoney (); return;// withdrew money and killed the withdrawal method} else {System.out.println (" insufficient balance! ") ;} else {System.out.println ("your own amount is not more than 100RMB, it's time to work hard") * @ param acc * / private static void depositMoney (Account acc,Scanner sc) {System.out.println ("= savings operation ="); System.out.println ("Please enter the amount of deposit:"); double money = sc.nextDouble () / / directly modify the amount to the money attribute of the account object to acc.setMoney (acc.getMoney () + money); System.out.println ("deposit complete!") ; showAccount (acc);} private static void showAccount (Account acc) {System.out.println ("= current account details ="); System.out.println ("Card number" + acc.getCardId ()); System.out.println ("name" + acc.getUserWord ()); System.out.println ("balance" + acc.getMoney ()) System.out.println ("current quota:" + acc.getQuotaMoney ());} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I
< 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null; //查无此账户,说明卡号没有重复了! }} 总结温习 转账功能需要判断系统中是否有2个账户对象及以上 同时还要判断总结账户是否有钱 接下来需要输入对方卡号,判断对方账户是否存在 对方账户存在还需要认证对方户主的姓氏 满足要求则可以把自己账户对象的金额修改到对方账户对象中去 修改密码与销户 分析 修改密码就是把当前对现象的密码属性使用set方法进行更新 销户是从集合对象中删除当前对象,并回到首页 这里为止所有的ATM系统的操作代码就已经完成 package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem { public static void main(String[] args) {// 1.准备系统需要的容器对象,用户存储账户对象 ArrayList accounts = new ArrayList();// 2.准备系统的首页,登入,开户 showMain(accounts); } public static void showMain(ArrayList accounts) //showMain 开户首页的意思// ArrayList accounts 使用方法定义功能传入容器中 accounts是传参 { System.out.println("=============欢迎进入首页==========="); Scanner sc = new Scanner(System.in); while(true) { System.out.println("请您输入您想做的操作:"); System.out.println("1.登录"); System.out.println("2.开户"); System.out.println("您可以输入命令了:"); int command = sc.nextInt(); switch(command) { case 1: //登录 login(accounts,sc); break; case 2: //开户 register(accounts,sc); break; default: System.out.println("您当前输入的操作命令不被支持!"); } } } /** * 完成用户登录 * @param accounts */ private static void login(ArrayList accounts,Scanner sc) { //必须系统中存在账户才可以登录 if (accounts.size() == 0) { //没有任何账户 System.out.println("当前系统中无任何账户,您需要先注册!"); return;//直接结束方法的执行! } //2.让用户键盘录入卡号,根据卡号查询账户对象 while (true) { System.out.println("请您输入登录的卡号:"); String cardId = sc.next(); //根据卡号查询账户对象 Account acc = getAccountBycardId(cardId,accounts);// 3.判断账户对象是否存在,存在说明卡号没问题 if (acc != null) { while (true) {// 4.让用户继续输入密码 System.out.println("请您输入登录的密码:"); String password = sc.next();// 5.判断密码是否正确 if (acc.getPassWord().equals(password)) { //密码正确,登入成功 //展示系统登录后的操作界面 System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId()); //展示操作页面 showUserCommand(sc,acc,accounts); return;//继续结束登录方法 } else { System.out.println("您的密码有误,请确认!"); } } } else { System.out.println("对不起,不存在该卡号的账户!"); } } } private static void showUserCommand(Scanner sc,Account acc,ArrayList accounts) { 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("请您输入操作命令:"); int command = sc.nextInt(); switch (command) { case 1: //查询账户 showAccount(acc); break; case 2: //存款 depositMoney(acc,sc); break; case 3: //取款 drawMoney(acc,sc); break; case 4: //转账 transferMoney(accounts,acc ,sc); break; case 5: //修改密码 updataPassWord(acc,sc); return;//结束当前………… case 6: //退出 System.out.println("欢迎下次光临!!"); return; //结束当前showUserCommand(Scanner sc,Account acc)的方法 case 7: //注销账户 //从当前集合中抹掉当前账户对象即可 accounts.remove(acc); System.out.println("销户成功了!!"); return; default: System.out.println("您输入有误!"); } } } /** * 修改密码 * @param acc */ private static void updataPassWord(Account acc,Scanner sc) { System.out.println("===========修改密码========="); while (true) { System.out.println("请您输入正确的密码:"); String okPassWord = sc.next(); //判断密码是否正确 if (acc.getPassWord().equals(okPassWord)) { //可以输入新密码 System.out.println("请您输入新的密码:"); String newPassWord = sc.next(); System.out.println("请您输入确认密码:"); String okNewPassWord = sc.next(); if (newPassWord.equals(okNewPassWord)) { //修改账户对象的密码为新密码 acc.setPassWord(newPassWord); return;//直接结束方法! } else { System.out.println("您两次输入的密码不一致~~"); } } else { System.out.println("当前输入的密码不正确~~~"); } } } /** * 转账功能 * @param accounts * @param acc * @param sc */ private static void transferMoney(ArrayList accounts, Account acc, Scanner sc) { //1.判断系统中是否有2个账户及以上 if (accounts.size() < 2) { System.out.println("对不起,系统中无其他账户,您不可以转账!!"); return; } //2.判断自己的账户对象中是否有钱 if (acc.getMoney() == 0) { System.out.println("对不起,您自己都快吃土了,就别装逼了!!"); return; } //3.开始转账逻辑 while (true) { System.out.println("请您输入对方账户的卡号:"); String cardId = sc.next(); Account account = getAccountBycardId(cardId,accounts); //判断整个账户对象是否存在,存在说明对方卡号输入正确 if (account != null) { //判断这个账户对象是否是当前自己登录的账户 if (account.getCardId().equals(acc.getCardId())) { //也就是这里企图想给自己转账 System.out.println("您不能给自己转账!"); } else { //确认对方的姓氏 String name = "*" + account.getUserWord().substring(1); System.out.println("请您确认【" + name + "】的姓氏:"); String preName = sc.next(); //判断 if (account.getUserWord().startsWith(preName)) { //真正的转账才刚刚开始 System.out.println("请您输入转账的金额:"); double money = sc.nextDouble(); //判断这个金额是否超过了自己的金额 if (money >Acc.getMoney () {System.out.println ("Sorry, you want to transfer too much money, you can transfer money at most:" + acc.getMoney ()) } else {/ / started acc.setMoney (acc.getMoney ()-money); account.setMoney (account.getMoney () + money) System.out.println ("Congratulations, the transfer is successful. It has been transferred for" + account.getUserWord () + ":" + money); showAccount (acc); return. }} else {System.out.println ("Sorry, the information you certified is incorrect ~ ~") } else {System.out.println ("Sorry, there is something wrong with the transfer card number you entered!") ;} / * * withdrawal operation * @ param acc * @ param sc * / private static void drawMoney (Account acc, Scanner sc) {System.out.println ("= withdrawal operation ="); / / 1. Judge whether its account is sufficient for 100RMB if (acc.getMoney () > = 100RMB) {while (true) {System.out.println ("Please enter the amount of withdrawal:"); double money = sc.nextDouble (); / / 2. Judge whether the whole amount exceeds the current limit if (money > acc.getQuotaMoney ()) {System.out.println ("if you withdraw more than the current limit, do not withdraw so much, you can take it at most:" + acc.getQuotaMoney ()). } else {/ / 3. Judge whether the current balance is enough for you to withdraw money if (acc.getMoney () > = money) {/ / enough, you can withdraw money acc.setMoney (acc.getMoney ()-money); System.out.println ("Congratulations, withdraw money" + money + "succeeded! There is still a balance in the current account: "+ acc.getMoney (); return;// withdrew money and killed the withdrawal method} else {System.out.println (" insufficient balance! ") ;} else {System.out.println ("your own amount is not more than 100RMB, it's time to work hard") * @ param acc * / private static void depositMoney (Account acc,Scanner sc) {System.out.println ("= savings operation ="); System.out.println ("Please enter the amount of deposit:"); double money = sc.nextDouble () / / directly modify the amount to the money attribute of the account object to acc.setMoney (acc.getMoney () + money); System.out.println ("deposit complete!") ; showAccount (acc);} private static void showAccount (Account acc) {System.out.println ("= current account details ="); System.out.println ("Card number" + acc.getCardId ()); System.out.println ("name" + acc.getUserWord ()); System.out.println ("balance" + acc.getMoney ()) System.out.println ("current quota:" + acc.getQuotaMoney ());} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I
< 8; i++) { cardId += r.nextInt(10); }// 判断卡号是否重复了 Account acc = getAccountBycardId(cardId,accouts); if (acc == null) { // 说明当前卡号没有重复 return cardId; } } } public static Account getAccountBycardId(String cardId, ArrayList accounts) {// 根据卡号查询对象 for (int i = 0; i < accounts.size(); i++) { Account acc = accounts.get(i); if (acc.getCardId().equals(cardId)) { return acc; } } return null; //查无此账户,说明卡号没有重复了! }} 5. ????源代码在这里这里拿package com.wangxinhua;/** 账户类 */public class Account { private String CardId;//卡号 private String UserWord;//客户名称 private String PassWord;//密码 private double Money;//余额 private double QuotaMoney;//当次取现限额//无参函数 public Account() { }// 构造好了有参函数,那么就会有无参函数// 有参函数 public Account(String cardId, String userWord, String passWord, double quotaMoney) { CardId = cardId; UserWord = userWord; PassWord = passWord; QuotaMoney = quotaMoney; } public String getCardId() { return CardId; } public void setCardId(String cardId) { CardId = cardId; } public String getUserWord() { return UserWord; } public void setUserWord(String userWord) { UserWord = userWord; } public String getPassWord() { return PassWord; } public void setPassWord(String passWord) { PassWord = passWord; } public double getMoney() { return Money; } public void setMoney(double money) { Money = money; } public double getQuotaMoney() { return QuotaMoney; } public void setQuotaMoney(double quotaMoney) { QuotaMoney = quotaMoney; }} 这里是第一个类用于构造函数 下面这个是第二个类 package com.wangxinhua;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class ATMSystem { public static void main(String[] args) {// 1.准备系统需要的容器对象,用户存储账户对象 ArrayList accounts = new ArrayList();// 2.准备系统的首页,登入,开户 showMain(accounts); } public static void showMain(ArrayList accounts) //showMain 开户首页的意思// ArrayList accounts 使用方法定义功能传入容器中 accounts是传参 { System.out.println("=============欢迎进入首页==========="); Scanner sc = new Scanner(System.in); while(true) { System.out.println("请您输入您想做的操作:"); System.out.println("1.登录"); System.out.println("2.开户"); System.out.println("您可以输入命令了:"); int command = sc.nextInt(); switch(command) { case 1: //登录 login(accounts,sc); break; case 2: //开户 register(accounts,sc); break; default: System.out.println("您当前输入的操作命令不被支持!"); } } } /** * 完成用户登录 * @param accounts */ private static void login(ArrayList accounts,Scanner sc) { //必须系统中存在账户才可以登录 if (accounts.size() == 0) { //没有任何账户 System.out.println("当前系统中无任何账户,您需要先注册!"); return;//直接结束方法的执行! } //2.让用户键盘录入卡号,根据卡号查询账户对象 while (true) { System.out.println("请您输入登录的卡号:"); String cardId = sc.next(); //根据卡号查询账户对象 Account acc = getAccountBycardId(cardId,accounts);// 3.判断账户对象是否存在,存在说明卡号没问题 if (acc != null) { while (true) {// 4.让用户继续输入密码 System.out.println("请您输入登录的密码:"); String password = sc.next();// 5.判断密码是否正确 if (acc.getPassWord().equals(password)) { //密码正确,登入成功 //展示系统登录后的操作界面 System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId()); //展示操作页面 showUserCommand(sc,acc,accounts); return;//继续结束登录方法 } else { System.out.println("您的密码有误,请确认!"); } } } else { System.out.println("对不起,不存在该卡号的账户!"); } } } private static void showUserCommand(Scanner sc,Account acc,ArrayList accounts) { 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("请您输入操作命令:"); int command = sc.nextInt(); switch (command) { case 1: //查询账户 showAccount(acc); break; case 2: //存款 depositMoney(acc,sc); break; case 3: //取款 drawMoney(acc,sc); break; case 4: //转账 transferMoney(accounts,acc ,sc); break; case 5: //修改密码 updataPassWord(acc,sc); return;//结束当前………… case 6: //退出 System.out.println("欢迎下次光临!!"); return; //结束当前showUserCommand(Scanner sc,Account acc)的方法 case 7: //注销账户 //从当前集合中抹掉当前账户对象即可 accounts.remove(acc); System.out.println("销户成功了!!"); return; default: System.out.println("您输入有误!"); } } } /** * 修改密码 * @param acc */ private static void updataPassWord(Account acc,Scanner sc) { System.out.println("===========修改密码========="); while (true) { System.out.println("请您输入正确的密码:"); String okPassWord = sc.next(); //判断密码是否正确 if (acc.getPassWord().equals(okPassWord)) { //可以输入新密码 System.out.println("请您输入新的密码:"); String newPassWord = sc.next(); System.out.println("请您输入确认密码:"); String okNewPassWord = sc.next(); if (newPassWord.equals(okNewPassWord)) { //修改账户对象的密码为新密码 acc.setPassWord(newPassWord); return;//直接结束方法! } else { System.out.println("您两次输入的密码不一致~~"); } } else { System.out.println("当前输入的密码不正确~~~"); } } } /** * 转账功能 * @param accounts * @param acc * @param sc */ private static void transferMoney(ArrayList accounts, Account acc, Scanner sc) { //1.判断系统中是否有2个账户及以上 if (accounts.size() < 2) { System.out.println("对不起,系统中无其他账户,您不可以转账!!"); return; } //2.判断自己的账户对象中是否有钱 if (acc.getMoney() == 0) { System.out.println("对不起,您自己都快吃土了,就别装逼了!!"); return; } //3.开始转账逻辑 while (true) { System.out.println("请您输入对方账户的卡号:"); String cardId = sc.next(); Account account = getAccountBycardId(cardId,accounts); //判断整个账户对象是否存在,存在说明对方卡号输入正确 if (account != null) { //判断这个账户对象是否是当前自己登录的账户 if (account.getCardId().equals(acc.getCardId())) { //也就是这里企图想给自己转账 System.out.println("您不能给自己转账!"); } else { //确认对方的姓氏 String name = "*" + account.getUserWord().substring(1); System.out.println("请您确认【" + name + "】的姓氏:"); String preName = sc.next(); //判断 if (account.getUserWord().startsWith(preName)) { //真正的转账才刚刚开始 System.out.println("请您输入转账的金额:"); double money = sc.nextDouble(); //判断这个金额是否超过了自己的金额 if (money >Acc.getMoney () {System.out.println ("Sorry, you want to transfer too much money, you can transfer money at most:" + acc.getMoney ()) } else {/ / started acc.setMoney (acc.getMoney ()-money); account.setMoney (account.getMoney () + money) System.out.println ("Congratulations, the transfer is successful. It has been transferred for" + account.getUserWord () + ":" + money); showAccount (acc); return. }} else {System.out.println ("Sorry, the information you certified is incorrect ~ ~") } else {System.out.println ("Sorry, there is something wrong with the transfer card number you entered!") ;} / * * withdrawal operation * @ param acc * @ param sc * / private static void drawMoney (Account acc, Scanner sc) {System.out.println ("= withdrawal operation ="); / / 1. Judge whether its account is sufficient for 100RMB if (acc.getMoney () > = 100RMB) {while (true) {System.out.println ("Please enter the amount of withdrawal:"); double money = sc.nextDouble (); / / 2. Judge whether the whole amount exceeds the current limit if (money > acc.getQuotaMoney ()) {System.out.println ("if you withdraw more than the current limit, do not withdraw so much, you can take it at most:" + acc.getQuotaMoney ()). } else {/ / 3. Judge whether the current balance is enough for you to withdraw money if (acc.getMoney () > = money) {/ / enough, you can withdraw money acc.setMoney (acc.getMoney ()-money); System.out.println ("Congratulations, withdraw money" + money + "succeeded! There is still a balance in the current account: "+ acc.getMoney (); return;// withdrew money and killed the withdrawal method} else {System.out.println (" insufficient balance! ") ;} else {System.out.println ("your own amount is not more than 100RMB, it's time to work hard") * @ param acc * / private static void depositMoney (Account acc,Scanner sc) {System.out.println ("= savings operation ="); System.out.println ("Please enter the amount of deposit:"); double money = sc.nextDouble () / / directly modify the amount to the money attribute of the account object to acc.setMoney (acc.getMoney () + money); System.out.println ("deposit complete!") ; showAccount (acc);} private static void showAccount (Account acc) {System.out.println ("= current account details ="); System.out.println ("Card number" + acc.getCardId ()); System.out.println ("name" + acc.getUserWord ()); System.out.println ("balance" + acc.getMoney ()) System.out.println ("current quota:" + acc.getQuotaMoney ());} / * * user account opening function * @ param accounts account collection object * / private static void register (ArrayList accounts, Scanner sc) {System.out.println ("= user account opening function =") / / enter name password confirmation password System.out.println ("Please enter account name:"); String name = sc.next (); String password = ""; while (true) {System.out.println ("Please enter account opening password:"); password = sc.next () System.out.println ("Please enter the confirmation password:"); String okPassword = sc.next (); / / determine whether the passwords entered twice are the same if (okPassword.equals (password)) / / string comparison with equals {break } else {System.out.println ("two passwords must be the same ~ ~");} System.out.println ("Please enter the current quota:"); double quotaMoney = sc.nextDouble (); / / 3. Generate the card number of the account. The card number is 8 digits and cannot be duplicated with other account card numbers. String cardId = creatCardId (accounts) / / 4. Create an account object that encapsulates account information / / public Account (String cardId, String userWord, String passWord, double money, double quotaMoney) Account account = new Account (cardId,name,password,quotaMoney); / / 5. Add account objects to the collection to accounts.add (account); System.out.println ("Congratulations, you have successfully opened an account, your card number is" + account.getCardId () + ", please take good care of it");} public static String creatCardId (ArrayList accouts) {while (true) {/ / generate 8-digit random numbers to represent the card number String cardId = " Random r = new Random (); for (int I = 0; I < 8; iTunes +) {cardId + = r.nextInt (10);} / / determine whether the card number repeats Account acc = getAccountBycardId (cardId,accouts) If (acc = = null) {/ / indicates that the current card number is not repeated return cardId;} public static Account getAccountBycardId (String cardId, ArrayList accounts) {/ / query the object for according to the card number (int I = 0; I < accounts.size () Account acc +) {Account acc = accounts.get (I); if (acc.getCardId (). Equals (cardId)) {return acc;}} return null; / / does not have this account, indicating that the card number has not been duplicated! }}
The above is all the contents of the article "how to implement the ATM system in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.