Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize ATM Simulation and Application JavaR

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

如何实现 ATM模拟应用JavaR,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

用面向对象的方式实现参考

1. 用户类

import java.util.Scanner;//用户类public class MyUser { private String cardNumber="111"; //卡号 private String cardPassword="123"; //密码 static Scanner sc=new Scanner(System.in); // 用户登录 public boolean userLogin(){ System.out.println("请输入卡号:"); System.out.println("提示:模拟插入银行卡,系统中仅有卡号111,其它无效!"); String cnumber=sc.next(); System.out.println("请输入密码:"); System.out.println("提示:默认123!"); String cPassword=sc.next(); if (cnumber.equals(this.cardNumber) && cPassword.equals(this.cardPassword))return true; else return false; }}

2. ATM类

//ATM类public class ATM { //[1]定义变量,用于模拟银行数据库相关信息 static int balance; //余额 //构造方法--用于对象初始化 public ATM() { showWelcome(); balance=200; } //[2-1]显示欢迎界面 public void showWelcome(){ System.out.println("###########################################################"); System.out.println("欢迎使用工商银行自动取款机!"); System.out.println("###########################################################"); } //[2-2]显示主操作界面 public void showMain(){ System.out.println("======================================"); System.out.println("主菜单:"); System.out.println("1-查询余额"); System.out.println("2-提取现金"); System.out.println("3-存款"); System.out.println("4-退出"); System.out.println("======================================"); System.out.println("请输入选择序号:"); } //[2-3]存钱 public void add(){ System.out.println(); System.out.println("请输入存款金额:"); System.out.println("提示:只可输入数字!"); int number=MyUser.sc.nextInt(); if(number > 0){ balance+=number; System.out.println("存款成功!"); } else { System.out.println("输入错误,存款失败!"); } show(); } //[2-4]取钱 public void sub(){ System.out.println(); System.out.println("请输入取款金额:"); System.out.println("提示:只可输入数字!"); int number=MyUser.sc.nextInt(); if(number > balance){ System.out.println("余额不足,取款失败!"); }else { balance-=number; System.out.println("取款成功!"); } show(); } //[2-5]查询余额 public void show(){ System.out.println(); System.out.println("当前余额为: " + balance + " 元"); System.out.println(); showMain(); } //[2-6]退卡 public void exitATM(){ System.out.println("卡已退出…………"); showWelcome(); } }

3. 主类

//主类public class MyMain { public static void main(String[] args) { // 【1】面向对象方式实现 ATM objATM=new ATM(); //由于构造方法初识化了objATM这个对象,所以程序执行到这里已经显示ATM欢迎界面了 // 【2】调用MyUser的登录方法,实现登录功能 MyUser objUser=new MyUser(); int inNumber = 3 ; //定义变量,用于标记登录次数 boolean status=false; //用于标识用户登录状态 //[2-1]用户登录,通过status变量标识登录状态 do { status=objUser.userLogin(); if(status) { objATM.showMain();//显示主操作界面 break;//或者将循环条件设置为假 } else{ inNumber--; if (inNumber!=0) System.out.println("用户名或密码不正确,请重新输入!"); } } while (inNumber > 0); //[2-2]成功则选择操作 if(status) { boolean isSelect=true; while(isSelect) { //控制主功能菜单循环操作 String obj=MyUser.sc.next(); switch (obj) { case "1": objATM.show(); break; case "2": objATM.sub(); break; case "3": objATM.add(); break; case "4": isSelect=false; MyUser.sc.close(); objATM.exitATM(); } } } else { objATM.exitATM(); } }}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report