我們可以用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中文網其他相關文章!