制作一个简易的ATM取款机

news/2024/11/30 7:32:01/

 ATM取款机主函数

package ATM;import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;public class ATMSystem {public static void main(String[] args) {ArrayList<Account> accounts = new ArrayList<>();Scanner sc = new Scanner(System.in);showMain(accounts, sc);}public static void showMain(ArrayList<Account> accounts, Scanner sc) {while (true) {System.out.println("请您输入您想要做的操作:");System.out.println("1、登录账户");System.out.println("2、注册账户");System.out.print("您可以输入命令了:");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 register(ArrayList<Account> accounts, Scanner sc) {System.out.println("===============用户开户功能==============");System.out.println("请您输入开户名称:");String password = "";String name = sc.next();while (true) {System.out.println("请您输入用户密码:");password=sc.next();System.out.println("请您输入确认密码:");String okpassword = sc.next();if (okpassword.equals(password)) {break;} else {System.out.println("两次密码必须一致---");}}System.out.println("请您输入每次限额:");double quotaMoney = sc.nextDouble();String cardId = createCarId(accounts);Account account = new Account(cardId, name, password, quotaMoney);accounts.add(account);System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCarId() + "请您妥善保管");}public static String createCarId(ArrayList<Account> accounts) {while (true) {String cardId = "";Random r = new Random();for (int i = 0; i < 8; i++) {cardId += r.nextInt(10);}Account acc = getAccountBuCardId(cardId, accounts);if (acc == null) {return cardId;}}}public static Account getAccountBuCardId(String cardId, ArrayList<Account> accounts) {for (int i = 0; i < accounts.size(); i++) {Account acc = accounts.get(i);if (acc.getCarId().equals(cardId)) {return acc;}}return null;}public static void login(ArrayList<Account> accounts, Scanner sc) {if (accounts.size() == 0) {System.out.println("当前系统无任何账号,您需要先注册!");return;}while (true) {System.out.println("请您输入登录卡号:");String cardId = sc.next();Account acc = getAccountBuCardId(cardId, accounts);if (acc != null) {System.out.println("请您输入登录的密码:");String password = sc.next();if (acc.getPassWord().equals(password)) {System.out.println("恭喜您," + acc.getUserName() + "先生成功进入系统,您的卡号是:" + acc.getCarId());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、注销账户");System.out.printf("请您输入操作命令:");int comand = sc.nextInt();switch (comand) {case 1:showAccount(acc);break;case 2:deposit(acc, sc);break;case 3:withdrawal(acc, sc);break;case 4:transfer(acc, sc, accounts);break;case 5:changePassword(acc, sc, accounts);return;case 6:System.out.println("欢迎下次光临!!");return;case 7:accounts.remove(acc);System.out.println("销户成功!");return;default:System.out.println("您输入的命令有误");}}}private static void changePassword(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("========欢迎您进入修改密码界面========");while (true) {System.out.println("请您输入当前账户的密码:");String password = sc.next();if (acc.getPassWord().equals(password)) {while (true) {System.out.println("请你输入新的密码:");String password1 = sc.next();System.out.println("请您确认密码:");String password2 = sc.next();if (password1.equals(password2)) {acc.setPassWord(password1);System.out.println("恭喜您,密码修改成功,请重新登录");return;} else {System.out.println("两次密码输入不一致,请重新输入:");}}} else {System.out.println("您输入的密码错误,请重新输入:");}}}private static void transfer(Account acc, Scanner sc, ArrayList<Account> accounts) {System.out.println("======欢迎您进入黑马银行用户转账界面======");if (accounts.size() < 2) {System.out.println("当前系统,账户不足2个,不能转账!");return;}if (acc.getMoney() <= 0) {System.out.println("您自己都没钱,就别转了!");return;} else {while (true) {System.out.println("请您输入转账的账户卡号:");String cardId = sc.next();Account acct = getAccountBuCardId(cardId, accounts);if (acc.getCarId().equals(acct.getCarId())) {System.out.println("您不可以为自己转账!");}if (acct == null) {System.out.println("您输入的卡号有误,请重新输入");continue;} else {System.out.println("您当前要为【*" + acct.getUserName().substring(1) + "】转账!");while (true) {System.out.println("请您输入姓氏确认:");String name = sc.next();if (acct.getUserName().charAt(0) == name.charAt(0)) {System.out.println("请您确认是否转账:");String s = sc.next();if (s.equals("是"))transferMoney(acc, acct, sc);elseSystem.out.println("您已退出转账系统:");return;} else {System.out.println("输入姓氏错误,请您重新输入:");}}}}}}private static void transferMoney(Account acc, Account acct, Scanner sc) {while (true) {System.out.println("请您输入转账的金额");double money = sc.nextDouble();if (money > acc.getQuotaMoney())System.out.println("您当前转账超过了当前限额!最多可转:" + acc.getQuotaMoney());else {if (money < acc.getMoney()) {acc.setMoney(acc.getMoney() - money);System.out.println("恭喜您,转账" + money + "成功!当前账户还剩余:" + acc.getMoney());return;} else {System.out.println("您的余额不足,最多可转账:" + acc.getMoney());}}}}private static void withdrawal(Account acc, Scanner sc) {System.out.println("======欢迎您进入黑马银行用户取款界面======");if (acc.getMoney() < 100) {System.out.println("账户余额不足100元,就别取啦吧!");return;} else {while (true) {System.out.println("请您输入取款的金额");double money = sc.nextDouble();if (money > acc.getQuotaMoney())System.out.println("您当前取款超过了当前限额!最多可取:" + acc.getQuotaMoney());else {if (money < acc.getMoney()) {acc.setMoney(acc.getMoney() - money);System.out.println("恭喜您,取款" + money + "成功!当前账户还剩余:" + acc.getMoney());return;} else {System.out.println("您的余额不足,最多可取:" + acc.getMoney());}}}}}private static void deposit(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.getCarId());System.out.println("姓名:" + acc.getUserName());System.out.println("余额:" + acc.getMoney());System.out.println("当次限额:" + acc.getQuotaMoney());}
}

ATM取款机对象类

package ATM;public class Account {private String carId;private String userName;private String passWord;private double money;private double quotaMoney;public Account() {}public Account(String carId, String userName, String passWord, double quotaMoney) {this.carId = carId;this.userName = userName;this.passWord = passWord;this.quotaMoney = quotaMoney;}public String getCarId() {return carId;}public void setCarId(String carId) {this.carId = carId;}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;}
}


http://www.ppmy.cn/news/382773.html

相关文章

自动取款机 冲正交易

&#xff08;一&#xff09;冲正交易是什么&#xff1f; 简单理解冲正是将一笔错误的交易恢复&#xff0c;比如取款1000&#xff0c;但钱没取出来&#xff0c;余额却少了&#xff0c;那么这1000块会被自动冲正回来&#xff0c;将余额恢复。 &#xff08;二&#xff09; 什么是银…

取款机中出现假币的分析

取款机中可能出现假币吗&#xff1f;答案是&#xff1a;可能&#xff01;但是强调一点&#xff0c;大规模出现假币的可能性没有。毕竟这种事情不论在哪个国家都不会被允许的。 前面介绍过取款机的工作原理。下面针对某市著名论坛上越来越频繁出现的“ATM上取到假钞”的事件做个…

售货机模拟

文章目录 前言一、状态机回顾二、设计规范三、设计输入四、运行效果总结 前言 在没有扫码支付的年代&#xff0c;售货机是需要投币完成商品的购买&#xff0c;现在的售货机功能变得越来越多样&#xff0c;不仅可以纸币购买、硬币购买、还可以扫码支付&#xff0c;但是今天让我们…

取款机

#include<stdio.h> int main() { int account0,x; for(;;){ printf("1--清空&#xff0c;2--存钱"); printf("3--取钱,4--查询余额\n"); printf("请选择&#xff1a;"); scanf("%d",&x); switch(x){ case 1: account0; bre…

数字翻牌器

效果入下&#xff1a; // 翻牌器组件 countFlop.vue <template><div class"count-flop" :key"compKey"><div :class"item!.?count-flop-box:count-flop-point" v-for"(item, index) in value" :key"index&quo…

银行取号机

银行取号机 &#xff08;1&#xff09;设计思路&#xff1a;每个银行都有若干个柜台&#xff0c;初始时建一个队列数组&#xff0c;保存各个柜台的工作信息。 使用对象&#xff1a;银行职员处理业务&#xff0c;客户办理业务。 客户取号办理业务&#xff0c;应该选择排队人数…

模拟ATM取款机

模拟ATM取款机 问题描述&#xff1a;采用结构化程序设计思想实现模拟ATM&#xff08;自动柜员机&#xff09;。 功能包括&#xff1a; 1、模拟ATM&#xff08;自动柜员机&#xff09;主界面&#xff1b; 2、模拟ATM&#xff08;自动柜员机&#xff09;的查询功能&#xff1b;…

ATM机模拟系统

ATM机模拟系统 &#x1f4dd; 个人主页&#xff1a;程序员阿红&#x1f525; &#x1f389; 支持我&#xff1a;点赞&#x1f44d;收藏⭐️留言&#x1f4dd; &#x1f4e3; 推荐文章&#xff1a;都什么时候了&#xff0c;你还不会框架SSM整合&#x1f341; 一、 概述 &am…