显示 ATM 交易的Java程序
让我们构建一个Java程序来表示 ATM 交易,其中用户必须从屏幕上显示的选项中选择输入。屏幕上的可用选项包括提款、存款、余额等操作。
以下是ATM中可用的基本操作
- 提取
- 订金
- 查看余额
- 出口
每个选项的方法
A. 退出:
- 将用户希望提取的金额作为输入。
- 如果余额金额大于或等于提款金额,则执行交易并给用户所需的金额。
- 否则打印资金不足消息。
B. 存款:
- 将用户希望存入的金额作为输入。
- 添加从用户接收到的输入以平衡和更新其值。
- 余额 = 余额 + 存款。
- 在屏幕上打印一条消息,说明存款交易已成功。
C. 检查余额:
- 在屏幕上打印一条消息,显示余额金额的值。
D、退出:
- 退出当前交易模式并将用户返回到主页或初始屏幕。
下面是上述方法的实现。
Java
// Java Program to Display the ATM Transaction
import java.io.*;
public class GFG {
// Display current balance in account
public static void displayBalance(int balance)
{
System.out.println("Current Balance : " + balance);
System.out.println();
}
// Withdraw amount and update the balance
public static int amountWithdrawing(int balance,
int withdrawAmount)
{
System.out.println("Withdrawn Operation:");
System.out.println("Withdrawing Amount : "
+ withdrawAmount);
if (balance >= withdrawAmount) {
balance = balance - withdrawAmount;
System.out.println(
"Please collect your money and collect the card");
displayBalance(balance);
}
else {
System.out.println("Sorry! Insufficient Funds");
System.out.println();
}
return balance;
}
// Deposit amount and update the balance
public static int amountDepositing(int balance,
int depositAmount)
{
System.out.println("Deposit Operation:");
System.out.println("Depositing Amount : "
+ depositAmount);
balance = balance + depositAmount;
System.out.println(
"Your Money has been successfully deposited");
displayBalance(balance);
return balance;
}
public static void main(String args[])
{
int balance = 10000;
int withdrawAmount = 5000;
int depositAmount = 2000;
// calling display balance
displayBalance(balance);
// withdrawing amount
balance
= amountWithdrawing(balance, withdrawAmount);
// depositing amount
balance = amountDepositing(balance, depositAmount);
}
}
输出
Current Balance : 10000
Withdrawn Operation:
Withdrawing Amount : 5000
Please collect your money and collect the card
Current Balance : 5000
Deposit Operation:
Depositing Amount : 2000
Your Money has been successfully deposited
Balance : 7000
时间复杂度: O(1)