当前位置:网站首页>ATM系统
ATM系统
2022-07-07 14:28:00 【张 明明】
ATM系统
***系统准备、首页设计
用户开户功能实现
用户登录功能实现
用户操作页设计、查询账户、退出账户功能实现
用户存款功能实现
用户取款功能实现
用户转账功能实现
用户密码修改、销户功能实现***
p86-p90
https://www.bilibili.com/video/BV1Cv411372m?p=90
package com.itheihma01;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/* * ATM系统入口类 * * */
public class ATMSystem {
public static void main(String[] args) {
//1,定义账户类
//2,定义一个集合容器,负责以后存储账户
ArrayList<Account> accounts=new ArrayList<>();
//3展示系统的首页
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("===============黑马ATM系统================");
System.out.println("1,账户登录");
System.out.println("2,账户开户");
System.out.println("请你选择操作:");
int command= sc.nextInt();
switch(command){
case 1:
//用户登录
login(accounts,sc);
break;
case 2:
//用户开户
register(accounts,sc);
break;
default:
System.out.println("你输入的命令不存在");
}
}
}
/* * 登录功能 * */
private static void login(ArrayList<Account> accounts, Scanner sc) {
System.out.println("================系统登录操作================");
if(accounts.size()==0){
System.out.println("对不起,当前系统并无账户,请先注册");
return;
}
//2正式进入登录操作
while (true) {
System.out.println("请你输入登录卡号:");
String cardId=sc.next();
//3判断卡号是否存在
Account acc=getAccountByCardId(accounts,cardId);
if(acc!=null){
//卡号存在
//4,让用户输入密码
while (true) {
System.out.println("请输入密码;");
String password=sc.next();
if(acc.getPassword().equals(password)){
//登录成功了
System.out.println("恭喜你"+acc.getUserName()+"先生/女士进入系统,你的卡号是:"+acc.getCardId());
//展示登录后的操作页
showUserCommand(sc,acc,accounts);
return;
}else{
System.out.println("密码错误");
}
}
}else{
System.out.println("对不起,系统不存在该账户");
}
}
}
//展示登录后的操作页
private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts) {
while (true) {
System.out.println("===========用户操作页===========");
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,注销账户");
int command=sc.nextInt();
switch (command){
case 1:
//1,查询账户
showAccount(acc);
break;
case 2:
//2,存款
despositMoney(acc,sc);
break;
case 3:
//3,取款
drawMoney(acc,sc);
break;
case 4:
//4,转账
transforMoney(sc,acc,accounts);
break;
case 5:
//5,修改密码
updatePassword(sc,acc);
return;
case 6:
//6,退出
System.out.println("退出成功,欢迎下次光临");
return;
case 7:
//7,注销账户,从当前集合删除当前账户,销毁就完成了
accounts.remove(acc);
System.out.println("销户成功!");
return;
default:
System.out.println("你输入的命令不正确");
}
}
}
//修改密码
private static void updatePassword(Scanner sc, Account acc) {
System.out.println("=========================用户密码修改===================");
while (true) {
System.out.println("请你输入当前密码:");
String password=sc.next();
if(acc.getPassword().equals(password)){
while (true) {
//密码正确
//输入新密码
System.out.println("请你输入新密码:");
String newpasswoed=sc.next();
System.out.println("请你再次输入密码:");
String okpasswoed=sc.next();
if(newpasswoed.equals(okpasswoed)){
acc.setPassword(okpasswoed);
System.out.println("密码修改成功");
return;
}else{
System.out.println("你输入的两次密码不一致");
}
}
}else{
System.out.println("密码错误");
}
}
}
//转账功能
private static void transforMoney(Scanner sc, Account acc, ArrayList<Account> accounts) {
System.out.println("================用户转账操作======================");
//判断账户个数
if(accounts.size()<2){
System.out.println("当前系统不足2个账户,不能转账");
return;
}
//判断自己账户是否有钱
if(acc.getMoney()==0){
System.out.println("当前余额为0,无法转账");
return;
}
//真正开始转账
while (true) {
System.out.println("请你输入对方账户:");
String cardId=sc.next();
//这个考好不能是自己
if(cardId.equals(acc.getCardId())){
System.out.println("对不起,你不能给自己转账");
continue;
}
//根据这个卡号查账户
Account account=getAccountByCardId(accounts,cardId);
if(account==null){
System.out.println("不存在该账户");
}else{
//账户存在,继续认证姓氏
String username=account.getUserName();
String tip="*"+username.substring(1);
System.out.println("请你输入["+tip+"]的姓氏");
String prename=sc.next();
if(username.startsWith(prename)){
while (true) {
//认证通过
System.out.println("请你输入转账金额:");
double money=sc.nextDouble();
if(money>acc.getMoney()){
System.out.println("余额不足,你最多可转金额:"+acc.getMoney());
}else{
//余额充足
acc.setMoney(account.getMoney()-money);
account.setMoney(account.getMoney()+money);
System.out.println("转账成功,你的余额为:"+acc.getMoney());
return;
}
}
}else{
System.out.println("对不起,输入信息有误");
}
}
}
}
//取钱
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("============用户取钱操作==============");
if(acc.getMoney()<100){
System.out.println("对不起,当前账户余额不够100元");
return;
}
System.out.println("请你输入取款金额:");
double money=sc.nextDouble();
if(money> acc.getQuotaMoney()){
System.out.println("对不起,输入金额大于当次限额:"+acc.getQuotaMoney());
}else{
if(money> acc.getMoney()){
System.out.println("余额不足,当前余额:"+acc.getMoney());
}else{
System.out.println("恭喜你,取钱成功,取款"+money);
//更新余额
acc.setMoney(acc.getMoney()-money);
showAccount(acc);
return;
}
}
}
//存钱
private static void despositMoney(Account acc, Scanner sc) {
System.out.println("============用户存钱操作=============");
System.out.println("请你输入存款金额:");
double money=sc.nextDouble();
//更新账户余额;原来的钱+新存入的钱
acc.setMoney(acc.getMoney()+money);
System.out.println("恭喜你,存钱成功,当前账户信息如下:");
showAccount(acc);
}
//展示账户信息
private static void showAccount(Account acc) {
System.out.println("===========当前账户信息如下==============");
System.out.println("卡号:"+acc.getCardId());
System.out.println("户主:"+acc.getUserName());
System.out.println("余额:"+acc.getMoney());
System.out.println("限额:"+acc.getQuotaMoney());
}
/** * 用户开户功能实现 * @param accounts */
private static void register(ArrayList<Account> accounts,Scanner sc){
System.out.println("================系统开户操作================");
//1,创建一个账户对象
Account account=new Account();
//2,录入当前这个账户的信息
System.out.println("请你输入用户名:");
String username=sc.next();
account.setUserName(username);
while (true) {
System.out.println("请你输入账户密码;");
String passWord=sc.next();
System.out.println("请你再次输入密码;");
String okPassWord=sc.next();
if(passWord.equals(okPassWord)){
account.setPassword(passWord);
break;
}else{
System.out.println("你输入的两次密码不一致");
}
}
System.out.println("请你输入账户当次限额;");
double quotaMoney=sc.nextDouble();
account.setQuotaMoney(quotaMoney);
//为账户随机一个8位且与其他账户开户的卡号
String cardId=getRandomCardId(accounts);
account.setCardId(cardId);
//把账户对象添加到账户集合中去
accounts.add(account);
System.out.println("恭喜你"+username+"先生/女士,你开户成功,你的卡号是"+cardId+",请妥善保管");
}
/* 生成8位卡号 * */
private static String getRandomCardId(ArrayList<Account> accounts){
Random r=new Random();
while (true) {
String cardId="";
for (int i = 0; i < 8; i++) {
cardId +=r.nextInt(10);
}
//2.判断这个8位卡号是否与其他账户重复
Account acc=getAccountByCardId(accounts,cardId);
if(acc==null){
return cardId;
}
}
}
//根据卡号返回一个账户
private static Account getAccountByCardId(ArrayList<Account> accounts,String cardId){
for (int i = 0; i < accounts.size(); i++) {
Account acc=accounts.get(i);
if(acc.getCardId().equals(cardId)){
return acc;
}
}
return null;
}
}
Account类
package com.itheihma01;
public class Account {
private String cardId;
private String userName;
private String password;
private double money;
private double quotaMoney;
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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 getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
边栏推荐
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- PHP has its own filtering and escape functions
- spark调优(三):持久化减少二次查询
- Laravel5.1 路由 -路由分组
- Pycharm terminal enables virtual environment
- How to determine whether the checkbox in JS is selected
- logback. XML configure logs of different levels and set color output
- How to implement backspace in shell
- Vs tool word highlight with margin
猜你喜欢
水平垂直居中 方法 和兼容
AutoLISP series (3): function function 3
Unity3d click events added to 3D objects in the scene
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
如何快速检查钢网开口面积比是否符合 IPC7525
Leetcode-231-2的幂
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Personal notes of graphics (1)
Continuous creation depends on it!
AutoLISP series (2): function function 2
随机推荐
Personal notes of graphics (2)
【Android -- 数据存储】使用 SQLite 存储数据
Have fun | latest progress of "spacecraft program" activities
Opportunity interview experience summary
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Description of vs common shortcut keys
Find tags in prefab in unity editing mode
Continuous creation depends on it!
Power of leetcode-231-2
Opencv configuration 2019vs
Imitate the choice of enterprise wechat conference room
Deep listening array deep listening watch
laravel构造函数和中间件执行顺序问题
Laravel5.1 Routing - routing packets
Leetcode-231-2的幂
iptables只允许指定ip地址访问指定端口
Vs tool word highlight with margin
Performance measure of classification model
three. JS create cool snow effect
爬虫(17) - 面试(2) | 爬虫面试题库