4. 再写出一个循环方法(将1,2,3,4,5方法嵌套)
public void xh() {//循环方法Scanner sc = new Scanner(System.in);out:while (MY) {System.out.print("请输入选项以便进行操作:");int input1 = sc.nextInt();switch (input1) {case 1:check();break;case 2:reserve();break;case 3:moveMoney();break;case 4:System.out.println("希望本次交易愉快!再见~");break out;case 5:if(updatePassword()){System.out.println("用户信息发生变动,请重新登录");flag=true;break out;}else {break;}default:System.out.println("输入错误,请重新输入:");continue out;}}}
源代码(用户)
package Day02.Bank;public class AccessBank {public static void main(String[] args) {ChinaBank customer = new ChinaBank();customer.xh();while (ChinaBank.flag){//如果密码被修改了,就需要重新登录,但是不是重新创建对象,否则密码会被初始化为1314521customer.register();System.out.println("登录成功!");customer.menu();customer.xh();}/** 银行账户:1234567* 密码:1314521* */}}
源代码(封装隐藏类信息)
【利用Java基础知识+简单封装写一个银行登录存取款系统】package Day02.Bank;import java.util.Scanner;public class ChinaBank {static String BankName = "中国人民银行";//String account = "1234567";String password = "1314521";int money = 1000000;boolean MY = register();//这个MY是记录账户是否登录成功static boolean flag=false;//flag记录是否密码被修改了,需要重新登录static {System.out.println("————————欢迎来到" + BankName + "——————————");System.out.println();}public ChinaBank() {//界面System.out.println("————————欢迎来到" + BankName + "——————————");System.out.println();System.out.println("\t\t1.查看余额");System.out.println("\t\t2.存款");System.out.println("\t\t3.取款");System.out.println("\t\t4.退出");System.out.println("\t\t5.修改密码");System.out.println("\t\t请输入选项1,2,3,4,5以便进行操作:");}public void menu(){System.out.println("————————欢迎来到" + BankName + "——————————");System.out.println();System.out.println("\t\t1.查看余额");System.out.println("\t\t2.存款");System.out.println("\t\t3.取款");System.out.println("\t\t4.退出");System.out.println("\t\t5.修改密码");System.out.println("\t\t请输入选项1,2,3,4以便进行操作:");}public void xh() {//循环方法Scanner sc = new Scanner(System.in);out:while (MY) {System.out.print("请输入选项以便进行操作:");int input1 = sc.nextInt();switch (input1) {case 1:check();break;case 2:reserve();break;case 3:moveMoney();break;case 4:System.out.println("希望本次交易愉快!再见~");MY=false;flag=false;//密码没被修改break out;case 5:if(updatePassword()){System.out.println("用户信息发生变动,请重新登录");flag=true;break out;}else {break;}default:System.err.println("输入错误,请重新输入:");continue out;}}}public boolean register() {//登录方法Scanner sc = new Scanner(System.in);do {System.out.print("请输入银行账户:");String account1 = sc.next();System.out.print("请输入账户密码:");String password1 = sc.next();if (this.account.equals(account1) && this.password.equals(password1))break;else System.out.println("账户或密码错误,请重新输入:");} while (true);return true;}public void check() {//查看储存款方法System.out.println("您目前所剩余额为:" + money);}//方法1.查看余额public void reserve() {//方法2.储存款方法Scanner sc = new Scanner(System.in);System.out.println("您目前所剩余额为:" + money);System.out.print("请输入存款金额:");int reserve1 = sc.nextInt();money += reserve1;System.out.println("存款成功!您目前所剩余额为:" + money);}public void moveMoney() {//方法3.取款方法Scanner sc = new Scanner(System.in);System.out.println("您目前所剩余额为:" + money);System.out.print("请输入取款金额:");while (MY) {int moveMoney1 = sc.nextInt();if (moveMoney1 > money) {System.out.print("余额不足,请重新取款:");continue;} else {money -= moveMoney1;System.out.println("取款成功!您目前所剩余额为:" + money);break;}}}public boolean updatePassword(){//方法5:修改密码Scanner sc=new Scanner(System.in);System.out.println("请输入旧密码:");String password1=sc.next();if(password1.equals(password)){//如果密码正确,才能修改密码\System.out.print("请输入新密码:");String temp1=sc.next();System.out.print("\n请再次输入新密码:");String temp2=sc.next();if(temp1.equals(temp2)){password=temp1;System.out.println("密码修改成功!");return true;}System.err.println("\n新密码两次输入不一致,此次修改密码作废!");}else {System.err.println("密码验证错误!");}return false;}}
- java实现银行简单业务
- 书柜顶部空间怎么利用,入户门后边空间的利用
- java地图图表动态亮点_动态展示地图和图表
- android自动生成dimens适配文件,无需Java工具类
- 为了实现对象的克隆,java 对象 克隆
- java开发150个建议
- 男子吐槽22元大碗面一口就能吃完,要求换碗或加面被拒绝,店方:用大碗是想利用网红效应,面量没变 吉尼斯记录大碗面
- 中啡咖啡怎么样,java中咖啡标志怎么设置大小
- 利用节假日外出游览:揭秘古代文人养生七法
- 前端技巧|利用CSS属性修改图片颜色