银行系统1.0

来源:互联网 发布:万虎网络 编辑:程序博客网 时间:2024/06/05 02:11

package bankSystem1_0;import java.util.Scanner;public class BankSys {public static void main(String[] args) {test();}private static void test() {// TODO Auto-generated method stubString[] name = new String[5];String[] pwd = new String[5];double[] money = new double[5];int re = 0;boolean flag = false;Scanner sc = new Scanner(System.in);// 用户1name[0] = "jack";pwd[0] = "123456";money[0] = 3000;// 用户2name[1] = "bob";pwd[1] = "1234";money[1] = 1000;System.out.println("欢迎来到银行系统");while (true) {System.out.println("1.登录  2.注册   3.粗款  4.取款   5.转账   6.注册");System.out.println("请输入你的操作代码");re = sc.nextInt();switch (re) {case 1:System.out.println("当前操作---->登录");Scanner scLogin = new Scanner(System.in);System.out.println("请输入用户名:");String n = scLogin.nextLine();System.out.println("请输入密码:");String p = scLogin.nextLine();for (int i = 0; i < name.length; i++) {if (n.equals(name[i]) && p.equals(pwd[i])) {// 判断用户名正确时,标志位为trueflag = true;break;}}// 判断标志位为true时提示登陆成功,否则登录失败if (flag) {System.out.println(n + "用户登录成功!");} elseSystem.out.println("登录失败!");break;case 2:System.out.println("当前操作---->注册");Scanner scRegis = new Scanner(System.in);// 判断数组内容为空的下标for (int i = 0; i < name.length; i++) {if (name[i] == null) {// 当下标为空时即可放入注册的信息System.out.println("请输入注册的用户名:");name[i] = scRegis.nextLine();System.out.println("请输入注册的密码:");pwd[i] = scRegis.nextLine();System.out.println("您注册的用户名为:" + name[i] + ", 密码为:"+ pwd[i]);break;}}break;case 3:System.out.println("当前操作---->存款");break;case 4:System.out.println("当前操作---->取款");break;case 5:System.out.println("当前操作---->转账");break;case 6:System.out.println("当前操作---->退出");// 强制退出System.exit(0);break;default:break;}System.out.println("是否继续:y/n");String strFlag = sc.next();// 判断是否继续,当输入为n时,跳出循环if (strFlag.equals("n")) {break;}}}}

Bank.java

package bankSystem1_0;import java.util.Scanner;public class Bank {String[] name = new String[5];String[] pwd = new String[5];double[] money = new double[5];boolean isLoginSuccess = false;int currentIndex = -1;public void init() {// 用户1name[0] = "jack";pwd[0] = "123456";money[0] = 3000;// 用户2name[1] = "bob";pwd[1] = "1234";money[1] = 1000;System.out.println("**********欢迎来到银行操作系统:**********");}public void menu() {System.out.println("1.登录  2.注册   3.存款  4.取款   5.转账   6.注册");System.out.println("请输入你的操作代码");Scanner sc = new Scanner(System.in);int re = sc.nextInt();switch (re) {case 1:System.out.println("当前操作------->登录");login();break;case 2:regist();break;case 3:save();break;case 4:withDraw();break;case 5:transMoney();break;case 6:exit();break;default:break;}}public void login() {Scanner scLogin = new Scanner(System.in);System.out.println("请输入用户名:");String n = scLogin.nextLine();System.out.println("请输入密码:");String p = scLogin.nextLine();for (int i = 0; i < name.length; i++) {if (n.equals(name[i]) && p.equals(pwd[i])) {// 判断用户名正确时,标志位为trueisLoginSuccess = true;currentIndex = i;break;}}// 判断标志位为true时提示登陆成功,否则登录失败if (isLoginSuccess) {System.out.println(n + "用户登录成功!");menu();} else {System.out.println("登录失败!");menu();}}public void regist() {System.out.println("当前位置------>注册");Scanner scRegis = new Scanner(System.in);// String regisPwd;// 判断数组内容为空的下标for (int i = 0; i < name.length; i++) {if (name[i] != null) {// 当下标为空时即可放入注册的信息System.out.println("请输入注册的用户名:");name[i] = scRegis.nextLine();// boolean flagLogin = false;System.out.println("请输入注册的密码:");pwd[i] = scRegis.nextLine();System.out.println("请输入开户金额:");money[i] = scRegis.nextDouble();System.out.println("您的注册信息为:\n用户名:" + name[i] + "\n 密码:"+ pwd[i] + "\n开户金额为:" + money[i]);break;}}System.out.println("是否继续:y/n");String isGo = scRegis.next();if (isGo.equals("y")) {menu();} elsemenu();}public void save() {Scanner scSave = new Scanner(System.in);if (!isLoginSuccess) {System.out.println("请先登录:");login();} else {System.out.println("当前位置------>存款");System.out.println("请输入存款金额:");double inputMoney = scSave.nextDouble();money[currentIndex] += inputMoney;System.out.println("存款余额为:" + money[currentIndex]);menu();}}public void withDraw() {Scanner scWithDraw = new Scanner(System.in);if (!isLoginSuccess) {System.out.println("请先登录:");login();} else {System.out.println("当前位置------>取款");System.out.println("请输入取款金额:");double withMoney=scWithDraw.nextDouble();if (withMoney>money[currentIndex]) {System.out.println("金额不够,请重新输入");withDraw();}else{System.out.println("您的余额为:"+(money[currentIndex]-withMoney));menu();}}}public void transMoney() {System.out.println("当前位置------>转账");}public void exit() {System.out.println("银行系统已经退出");}}
TestBank.java

package bankSystem1_0;public class BankTest {public static void main(String[] args) {Bank bank=new Bank();bank.init();bank.menu();}}

转账代码:

public void tran() {String na;int index = 0;double mo;boolean flag = false;Scanner scTran = new Scanner(System.in);if (!isLoginSucc) {System.out.println("请先登录");} else {System.out.println("请输入对方用户名:");na = scTran.next();for (int i = 0; i < name.length; i++) {while (na.equals(name[i])) {System.out.println("你输入的用户名错误");break;}break;}}System.out.println("请输入转账金额");mo = scTran.nextDouble();money[index] += mo;System.out.println("转账成功");menu();}

输入转账的用户错误时,系统提示:输入用户名错误,并跳出循环。当输入的用户名正确时,继续提示输入转账金额

System.out.println("请输入对方用户名:");na = scTran.next();for (int i = 0; i < name.length; i++) {while (na.equals(name[i])) {System.out.println("你输入的用户名错误");break;}break;}




原创粉丝点击