How to use the Java method to implement a simple ATM function
Editor to share with you how to use the Java method to achieve a simple ATM function, I hope you will learn something after reading this article, let's discuss it together!
The code is as follows:
Package com.atm;import java.util.Scanner;public class Atm {/ * * Note: cardNos is the card number, passwords is the corresponding password * userNames is the corresponding user name, moneys is the corresponding balance * time is used to limit the number of times to enter account and password * userIndex (subscript) 10 indicates login failure, which is used to determine the number of accounts entered in the array, so as to facilitate the following operation * / public Scanner scanner = new Scanner (System.in) Public String [] cardNos= {"123,456", "111l"}; public String [] passwords= {"11212,223,121"}; public String [] userNames= {"Xiao", "Xiao", "Zhang Xiao"}; public double [] moneys = {1000, 2000, 3000}; public int userIndex = 10; public int time=3 / * * main program * / public static void main (String [] args) {Atm atm = new Atm (); / * execute startup atm * / atm.atmRun ();} / * * launch atm * / public void atmRun () {while (time > 0) {System.out.println ("Please enter card number:") String cardNo = scanner.nextLine (); System.out.println ("Please enter password:"); String pwd = scanner.nextLine (); / * * execute login * / int userIndex= this.login (cardNo, pwd); / * * determine whether login is successful * / if (userIndexExecutable 10) {/ * execute function menu * / this.showMenu () } else {time -; System.out.println ("the account number or password you entered is incorrect"); System.out.println ("you still have" + time+ "second chance");} / * * number of times to exit the system * / if (time==0) {System.exit (0) } / * @ param cardNo card number * @ param pwd password * @ return Integer userIndex userIndex:10 indicates login failed otherwise it is successful * / public int login (String cardNo,String pwd) {/ * * cycle to determine whether the account exists * / for (int item0) Imoneys [userIndex]) {System.out.println ("Kiss you don't have enough balance!") ; System.out.println ("your balance is:" + moneys [userIndex]);} else {moneys [userIndex]-= getMoney; System.out.println ("Congratulations on your successful withdrawal" + getMoney+ "your balance is:" + moneys [userIndex]);}} / * * Deposit function * / public void savaMoney () {System.out.println ("Please enter your deposit amount:"); int savaMoney=scanner.nextInt () Moneys [userIndex] + = savaMoney; System.out.println ("Congratulations on your successful deposit" + savaMoney+ "your balance is" + moneys [userIndex]);} / * * transfer function * / public void transMoneys () {/ * * Note: hasUser is used to determine whether the transfer user exists. If 10, there is no subscript * / int hasUser=10; int transIndex=0 used by * transIndex to obtain the transfer account. System.out.println ("Please enter the account number of the transfer:"); String transCard=scanner.next (); for (int iTuno ActImoneys [userIndex]) {System.out.println ("transfer failed!") ; System.out.println ("cause of failure: the amount transferred is larger than the balance, your balance is:" + moneys [userIndex]);} else {transCard+=transMoney; Moneys [userIndex]-= transMoney; System.out.println ("successful: + userNames [transIndex] +" transfer: "+ transMoney); System.out.println (" your remaining amount is: "+ moneys [userIndex]) After reading this article, I believe you have some understanding of "how to use Java to achieve a simple ATM function". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!