大家好
为了方便我们的生活,ATM机应运而生。那么如何使用java编译呢,本文详细为大家介绍。
希望对大家有帮助。
一、ATM虚拟机是什么?
所谓ATM虚拟机,就是使用编译软件,编译虚拟的ATM机。我们可以在上面进行一系列的操作,如,登录,注册账户。查询账户,存款,取款,转账,修改密码,退出登录,注销账户。
本文使用的是java进行编译。
二、使用步骤
1.编译代码
代码如下(示例):
package ATM; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class AtmSystem { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Account> accounts = new ArrayList<>(); 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("操作不规范,请重新操作"); } } } /** * * @param accounts 全部对象 * @param sc 扫描器 */ private static void login(ArrayList<Account> accounts, Scanner sc) { System.out.println("=========================系统登陆操作================="); if (accounts.size() == 0) { System.out.println("亲没有登录账户,请注册哦"); return; } while (true) { System.out.println("请输入登录卡号"); String cardId = sc.next(); Account acc = getaccountByCardId(accounts, cardId); if (acc != null) { //说明卡号存在 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("该卡号不存在亲,请重新登录"); } } } /** * * @param sc 扫描器 * @param acc 当前对象 * @param accounts 全部对象集合 */ 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: //定义方法查询 ShowAccount(acc); break; case 2: depositMoney(acc, sc); break; case 3: drawMoney(acc, sc); break; case 4: transferMoney(sc, acc, accounts); break; case 5: updatepassword(acc, sc); return; case 6: System.out.println("退出成功"); return; case 7: accounts.remove(acc); System.out.println("恭喜你注销成功"); return; default: System.out.println("操作不规范请重新操作"); } } } /** * @param acc * @param sc */ private static void updatepassword(Account acc, Scanner sc) { while (true) { System.out.println("请输入旧密码"); String password = sc.next(); // 认证旧密码 if (password.equals(acc.getPassword())) { while (true) { System.out.println("请输入新密码"); String newpassword = sc.next(); System.out.println("请确认输入新密码"); String okpassword = sc.next(); if (newpassword.equals(okpassword)) { acc.setPassword(okpassword); System.out.println("恭喜你密码修改成功"); return; } else { System.out.println("两次输入的密码不一致,请重新输入"); } } } else { System.out.println("旧密码错误请重新输入"); } } } /** * * @param sc * @param acc * @param accounts */ //用户转账界面 private static void transferMoney(Scanner sc, Account acc, ArrayList<Account> accounts) { while (true) { if (accounts.size() == 1) { System.out.println("不足两个账户,无法转账"); return; } if (acc.getMoney() == 0) { System.out.println("没有钱无法转账"); return; } else { while (true) { System.out.println("请输入卡号"); String cardId = sc.next(); if (acc.getCardId().equals(cardId)) { System.out.println("自己不能给自己转账"); continue; } Account account = getaccountByCardId(accounts, cardId); if (account == null) { System.out.println("这个卡号不存在,重新输入"); } else { System.out.println("恭喜你卡号输入成功"); // 认证姓氏 String username = account.getUsername(); String tip = "*" + username.substring(1); System.out.println("请输入" + tip + ",的姓氏"); String preName = sc.next(); if (username.startsWith(preName)) { System.out.println("请输入转账金额"); double money = sc.nextDouble(); acc.setMoney(acc.getMoney() - money); account.setMoney(account.getMoney() + money); System.out.println("转账完成"); ShowAccount(acc); ShowAccount(account); return; } else { System.out.println("你输入的信息有误"); } } } } } } /** * * @param acc * @param sc */ //取款方法 private static void drawMoney(Account acc, Scanner sc) { System.out.println("======取款界面======"); System.out.println("请输入取款金额"); if (acc.getMoney() < 100) { System.out.println("金额过少无法取出"); return; } double money = sc.nextDouble(); if (money > acc.getQuotamoney()) { System.out.println("超过最大取款额度" + "最大取款额度为:" + acc.getQuotamoney()); } else { if (money > acc.getMoney()) { System.out.println("余额不足" + "当前余额为" + acc.getMoney()); } else { acc.setMoney(acc.getMoney() - money); System.out.println("恭喜你取" + money + "款成功"); ShowAccount(acc); return; } } } //存款方法 /** * * @param acc * @param sc */ private static void depositMoney(Account acc, Scanner sc) { System.out.println("=========欢迎进入存款========"); System.out.println("请输入存款金额:"); double money = sc.nextDouble(); acc.setMoney(acc.getMoney() + money); 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()); } private static void register(ArrayList<Account> accounts, Scanner sc) { System.out.println("===================系统开户操作====================="); Account account = new Account(); 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 (okpassword.equals(password)) { 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("恭喜你," + account.getUsername() + "先生/女士卡号注册成功" + "卡号为:" + account.getCardId()); } public 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); } Account acc = getaccountByCardId(accounts, cardId); if (acc == null) { return cardId; } } } // // // 再次创建一个方法根据卡号查是否重复 public static Account getaccountByCardId(ArrayList<Account> accounts, String cardId) { for (int i = 0; i < accounts.size(); i++) { Account account = accounts.get(i); if (account.getCardId().equals(cardId)) { return account; } } return null; } } package ATM; 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; } }
上述代码,是属于2个类的,需要创建2个java类。
2.运行代码
如果出现上面运行结果,恭喜你运行成功了。