我们可以用Java构建一个ATM程序来显示ATM交易。自动柜员机 (ATM) 或提款机(英式英语)是一种电子电信系统,允许银行公司的客户进行金融交易。用户必须从 ATM 应用程序屏幕上显示的选项中进行选择。例如,取款、存款、查看余额、退出可用选项。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试退出前要提取资金、存入资金以及查看账户余额,需要在ATM程序中执行以下操作:
下面给出的是 Java 中的 ATM 程序的示例:
Java ATM 程序取款、存款、查询余额示例。
代码:
package jex; import java.util.*; class ATM { public static void main( String args[] ) { //declare and initialize balance, withdraw, and deposit int balance = 50000; int withdraw, deposit; //create scanner class object to get choice of user Scanner sc = new Scanner(System.in); while(true) { System.out.println( "Welcome to ATM ... " ); System.out.println( "Select 1 for Withdraw" ); System.out.println( "Select 2 for Deposit" ); System.out.println( "Select 3 for Check Balance" ); System.out.println( "Select 4 for EXIT" ); System.out.print( "Select the appropriate options you want to perform:" ); //get the user selected option int op = sc.nextInt( ); switch( op ) { case 1: System.out.print( "Enter the amount to be withdrawn :" ); // accept the withdraw amount from the user withdraw = sc.nextInt(); //check whether the balance is greater than or equal to the withdrawal amount withdraw( balance, withdraw); break; case 2: System.out.print( "Enter the amount to be deposited :" ); //accept the deposit amount from the user deposit = sc.nextInt(); // call the function and add the deposit amount to the total balance deposit( balance, deposit ); break; case 3: // printing the total balance of the user printBalance( balance ); System.out.println(" "); break; case 4: // exit from the menu System.exit( 0 ); } } } // function to print the current balance in an account public static void printBalance(int balance) { System.out.println(" The Current Balance : " + balance); System.out.println(); } // The function to Withdraw an amount and update the balance public static int withdraw(int balance, int withdrawAmount) { System.out.println( "Withdrawn Operation :" ); System.out.println("The withdrawing Amount is : " + withdrawAmount); if (balance >= withdrawAmount) { balance = balance - withdrawAmount; System.out.println( "Please collect your money and remove the card" ); printBalance( balance ); } else { System.out.println( "Sorry! the balanace is insufficient." ); System.out.println( ); } return balance; } // The function to deposit an amount and update the balance public static int deposit(int balance, int depositAmount) { System.out.println( "Deposit Operation :" ); System.out.println(" The depositing amount is : " + depositAmount); balance = balance + depositAmount; System.out.println( "Your Money has been successfully deposited" ); printBalance(balance); return balance; } }
输出:
上述提现操作代码的输出为:
上述存款操作代码的输出是:
最后,上述存款操作代码的输出为:
如上面的程序,创建了 ATM 类,其中包含withdraw()、deposit() 和 printbalance() 函数。 withdraw()函数用于执行提现操作;此功能接受余额和取款金额。在withdraw()函数中,首先检查余额是否大于取款金额;如果为真,则通过从余额中减去提款金额来更新余额。接下来,函数deposit()用于执行存款操作;此功能接受余额和存款金额。
在deposit()函数中,它通过将存款金额添加到余额来更新余额。接下来,printbalance()函数用于打印余额;它接受余额。然后,在主函数中,创建一个整数的余额变量。接下来,根据案例要执行的特定选项选择,打印用于取款、存款、余额和退出操作的选择 Piton,如我们在上面的输出中看到的。
自动柜员机 (ATM) 是一种电子电信系统,允许银行公司的客户进行金融交易。我们可以用Java创建一个ATM程序来显示ATM交易,用户可以取款、存款、查看余额、退出ATM。
以上是Java ATM 程序的详细内容。更多信息请关注PHP中文网其他相关文章!