文章目录
- 📖前言:
- 🎀 思路构建总结:
- 🧸变量信息
- 🏅1. 设计登录交互界面
- 🎇2. 验证账户密码是否正确
- 🪀3. 写出(1.查看存款余额 2.存款3.取款4.退出 5.修改密码)方法
- 🍉4. 再写出一个循环方法(将1,2,3,4,5方法嵌套)
- 🎯源代码(用户)
- 🎯源代码(封装隐藏ChinaBank类信息)
📖前言:
本篇博客主要以介绍入门级java如何实现(简化版)银行登录存取款系统
🎀 思路构建总结:
- 设计登录交互界面
- 验证账户密码是否正确
- 写出(1.查看存款余额 2.存款3.取款4.退出 )方法
- 再写出一个循环方法(将1,2,3,4,5方法嵌套)
5.新增了一个修改密码的功能,代码有略微改动
(注:需要用到静态块。)具体效果如下(下面有代码):
🧸变量信息
static String BankName = "中国人民银行";//
String account = "1234567";//账户
String password = "1314521";//密码
int money = 1000000;//我的银行余额o(* ̄︶ ̄*)o
boolean MY = register();//登录方法返回值判断账户密码是否正确
🏅1. 设计登录交互界面
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,5以便进行操作:");
}
🎇2. 验证账户密码是否正确
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;
}
🪀3. 写出(1.查看存款余额 2.存款3.取款4.退出 5.修改密码)方法
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.err.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;
}
🍉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){//如果密码被修改了,就需要重新登录,但是🎀不是重新创建对象,否则密码会被初始化为1314521
customer.register();
System.out.println("登录成功!");
customer.menu();
customer.xh();
}
/*
* 银行账户:1234567
* 密码:1314521
* */
}
}
🎯源代码(封装隐藏ChinaBank类信息)
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;
}
}