首页 > Java > java教程 > 正文

Java ATM 程序

WBOY
发布: 2024-08-30 15:14:38
原创
647 人浏览过

我们可以用Java构建一个ATM程序来显示ATM交易。自动柜员机 (ATM) 或提款机(英式英语)是一种电子电信系统,允许银行公司的客户进行金融交易。用户必须从 ATM 应用程序屏幕上显示的选项中进行选择。例如,取款、存款、查看余额、退出可用选项。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

Java 中 ATM 程序的工作

退出前要提取资金、存入资金以及查看账户余额,需要在ATM程序中执行以下操作:

  • 提款:用于提款,从用户处获取提款金额,从总余额中扣除,并显示消息。
  • 存款:用于存入资金,从用户处获取存款金额进行添加,将其添加到总余额中,并显示消息。
  • 查看余额:查看余额时,显示用户的总余额。
  • 退出:通过退出当前交易模式将用户返回到主页或初始屏幕。

Java 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;
}
}
登录后复制

输出:

上述提现操作代码的输出为:

Java ATM 程序

上述存款操作代码的输出是:

Java ATM 程序

最后,上述存款操作代码的输出为:

Java ATM 程序

如上面的程序,创建了 ATM 类,其中包含withdraw()、deposit() 和 printbalance() 函数。 withdraw()函数用于执行提现操作;此功能接受余额和取款金额。在withdraw()函数中,首先检查余额是否大于取款金额;如果为真,则通过从余额中减去提款金额来更新余额。接下来,函数deposit()用于执行存款操作;此功能接受余额和存款金额。

在deposit()函数中,它通过将存款金额添加到余额来更新余额。接下来,printbalance()函数用于打印余额;它接受余额。然后,在主函数中,创建一个整数的余额变量。接下来,根据案例要执行的特定选项选择,打印用于取款、存款、余额和退出操作的选择 Piton,如我们在上面的输出中看到的。

结论

自动柜员机 (ATM) 是一种电子电信系统,允许银行公司的客户进行金融交易。我们可以用Java创建一个ATM程序来显示ATM交易,用户可以取款、存款、查看余额、退出ATM。

以上是Java ATM 程序的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!