实现的功能有
1,用户登录
2,用户开户(基于用户登录)
3,查询账户(基于用户登录)
4,存款
5,取款
6,转账
7,修改密码(只有三次确认密码的机会)
8,退出登录
9,注销
这个小例子可以让我们充分复习所学的javase知识
比如:
1,八种基本数据类型以及引用数据类型
2,set,get方法。
3,if语句的判断(对判断条件的掌握)
4,switch语句的熟练运用
6,for循环的使用
7,while分支语句的运用
8,break和continue的理解会更加深刻
9,list集合的使用
1,先建一个Account实体类来存入一下元素
private String cardID;//卡号
private String username;//姓名
private char gender;//性别
private String password;//密码
private double balance;//余额
private double quota;//每次提现额度
package com.itheima.yaoyao; public class Account { private String cardID;//卡号 private String username;//姓名 private char gender;//性别 private String password;//密码 private double balance;//余额 private double quota;//每次提现额度 public Account(String cardID, String username, char gender, String password, double balance, double quota) { this.cardID = cardID; this.username = username; this.gender = gender; this.password = password; this.balance = balance; this.quota = quota; } public Account() { } public String getCardID() { return cardID; } public void setCardID(String cardID) { this.cardID = cardID; } public String getUsername() { return username+(gender == '男' ? "先生" : "女士"); } public void setUsername(String username) { this.username = username; } public char getGender(char gender) { return this.gender; } public void setGender(char gender) { this.gender = gender; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getQuota() { return quota; } public void setQuota(double quota) { this.quota = quota; } }
2,再建一个ATM类来实现具体的业务逻辑
package com.itheima.yaoyao; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class Atm { //定义一个ArrayList,用来存储所有的账户对象 ArrayList<Account> list = new ArrayList<>(); //准备一个键盘录入技术 Scanner scanner = new Scanner(System.in); //定义一个变量,用来记录登录成功的账户 private Account loginAccount; public void start(){ while (true) { System.out.println("==欢迎进入ATM耀耀系统"); System.out.println("1,用户登录"); System.out.println("2,用户开户"); System.out.println("请选择您想要的操作"); int commend = scanner.nextInt(); switch (commend){ case 1 : //用户登录 login(); break; case 2: //用户开户 register(); break; default: System.out.println("对不起,选择的操作有误"); } } } /*用户登录功能*/ private void login() { System.out.println("==用户登录界面=="); //1,判断集合中是否有帐户对象 if (list.size() == 0) { System.out.println("先开户"); register(); return; } //说明系统中有对象 while (true) { //2,让用户输入卡号 System.out.println("请输入你的卡号"); String carId = scanner.next(); //3,判断卡号是否存在 Account acc = getAccountByCarId(carId); //判断得到的帐户对象是否为null if (acc == null) { System.out.println("您输入的卡号不正确请重新输入"); continue; } //说明卡号正确了 //4,让用户输入密码 while (true) { System.out.println("请输入您的密码"); String password = scanner.next(); if (password.equals(acc.getPassword())){ //密码对了 //TDOO操作界面方法 loginAccount = acc; showAllCommands(); return; }else { System.out.println("对不起您输入的密码有误,请重新输入"); } } } } /** * 用户登录成功后的操作页 */ private void showAllCommands() { while (true) { System.out.println("==用户操作界面=="); System.out.println("欢迎"+loginAccount.getUsername()); 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.println("请选择"); int commend = scanner.nextInt(); switch (commend){ case 1: //查询账户 queryAccount(); break; case 2: //存款 deposit(); break; case 3: //取款 withdraw(); break; case 4: //转账 transfer(); break; case 5: //修改密码 boolean s = updatepasswprd(); if (s){ //修改成功 return; } break; case 6: //退出登录 System.out.println("退出成功,欢迎下次光临"); return; case 7: //注销账户 boolean f = deleteAccount(); if (f){ //销户成功 return; } //销户失败 break; default: System.out.println("您输入的命令不存在,重新输入"); } continue; } } /** * 用户修改密码功能 * @return */ private boolean updatepasswprd() { System.out.println("==用户修改密码=="); for (int i = 0; i < 3; i++) { System.out.println("请输入当前密码"); String oldpassword = scanner.next(); if (!oldpassword.equals(loginAccount.getPassword())) { System.out.println("您输入的密码错误您还有"+(2-i)+"次机会"); }else { //旧密码输入正确· while (true){ System.out.println("请输入新密码"); String newpassword = scanner.next(); System.out.println("请再次输入新密码"); String renewpassword = scanner.next(); if (renewpassword.equals(newpassword)) { loginAccount.setPassword(newpassword); System.out.println("密码修改成功"); return true; }else { System.out.println("您两次输入的密码不一致,请重新输入"); } } }} return false; } /** * 用户销户功能 * @return */ private boolean deleteAccount() { while (true) { System.out.println("==用户销户页面=="); //提醒用户是否要销户 System.out.println("您确定要销户吗"); String command = scanner.next(); //判断用户输入的是什么 switch (command) { case "y": //销户 //判断账户中是否还有钱 if (loginAccount.getBalance() != 0) { System.out.println("您的账户还有钱"); return false; } else { //销户 list.remove(loginAccount); System.out.println("销户成功"); return true; } case "n": //不销户 System.out.println("账户保留"); return false; default: System.out.println("输入有误:y/n"); } } } /** * 用户转账功能 */ private void transfer() { System.out.println("==用户转账=="); //1,判断系统中是否有二个或二个以上账户 if (list.size()==1) { System.out.println("当前系统账户不足"); return; } //2,判断账户中是否有钱 if (loginAccount.getBalance()==0) { System.out.println("账户一毛没有"); return; } while (true) { //3,输入转账金额 System.out.println("请输入转账金额"); double money = scanner.nextDouble(); //4,判断是否超过账户余额 if (money > loginAccount.getBalance()) { System.out.println("余额不足最多转"+loginAccount.getBalance()+"元"); continue; } //账户余额充足 while (true) { System.out.println("输入对方账户"); String targetCardId = scanner.next(); Account targetAccount = getAccountByCarId(targetCardId); //先判断卡号是否存在 if (targetAccount == null) { System.out.println("卡号不存在"); continue; } //判断不能给自己转账 if (targetAccount.getCardID().equals(loginAccount.getCardID())) { System.out.println("您不能给自己转账"); continue; } //卡号没问题 //让用户验证对方的姓氏 String targetUsername = targetAccount.getUsername(); System.out.println("请输入*"+targetUsername.substring(1)+"的名字首字母"); String targetname = scanner.next(); if (targetname.equals(targetUsername.substring(0,1))) { //姓氏通过,可以转账 loginAccount.setBalance(loginAccount.getBalance()-money); targetAccount.setBalance(targetAccount.getBalance()+money); System.out.println("转账成功,转账后您的余额为"+loginAccount.getBalance()); return; }else { System.out.println("姓氏验证没通过"); } } } } /** * 用户取款功能 */ private void withdraw() { System.out.println("==用户取款=="); //1,判断用户余额是否大于100元 if (loginAccount.getBalance() < 100) { System.out.println("你的余额不足"); return; } //账户余额大于100 //2,提示用户输入取款金额 while (true) { System.out.println("==用户取款=="); System.out.println("请输入取款金额"); double money = scanner.nextDouble(); //3,判断取款金额有无超过余额 if (loginAccount.getQuota() < money) { System.out.println("对不起您超过限额"); continue; } //4,说明取款金额没有超过限额 //判断余额是否足够 if (loginAccount.getBalance() < money) { System.out.println("对不起余额不足 您的余额为"+loginAccount.getBalance()); continue; } //余额也够,可以取钱 loginAccount.setBalance(loginAccount.getBalance()-money); System.out.println("取款"+money+"元成功你的余额为"+loginAccount.getBalance()); break; } } /** * 用户存款功能 */ private void deposit() { System.out.println("请输入存款金额"); double money = scanner.nextDouble(); //把存的钱加入余额中 loginAccount.setBalance(loginAccount.getBalance()+money); System.out.println("存款后您的余额是"+loginAccount.getBalance()); } /** * 用户查询账户功能 */ private void queryAccount() { System.out.println("查询页面"); System.out.println("您的姓名是"+loginAccount.getUsername()); System.out.println("您的卡号是"+loginAccount.getCardID()); System.out.println("您的余额是"+loginAccount.getBalance()); } /* * 用户开户功能 * */ private void register() { System.out.println("==用户开户界面=="); //1,准备一个账户对象 Account account = new Account(); //让用户输入先关信息 System.out.println("请输入您的姓名"); String username = scanner.next(); account.setUsername(username); while (true){ System.out.println("请输入您的性别"); char gender = scanner.next().charAt(0); if (gender == '男'||gender=='女') { account.setGender(gender); break; }else { System.out.println("对不起您输入的性别有误"); } } while (true){ System.out.println("亲输入您的密码"); String password = scanner.next(); System.out.println("请再次输入您的密码"); String repassword = scanner.next(); if (password.equals(repassword)) { account.setPassword(password); break; }else { System.out.println("对不起您输入的密码重复"); } } System.out.println("亲输入您的提款限额"); double quota = scanner.nextDouble(); account.setQuota(quota); while (true){ //生成卡号alexis String carId = createCarId(); //判断生成的卡号是否重复 Account account1 =getAccountByCarId(carId); if (account1 == null) { //卡号不存在 account.setCardID(carId); break; } } //3,把账户对象添加到集合当中,才算真正开户成功 list.add(account); System.out.println("恭喜你,开户成功,您的卡号是:"+account.getCardID()); } /** * 根据卡号查找对象 * @return carId * 找到的账户对象 */ private Account getAccountByCarId(String carId){ //1,遍历账户集合,判断拿到的账户对象卡号是否是要查找到的 for (int i = 0; i < list.size(); i++) { Account account = list.get(i); if (account.getCardID().equals(carId)) { //找到了 return account; } } //找不到 return null; } /** * 生成1个随机卡号 * @return生成的随即卡号 */ private String createCarId(){ //1,定义一个空字符串,用来存储卡号 String carId = ""; //2,生成八位随机卡号 //先单独生成一位:1-9 Random random = new Random(); carId += random.nextInt(9)+1; //在生成7位1-9 for (int i = 0; i < 7; i++) { carId+=random.nextInt(10); } //返回生成的随机卡号 return carId; } }
3,最后建一个测试来测试一下
package com.itheima.yaoyao; public class Test { public static void main(String[] args) { Atm atm = new Atm(); atm.start(); } }
标签:loginAccount,练习,return,String,ATM,System,println,JAVASE,out From: https://www.cnblogs.com/yaomagician/p/17133352.html