java实现atm机,java基础简单实现atm机
本文实例为大家分享了爪哇实现异步传输模式系统的具体代码,供大家参考,具体内容如下
这应该算最基础的标准版项目了,但其中逻辑还是得想想的。
功能还算完善,只是对输入数据的校验没做全,之后做网时再加上。
没有数据库。
完整代码在最后。
流程图
登录
想模拟提款机插卡登录的,因此没做注册账号的功能,手动先塞了三个账号。有三次输入密码机会。
查询
取款
存款
转账
修改密码
退出
代码
文件结构
下面两个类复制过去放到ATM文件夹下就能运行。
打包异步传输模式导入Java。文字。简单的日期格式;导入Java。util。日历;导入Java。util。日期;公共类帐户{私有字符串卡号私有字符串用户名;私有字符串密码;私人双平衡;私有布尔状态;//卡锁定状态public Account(){ } public Account(字符串cardID,字符串用户名,字符串密码,双余额,布尔状态){ this。cardID=cardIDthis .用户名=用户名;this.password=密码;this . balance=balance this . status=status }公众账号(字符串cardID,字符串用户名,字符串密码,双余额){ this . cardid=cardid this . username=用户名;this.password=密码;this.balance=balance}公共字符串get cardID(){ return cardID;} public void setCardID(String cardID){ this。cardID=cardID}公共字符串get username(){ return username;} public void set username(字符串用户名){ this。用户名=用户名;}公共字符串get password(){返回密码;} public void set password(字符串密码){ this。密码=密码;} public double get balance(){ return balance;} public void set balance(double balance){ this。余额=余额;} public boolean isStatus() {返回状态;} public void set status(布尔状态){ this。地位=地位;} @覆盖
public String toString(){ System.currentTimeMillis(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatDate = sdf.format(date); return "银行:新航路银行n" + "银行账号:"+cardID+"n" +"用户名:"+username+"n" +"余额:"+balance+"n" +formatDate; }}
package ATM;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Scanner;public class ATM { static Scanner sc = new Scanner(System.in); static ArrayList<Account> accounts; public static void main(String[] args) { //模仿插卡ATM,因此没做注册用户功能,这里自己添加了几个用户做测试。 accounts = new ArrayList<>(); accounts.add(new Account("10001", "路飞", "ONEPIECE", 100,true)); accounts.add(new Account("10002", "索隆", "123456", 10000,false)); accounts.add(new Account("10003", "娜美", "123456", 1000000d,true)); //登录 loginVerify(); } //登录验证 public static void loginVerify() { System.out.println("提示:有这些账户"); for (Account account : accounts) { System.out.println("卡号:"+account.getCardID() +" 用户名:" + account.getUsername()+" 密码:"+account.getPassword()+" 余额:"+account.getBalance()); } System.out.println("---------------------------------------------"); //模拟插卡,手动输入银行卡号 System.out.print("输入银行卡号:"); String cardID = sc.next(); //根据卡号,判断此账号是否存在 for (int i = 0; i < accounts.size(); i++) { Account account = accounts.get(i); if (cardID.equals(account.getCardID())) { //判断卡号是否存在 if(account.isStatus()) { //看该卡是否被锁 //账号存在,输入密码 System.out.print("请输入密码:"); int count = 3;//可输入3次密码 while (count > 0) { sc.useDelimiter("n"); String pwd = sc.next(); if (pwd.equals(accounts.get(i).getPassword())) { //登录成功,转到主界面 mainInterface(accounts.get(i)); } else { if(--count == 0){ System.out.println("此卡已锁,请到人工处咨询办理。"); accounts.get(i).setStatus(false); }else { System.out.println("还有" + count + "次输入机会"); } } } }else{ System.out.println("此卡已锁,请到人工处咨询办理。"); } break; } if(i == accounts.size()-1){ System.out.println("此卡号不存在!!"); } } } public static void mainInterface(Account account) { System.out.println("====================================="); System.out.println(" 1.查询余额 5.修改密码"); System.out.println(" 2.取款 6.退卡 "); System.out.println(" 3.存款 "); System.out.println(" 4.转账 "); System.out.println("====================================="); System.out.print("请输入要操作的号码:"); int i = sc.nextInt(); switch (i) { case 1: queryBalance(account); break; case 2: withdrawal(account); break; case 3: deposit(account); break; case 4: transfer(account); break; case 5: updatePassword(account); break; case 6: exitSystem(); break; default: System.out.println("输入号码不对!"); break; } } //查询余额 public static void queryBalance(Account account) { System.out.println("====================================="); System.out.println(" "+account.getUsername() + "的余额: " + account.getBalance()); System.out.println("====================================="); mainInterface(account); } //取款 public static void withdrawal(Account account) { System.out.print("请输入取款金额:"); double money = sc.nextInt(); if (money <= account.getBalance()) { account.setBalance(account.getBalance() - money); System.out.println("取款成功!!"); System.out.println("====================================="); System.out.println(" 1.返回主界面 2.打印发票"); System.out.println("====================================="); int i = sc.nextInt(); switch (i) { case 1: mainInterface(account); break; case 2: Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatDate = sdf.format(date); System.out.println("银行:新航路银行n"+ "银行卡:"+account.getCardID()+"n"+ "用户名:"+account.getUsername()+"n"+ "本次取款:"+money+"n"+ formatDate); mainInterface(account); break; } } else { System.out.println("oh,我可怜的孩子!余额不足捏。"); System.out.println("====================================="); System.out.println(" 1.返回 enter "); System.out.println("====================================="); sc.next(); mainInterface(account); } } //存款 public static void deposit(Account account) { System.out.print("请输入要存款金额:"); double tempMoney = sc.nextDouble(); account.setBalance(account.getBalance() + tempMoney); System.out.println("哔~哔~哔 点钞中..."); System.out.println("存款成功"); mainInterface(account); } //转账 public static void transfer(Account account) { System.out.println("请输入对方卡号(提示账号有:10001 /10002 /10003)"); String cardID = sc.next(); //根据卡号,判断此账号是否存在 for (int i = 0; i < accounts.size(); i++) { if (cardID.equals(accounts.get(i).getCardID())) { System.out.print("请输入对象用户名验证:"); sc.nextLine(); String next = sc.nextLine(); if(next.equals(accounts.get(i).getUsername())) { System.out.print("请输入要转的金额:"); double m = sc.nextDouble(); if (m > account.getBalance()) { System.out.println("你没这么多钱,转账失败!"); break; } else { account.setBalance(account.getBalance() - m); accounts.get(i).setBalance(m + accounts.get(i).getBalance()); &am
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。