基于Java开发实现ATM系统
本文实例为大家分享了Java开发实现ATM系统的具体代码,供大家参考,具体内容如下
一.业务分析
通过使用Java面向对象的基础知识,开发一个ATM系统,实现存款,取款,转账,修改密码,注销账户等功能。
二.开发准备
首先,创建一个用户类,为建立用户对象做准备,用户类主要包括用户卡号(系统随机生成),用户名,账户密码,余额,取现额度。并搭建构造器,以及get,set。
public class user { private String cardId ; //卡号 private String username; //用户名 private String password; //密码 private double money; //余额 private double qumoney; //取现额度 public user(String cardId, String username, String password, double qumoney) { this.cardId = cardId; this.username = username; this.password = password; this.qumoney = qumoney; } public user() { } 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 getQumoney() { return qumoney; } public void setQumoney(double qumoney) { this.qumoney = qumoney; } }
三.创建测试类,写入main方法
public class ATMSystem { public static void main(String[] args) { ArrayList<user> users = new ArrayList<>(); Main(users); }
四.将主界面设置成Main方法,设计主界面,包括登录账户,注册账户,并且设置登录方法,注册方法,再调用。
public static void Main(ArrayList<user> users) { System.out.println("==========欢迎进入xx银行ATM系统=========="); while (true) { System.out.println("请选择操作:"); System.out.println("1.登录账户"); System.out.println("2.注册账户"); Scanner sc = new Scanner(System.in); int command = sc.nextInt(); switch (command) { case 1: //登录 denglu(users, sc); break; case 2: //注册 zhuce(users, sc); break; default: System.out.println("功能不存在!"); } } }
运行结果:
五.将注册账户写成方法
public static void zhuce(ArrayList<user> users, Scanner sc) { System.out.println("==========注册账户=========="); String password = ""; String password2 = ""; System.out.println("请输入您的账号名称:"); sc = new Scanner(System.in); String name = sc.next(); while (true) { System.out.println("请输入您的账号密码:"); password = sc.next(); System.out.println("请您再次输入密码:"); password2 = sc.next(); if (password2.equals(password)) { break; } else { System.out.println("两次密码输入不一致!,请重新输入"); } } //生成卡号 System.out.println("请输入账号当次限额"); double qumoney = sc.nextDouble(); String cardId = kahao(users); user u1 = new user(cardId, name, password, qumoney); users.add(u1); System.out.println("恭喜开户成功!,您的卡号是" + u1.getCardId() + ",请您保管"); }
其中,涉及到生成卡号,以及查询系统生成的卡号是否相同,需要再写两个方法:
//生成卡号 public static String kahao(ArrayList<user> users) { while (true) { Random rs = new Random(); String cardId = ""; for (int i = 0; i < 8; i++) { cardId += rs.nextInt(10); } user a = getcardId(cardId, users); if (a == null) { //无重复 return cardId; } } }
//查询卡号 public static user getcardId(String kahao, ArrayList<user> users) { for (int i = 0; i < users.size(); i++) { user a = users.get(i); if (a.getCardId().equals(kahao)) { return a; } } return null; }
六.将登录账户写成方法,并设计主界面
public static void denglu(ArrayList<user> users, Scanner sc) { System.out.println("==========登录账户=========="); if (users.size() == 0) { System.out.println("系统中无账户!请先注册"); return; } while (true) { System.out.println("请输入您的卡号:"); String cardId = sc.next(); String password = ""; user acc = getcardId(cardId, users); if (acc != null) { while (true) { System.out.println("请输入您的密码:"); password = sc.next(); //判断密码是否正确 if (acc.getPassword().equals(password)) { System.out.println("登录成功!,欢迎卡号" + acc.getCardId() + "的贵宾" + acc.getUsername() + "进入系统"); //展示系统登录后的操作界面 jiemian(sc, acc, users); return; } else { System.out.println("密码错误!请重新输入"); } } } else { System.out.println("不存在该卡号!"); } } }
六.1编写 展示主界面方法
public static void jiemian(Scanner sc, user acc, ArrayList<user> users) { while (true) { System.out.println("==========欢迎进入xx银行用户操作界面==========="); 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.注销当前账户"); sc = new Scanner(System.in); int commend = sc.nextInt(); switch (commend) { case 1: atm1(acc); break; case 2: atm2(acc, sc); break; case 3: atm3(acc, sc); break; case 4: atm4(users, acc, sc); break; case 5: atm5(acc, sc); return; case 6: System.out.println("欢迎下次来到ATM系统"); return; case 7: System.out.println("是否确认注销账户?按1确认,按2取消注销"); int tf = sc.nextInt(); if (tf == 1) { users.remove(acc); System.out.println("注销成功!"); return; } else { System.out.println("取消注销"); break; } default: System.out.println("不存在该功能!"); } } }
七.接下来,依次编写各个功能的方法
//查询功能 private static void atm1(user acc) { System.out.println("========当前账户详情========="); System.out.println("卡号:" + acc.getCardId()); System.out.println("姓名:" + acc.getUsername()); System.out.println("余额:" + acc.getMoney()); System.out.println("当次取款限额:" + acc.getQumoney()); } //存款功能 private static void atm2(user acc, Scanner sc) { System.out.println("=========存款操作========="); System.out.println("请您输入存款金额:"); double money = sc.nextDouble(); acc.setMoney(acc.getMoney() + money); System.out.println("存款成功!"); atm1(acc); } //取款 private static void atm3(user 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.getMoney()) { System.out.println("账户余额不足!"); System.out.println("继续取款按1,退出取款按2"); int commend = sc.nextInt(); switch (commend) { case 1: break; case 2: return; } } else if (money > acc.getQumoney()) { System.out.println("超过了当次取款限额!,每次最多可以取" + acc.getQumoney() + "元"); } else { System.out.println("取款成功!"); acc.setMoney(acc.getMoney() - money); atm1(acc); return; } } } } //转账 private static void atm4(ArrayList<user> users, user acc, Scanner sc) { System.out.println("=========转账操作========="); //判断系统中是否有两个以上账户 if (users.size() < 2) { System.out.println("系统中无其他账户,不可以转账!"); return; } else { if (acc.getMoney() == 0) { System.out.println("您的账户余额为0,无法转账"); return; } else { while (true) { System.out.println("请输入对方的卡号:"); String dui = sc.next(); user c = getcardId(dui, users); if (c != null) { //判断账户对象是否为自己 if (c.getCardId().equals(acc.getCardId())) { System.out.println("不能给自己转账!"); } else { //确认对方姓氏 String name = "*" + c.getUsername().substring(1); System.out.println("请您确认[" + name + "]的姓氏"); String xingshi = sc.next(); if (c.getUsername().startsWith(xingshi)) { System.out.println("输入正确!"); while (true) { System.out.println("请您输入转账金额:"); double money = sc.nextDouble(); if (money > acc.getMoney()) { System.out.println("余额不足!"); System.out.println("继续转账按1,退出转账按2"); int commend = sc.nextInt(); switch (commend) { case 1: break; case 2: return; } } else { acc.setMoney(acc.getMoney() - money); c.setMoney(c.getMoney() + money); System.out.println("为" + c.getUsername() + "用户" + "转账成功"); atm1(acc); return; } } } else { System.out.println("对不起,您输入的信息有误!"); } } } else { System.out.println("对不起,您输入卡号有误!"); } } } } } //修改密码操作 private static void atm5(user acc, Scanner sc) { System.out.println("=========修改密码操作========="); while (true) { System.out.println("请输入您的旧密码:"); String oldpassword = sc.next(); String newpassword = ""; if (oldpassword.equals(acc.getPassword())) { while (true) { System.out.println("请输入新密码:"); newpassword = sc.next(); if (newpassword.equals(oldpassword)) { System.out.println("新密码不能与旧密码重复!"); } else { System.out.println("请再次输入密码:"); String okpassword = sc.next(); if (newpassword.equals(okpassword)) { System.out.println("修改密码成功!"); acc.setPassword(newpassword); return; } else { System.out.println("两次密码输入不一致!"); } } } } else{ System.out.println("旧密码输入错误!"); } } }
至此,所有功能完成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)