java每日一题——ATM系统编写(答案及编程思路)

前言:

基础语句学完,也可以编写一些像样的程序了,现在要做的是多加练习,巩固下知识点,打好基础,daydayup!

题目:模仿银行ATM系统,可以创建用户,存钱,转账,修改密码注销账户等操作

思路:利用面向对象编程:1,定义一个账户类Account,至少需要包含(卡号、姓名、性别、密码、余额、每次取现额度);2,定义一个ATM类,用来代表ATM系统,负责提供所有的业务需求;3,定义一个测试类Test,负责对我们开发的ATM系统进行测试。

1,创建实体类:

创建一个实体类,用来记录姓名、卡号、性别、密码、余额、每次取现额度等信息。

public class Account {private String card;private String username ;private  char sex;private  String password;private double money;private  double limit;public Account() {}public String getCard() {return card;}public void setCard(String card) {this.card = card;}public String getUsername() {return username + (sex=='男'? "先生":"女士");}public void setUsername(String username) {this.username = username;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}public double getLimit() {return limit;}public void setLimit(double limit) {this.limit = limit;}
}

2,创建操作类:

1,创建界面 

通过选择1,或选择2进入系统

  public void start(){Scanner sc =new Scanner(System.in);while (true) {System.out.println("======欢迎来到ATM=======");System.out.println("1,用户登陆");System.out.println("2,用户开户");System.out.println("请选择");int id =sc.nextInt();switch (id){case 1://用户登陆login();break;case 2://用户开户Operator();break;default:System.out.println("请重新输入");}}}

2,用户开户 

运用Scanner输入来记入需要记录的信息,值得一提的是:由于Scanner没办法记录char变量,所以使用charAt来取第一个值。

   private  void Operator(){Account acc = new Account();System.out.println("请输入姓名");String name = sc.next();acc.setUsername(name);while (true) {System.out.println("请输入性别(男/女)");char sex = sc.next().charAt(0);if (sex == '男'|| sex == '女'){acc.setSex(sex);break;}else{System.out.println("请输入(男/女)");}}while (true) {System.out.println("请设置你的密码");String password = sc.next();System.out.println("请再次输入你的密码");String okpassword = sc.next();if (password.equals(okpassword)){acc.setPassword(okpassword);break;}else{System.out.println("两次密码不一致,请重新输入");}}System.out.println("请设置每日取款限额");double limit = sc.nextDouble();acc.setLimit(limit);String id =card();acc.setCard(id);accounts.add(acc);System.out.println("恭喜"+acc.getUsername()+"开户成功,您的卡号为:"+acc.getCard());}
2.1系统输入卡号 

由于卡号需要随机生成8位数字,并且不能与其他人的号码重复,所以需要建立两个方法来做调试。一个方法用于生成8位随机数字,一个方法用于检测号码是否重复

private String card(){Random r = new Random();while (true) {String id = "";for (int i = 0; i < 8; i++) {int data=  r.nextInt(10);id +=data;}Account acc= vs(id);if (acc == null){return id;}}}private  Account vs (String card){for (int i = 0; i <accounts.size(); i++) {Account acc= accounts.get(i);if (acc.getCard().equals(card)){return acc;}}return null;

 这样一来,开户也就成功了。接下来是登陆的操作。

3,用户登陆

需要注意的是:当系统中没有账号时,要提示没有账号。登陆时需要注意号码的匹对。

 private void login(){if (accounts.size()==0){System.out.println("请先创建账号");return;}while (true) {System.out.println("请输入卡号");String card = sc.next();Account acc = vs(card);if(acc == null){System.out.println("没有该账号,请重新输入");}else if(acc.getCard().equals(card)){while (true) {System.out.println("请输入密码");String password =sc.next();if (acc.getPassword().equals(password)){acco = acc;check();return;}else{System.out.println("密码不正确,请重新输入");}}}}}

4,业务界面

登陆成功后,便可进行业务选择。运用switch语句可以精准选择业务需求

private void check(){while (true) {System.out.println(acco.getUsername()+"你可以办理以下业务");System.out.println("1.查询账户");System.out.println("2.存款");System.out.println("3.取款");System.out.println("4.转账");System.out.println("5.修改密码");System.out.println("6.退出");System.out.println("7.注销账户");System.out.println("请选择");int check= sc.nextInt();switch (check){case 1:idcheck();break;case 2:moenycheck();break;case 3:moneyleave();break;case 4:transmoney();break;case 5:changepassword();return;case 6:System.out.println("你已经退出");return;case 7:if (deleteid());return;default:System.out.println("你输入的数字有误,请重新输入");}}}
4.1账户确认

建议独立一个方法,其他没什么需要注意的

private  void idcheck(){System.out.println("号码:"+acco.getCard());System.out.println("性别:"+acco.getSex());System.out.println("存款:"+acco.getMoney());System.out.println("每日限额:"+acco.getLimit());}
4.2存款 

 熟用switch语句及死循环能够很好的解决问题

private void moenycheck() {while (true) {System.out.println("欢迎来到存款界面");System.out.println("存款请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch (sd){case 1:System.out.println("请输入你要存多少");double money= sc.nextDouble();System.out.println("请问你确定要存入"+money+"么");System.out.println("确定请按1");System.out.println("返回请按2");int cc =sc.nextInt();switch (cc){case 1:acco.setMoney(acco.getMoney()+money);System.out.println("您的余额为"+acco.getMoney());break;case 2:return;default:System.out.println("请重新输入");}break;case 2:return;default:System.out.println("请重新输入");}}}
4.3取款 

一连串的switch语句和if问句,解决每一项逻辑

 private void moneyleave() {while (true) {System.out.println("欢迎来到取款界面");System.out.println("取款请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch(sd){case 1:System.out.println("你目前的存款为" + acco.getMoney());if (acco.getMoney() < 100) {System.out.println("最低取款金额为100,您的余额不足");break;} else {System.out.println("请输入你要取走的金额");double money = sc.nextDouble();if (acco.getMoney()<money){System.out.println("您的余额不足,请重新输入");break;}else{if (money>acco.getLimit()){System.out.println("您已超过限额,请重新输入");break;}else{System.out.println("您已取走"+money+"元");acco.setMoney(acco.getMoney()- money);System.out.println("您的余额为:"+acco.getMoney());}}}break;case 2:return;default:System.out.println("请重新输入");}}}
4.4转账

选要注意的是:这里需要判断对方的姓氏,采用的方法为“*”加上第二位开始的名字。转账者需要填写姓氏后运用startwith语句进行匹配。

private void transmoney() {while (true) {System.out.println("欢迎来到转账界面");System.out.println("转账请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch (sd){case 1:if (accounts.size()<2){System.out.println("当前系统中只有一个账号,请创建新的账号");break;}if (acco.getMoney()==0){System.out.println("您的余额不足,不能转账");break;}System.out.println("请输入对方的账号");String id =sc.next();Account acc =vs(id);if (acc == null){System.out.println("没有该账号,请重新输入");}else{String name ="*"+acc.getUsername().substring(1);System.out.println("请输入【"+name+"】的姓氏");String trname =sc.next();if (acc.getUsername().startsWith(trname)){System.out.println("请输入转账金额");double money =sc.nextDouble();if (acco.getMoney() >= money){acco.setMoney(acco.getMoney()-money);acc.setMoney(acc.getMoney()+ money);System.out.println("您已转账"+money+"元,您的余额为"+acco.getMoney());break;}else{System.out.println("您的余额不足,不能转账");}}else {System.out.println("姓氏认证有问题");}}break;case 2:return;}}}
4.5 更换密码

使用if语句询问即可,需要注意的是,最后要用return返回,不能用break,(return是退出方法,break是退出循环)

 private void changepassword() {while (true) {System.out.println("欢迎来到更换密码界面");System.out.println("输入当前密码");String pass =sc.next();if (acco.getPassword().equals(pass)){System.out.println("输入新密码");String okpass =sc.next();System.out.println("再一次输入新密码");String okkpass =sc.next();if (okkpass.equals(okpass)){acco.setPassword(okkpass);System.out.println("修改密码成功");return;}else{System.out.println("密码有误");}}else {System.out.println("密码有误");}}}
4.6退出系统  

用return即可

  case 6:System.out.println("你已经退出");return;
4.7删除账户 

删除当前账户即可。当前账户和创建账户的实体类是同一个地址,删除当前账户就是在ArrayList中删除了当前账户实体类的地址

  private boolean deleteid() {while (true) {System.out.println("确定删除么(y/n)");String sd =sc.next();switch (sd){case "y":if (acco.getMoney()==0){accounts.remove(acco);return true;}else{System.out.println("还有存款,不能销户");return false;}default:System.out.println("删除失败");return false;}}}

操作完整版在这里 

这样操作类就完成了,有需要的可以复制这个完整版


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class ATMOperator {private ArrayList<Account> accounts = new ArrayList<>();private Scanner sc = new Scanner(System.in);private  Account acco = new Account();public void start(){Scanner sc =new Scanner(System.in);while (true) {System.out.println("======欢迎来到ATM=======");System.out.println("1,用户登陆");System.out.println("2,用户开户");System.out.println("请选择");int id =sc.nextInt();switch (id){case 1://用户登陆login();break;case 2://用户开户Operator();break;default:System.out.println("请重新输入");}}}private void login(){if (accounts.size()==0){System.out.println("请先创建账号");return;}while (true) {System.out.println("请输入卡号");String card = sc.next();Account acc = vs(card);if(acc == null){System.out.println("没有该账号,请重新输入");}else if(acc.getCard().equals(card)){while (true) {System.out.println("请输入密码");String password =sc.next();if (acc.getPassword().equals(password)){acco = acc;check();return;}else{System.out.println("密码不正确,请重新输入");}}}}}private void check(){while (true) {System.out.println(acco.getUsername()+"你可以办理以下业务");System.out.println("1.查询账户");System.out.println("2.存款");System.out.println("3.取款");System.out.println("4.转账");System.out.println("5.修改密码");System.out.println("6.退出");System.out.println("7.注销账户");System.out.println("请选择");int check= sc.nextInt();switch (check){case 1:idcheck();break;case 2:moenycheck();break;case 3:moneyleave();break;case 4:transmoney();break;case 5:changepassword();return;case 6:System.out.println("你已经退出");return;case 7:if (deleteid());return;default:System.out.println("你输入的数字有误,请重新输入");}}}private void changepassword() {while (true) {System.out.println("欢迎来到更换密码界面");System.out.println("输入当前密码");String pass =sc.next();if (acco.getPassword().equals(pass)){System.out.println("输入新密码");String okpass =sc.next();System.out.println("再一次输入新密码");String okkpass =sc.next();if (okkpass.equals(okpass)){acco.setPassword(okkpass);System.out.println("修改密码成功");return;}else{System.out.println("密码有误");}}else {System.out.println("密码有误");}}}private boolean deleteid() {while (true) {System.out.println("确定删除么(y/n)");String sd =sc.next();switch (sd){case "y":if (acco.getMoney()==0){accounts.remove(acco);return true;}else{System.out.println("还有存款,不能销户");return false;}default:System.out.println("删除失败");return false;}}}private void transmoney() {while (true) {System.out.println("欢迎来到转账界面");System.out.println("转账请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch (sd){case 1:if (accounts.size()<2){System.out.println("当前系统中只有一个账号,请创建新的账号");break;}if (acco.getMoney()==0){System.out.println("您的余额不足,不能转账");break;}System.out.println("请输入对方的账号");String id =sc.next();Account acc =vs(id);if (acc == null){System.out.println("没有该账号,请重新输入");}else{String name ="*"+acc.getUsername().substring(1);System.out.println("请输入【"+name+"】的姓氏");String trname =sc.next();if (acc.getUsername().startsWith(trname)){System.out.println("请输入转账金额");double money =sc.nextDouble();if (acco.getMoney() >= money){acco.setMoney(acco.getMoney()-money);acc.setMoney(acc.getMoney()+ money);System.out.println("您已转账"+money+"元,您的余额为"+acco.getMoney());break;}else{System.out.println("您的余额不足,不能转账");}}else {System.out.println("姓氏认证有问题");}}break;case 2:return;}}}private void moneyleave() {while (true) {System.out.println("欢迎来到取款界面");System.out.println("取款请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch(sd){case 1:System.out.println("你目前的存款为" + acco.getMoney());if (acco.getMoney() < 100) {System.out.println("最低取款金额为100,您的余额不足");break;} else {System.out.println("请输入你要取走的金额");double money = sc.nextDouble();if (acco.getMoney()<money){System.out.println("您的余额不足,请重新输入");break;}else{if (money>acco.getLimit()){System.out.println("您已超过限额,请重新输入");break;}else{System.out.println("您已取走"+money+"元");acco.setMoney(acco.getMoney()- money);System.out.println("您的余额为:"+acco.getMoney());}}}break;case 2:return;default:System.out.println("请重新输入");}}}private void moenycheck() {while (true) {System.out.println("欢迎来到存款界面");System.out.println("存款请按1");System.out.println("退出请按2");int sd = sc.nextInt();switch (sd){case 1:System.out.println("请输入你要存多少");double money= sc.nextDouble();System.out.println("请问你确定要存入"+money+"么");System.out.println("确定请按1");System.out.println("返回请按2");int cc =sc.nextInt();switch (cc){case 1:acco.setMoney(acco.getMoney()+money);System.out.println("您的余额为"+acco.getMoney());break;case 2:return;default:System.out.println("请重新输入");}break;case 2:return;default:System.out.println("请重新输入");}}}private  void idcheck(){System.out.println("号码:"+acco.getCard());System.out.println("性别:"+acco.getSex());System.out.println("存款:"+acco.getMoney());System.out.println("每日限额:"+acco.getLimit());}private  void Operator(){Account acc = new Account();System.out.println("请输入姓名");String name = sc.next();acc.setUsername(name);while (true) {System.out.println("请输入性别(男/女)");char sex = sc.next().charAt(0);if (sex == '男'|| sex == '女'){acc.setSex(sex);break;}else{System.out.println("请输入(男/女)");}}while (true) {System.out.println("请设置你的密码");String password = sc.next();System.out.println("请再次输入你的密码");String okpassword = sc.next();if (password.equals(okpassword)){acc.setPassword(okpassword);break;}else{System.out.println("两次密码不一致,请重新输入");}}System.out.println("请设置每日取款限额");double limit = sc.nextDouble();acc.setLimit(limit);String id =card();acc.setCard(id);accounts.add(acc);System.out.println("恭喜"+acc.getUsername()+"开户成功,您的卡号为:"+acc.getCard());}private String card(){Random r = new Random();while (true) {String id = "";for (int i = 0; i < 8; i++) {int data=  r.nextInt(10);id +=data;}Account acc= vs(id);if (acc == null){return id;}}}private  Account vs (String card){for (int i = 0; i <accounts.size(); i++) {Account acc= accounts.get(i);if (acc.getCard().equals(card)){return acc;}}return null;}
}

最后测试:

public class ATMDemo {public static void main(String[] args) {ATMOperator de = new ATMOperator();de.start();}}

 测试效果:

总结:完美运行,有些语句需要在加强,熟用if语句和switch可以完成精准操作

整理结束撒花!!!! 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/620211.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2023-12-02 青少年软件编程(C语言)等级考试试卷(五级)解析

2023-12-02 青少年软件编程(C语言)等级考试试卷(五级)解析 一、编程题(共4题,共100分)T1. 书架 John最近买了一个书架用来存放奶牛养殖书籍,但书架很快被存满了,只剩最顶层有空余。 John共有N头奶牛(1 ≤ N ≤ 20,000),每头奶牛有自己的高度Hi(1 ≤ Hi ≤ 10,000),…

Python 二维平面Delaunay三角网建立

目录 一、算法概念二、代码实现三、结果示例根据二维平面内的离散点建立平面三角网。 一、算法概念 三角剖分与Delaunay剖分定义:如何把一个散点集剖分成不均匀的三角形网格,即在给定的平面点集上,生成三角形集合的过程。考虑平面点集P={p1,p2,p3,…,pn},我们希望得到三…

基于Matlab/Simulink开发自动驾驶的解决方案

文章目录 处理自动驾驶数据 仿真自动驾驶场景 设计感知算法 设计规划和控制算法 生成代码和部署算法 集成和测试 参考文献 使用 MATLAB/Simulink开发自动驾驶&#xff0c;能够深入建模真实世界的行为、减少车辆测试并验证嵌入式软件的功能&#xff0c;从而推进自动驾驶感…

QuEra 10,000个物理量子位和100个逻辑量子位的量子计算机2026

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

xtu oj 1334 Least Common Multiple

题目描述 一个集合&#xff0c;任取3个不同的元素&#xff0c;求其最小公倍数中最小的值是多少&#xff1f; 输入 第一行是样例数T(1≤T≤100)。 每个样例的第一行是一个整数n(3≤n≤50)&#xff0c;表示集合元素的个数。 每个样例的第二行是n个整数a1,a2,…,an,1≤ai≤106。…

AWS-SAA-C03认证——之基础知识扫盲

文章目录 前言一、Amazon ECS二、 Amazon EKS三、Amazon EC2四、Elastic Beanstalk五、AWS Fargate六、 AWS Lambda &#xff08;serverless&#xff09;七、Amazon EBS7.1 EBS生命周期 八、Amazon Elastic File System (EFS) -共享文件系统九、什么是 Amazon S3&#xff1f;9.…

anoconda 安装报错

表现形式&#xff1a;Output folder: D:\anoconda\Lib Extract: _nsis.py Extract: _system_path.py Output folder: D:\anoconda........................ 解决办法&#xff1a; 网址&#xff1a;Index of /anaconda/archive/ | 清华大学开源软件镜像站 | Tsinghua Open Sour…

Jupyter Notebook之命令行执行Jupyter Notebook文件的命令行实现

命令行 jupyter nbconvert --to notebook --execute yourjuypter.ipynb --output executed.ipynb命令行参数解析 jupyter nbconvert: 这是调用 Jupyter Notebook 的 nbconvert 工具的命令。nbconvert 允许用户将 Jupyter Notebook&#xff08;.ipynb 文件&#xff09;转换为…

Django教程|数据统计图表(echarts、highchart)

前言 highchart&#xff0c;国外。 echarts&#xff0c;国内。 本项目集成 hightchart和echarts图表库实现数据统计功能。 包括&#xff1a;折线图&#xff0c;柱状图&#xff0c;饼图和数据集图。 效果图 echats Highcharts 源代码 编写模板&#xff08;Template&#x…

华为设备vlan下配置MSTP,STP选举

核心代码,不同实例&#xff0c;承载不同流量&#xff0c;为每个实例设置一个根网桥达到分流的效果 stp region-config //进入stp区域的设置 region-name R1 //区域命名为R1 instance 1 vlan 10 …

专业课140+总分410+电子科技大学858信号与系统考研经验,电子信息通信

我的初试备考从4月末&#xff0c;持续到初试前&#xff0c;这中间没有中断。 我是二战考生&#xff0c;准备的稍微晚一些&#xff0c;如果是一战考生&#xff0c;建议在2、3月份开始。 总的时间分配上&#xff0c;是数学>专业课>英语>政治&#xff0c;虽然大家可支配…

【Flutter】关注的那些点

第一次接触Flutter 应该是19年&#xff0c;被声明式语法所吸引&#xff0c;后来苦于事件成本的问题那篇Fltutter 状态管理的Provider 就一直创建在哪里&#xff0c;没有理会他。也一直没空去写。22年因为某些原因&#xff0c;去了某厂&#xff0c;主要的跨平台技术栈事ReactNat…

DP读书:《openEuler操作系统》(八)TCP、UDP与跨机器通讯

10min速通TCP与UDP 2024 DP读书计算机网络简介TCP/IP协议栈A. 物理层1.信号及信道传递2.信号调制与调解3.信道的复用 B. 数据链路层1.封装成帧2.透明传输3.差错控制 C. 网络层1.IP2.ARP3.路由选择协议 D. 传输层1.端口号2.3.UDP 2024 DP读书 第八章 跨机器通讯 在第六章之中&a…

ES的索引库操作

索引库操作 索引库就类似数据库表&#xff0c;mapping映射就类似表的结构。 我们要向es中存储数据&#xff0c;必须先创建“库”和“表”。 1.mapping映射属性 mapping是对索引库中文档的约束&#xff0c;常见的mapping属性包括&#xff1a; type&#xff1a;字段数据类型&a…

Tomcat 静态资源访问与项目根路径设置(AI问答)

一个静态文件&#xff0c;放在Tomcat中&#xff0c;希望能够通过网络访问&#xff0c;应该放在哪里&#xff1f; 在Apache Tomcat中&#xff0c;如果想要部署静态文件&#xff08;例如HTML、CSS、JavaScript、图片等&#xff09;并能通过网络访问&#xff0c;通常需要将这些文…

MySQL修炼手册7:数据修改基础:INSERT、UPDATE、DELETE语句详解

写在开头 在掌握了MySQL数据库的基础之后&#xff0c;学习如何对数据进行有效的修改是至关重要的。本篇博客旨在提供一个深入的指南&#xff0c;涵盖了数据修改的三大基础操作&#xff1a;插入&#xff08;INSERT&#xff09;、更新&#xff08;UPDATE&#xff09;、删除&…

java数据结构与算法:单链表 SinglyLinkedList

单链表 SinglyLinkedList 创建实现类并实现方法 package com.lhs;public class SinglyLinkedList<E> implements List<E>{// 头节点private Node<E> first;// 尾节点private Node<E> last;// 节点数量private int size;public static class Node<…

微信小程序开发学习笔记《12》下拉刷新事件

微信小程序开发学习笔记《12》下拉刷新事件 博主正在学习微信小程序开发&#xff0c;希望记录自己学习过程同时与广大网友共同学习讨论。建议仔细阅读官方文档 一、什么是下拉刷新 下拉刷新是移动端的专有名词&#xff0c;指的是通过手指在屏幕上的下拉滑动操作&#xff0c;…

php反序列化漏洞基础

一、序列化 serialize(): 序列化是将对象或类转换为字符串的过程,以便在程序运行过程中对其进行持久化存储或传输的操作。在PHP中,序列化主要用于将类对象或数组转换成字节流的形式,以便于存储在磁盘或传输到其他系统。 通过序列化,可以将对象或类转换成一串字符串,然后可…

【数据开发】HiveSQL 临时表分步执行(with, as )与时间函数(时间戳unix_timestamp)

1、分步执行&#xff08;with…as…&#xff09; Hive SQL中的WITH…AS…语句可以用于分步执行&#xff0c;即将一个大的查询语句拆分成多个小的查询语句&#xff0c;每个小的查询语句都可以使用WITH…AS…语句定义一个临时表&#xff0c;然后在后面的查询语句中使用这些临时表…