Design steps:
Note: In this code, just build two classes (Bank_Account, BankManage) and fill the method in BankManage.In order to make self-study friends like me accept faster.However, there is still a lot of space for code optimization (for example, to judge the ID legitimacy can be written as a method, because it is to learn to type code, all in the logical language, and then change it when you have time), hopefully the friends you use will blend together.Run in the final AccountTest test class.
Welcome the thinking white whore party and refuse the feticist stretcher party!
1. Overall structure:
(1) Design the Bank_Account class, which stores data, and the BankManage class, which implements it
Note: The code from (3) the main body of the BankManage class is a method that can be used directly and independently. I have not optimized the parts that can be taken out separately from each method, which is the most independent and can be viewed directly (of course, method calls can not be separated from methods, you should understand a little, there are comments)
Catalog:
- Bank_Account class:
- BankManage class body
- Administrator login method
- Main Interface Method
- How to return to the main page
- Method of displaying all accounts
- How to add an account
- Ways to save money
- Ways to get money
- Transfer method
- Method of modifying password
- Method of account cancellation
- AccountTest Test Test Class
2. Implementation Code
(2) Bank_Account class:
package com.chen.bankmanager; public class Bank_Account { String aname; //Account Name float balance; //Account Balance int Password; //Account Password int aid; //account ID Bank_Account[] accounts = new Bank_Account[3]; public void inputaccounts(){ //Show Account Functions, Save Section for Testing for (int i=0;i<accounts.length;i++) { accounts[i]=new Bank_Account(); //Object array must be instantiated before use accounts[i].aname="Deposit account"; accounts[i].aid=i+1; accounts[i].Password=666; accounts[i].balance=0; } } }
(3) BankManage class body (other methods can be filled in one by one):
Insert a code snippet here package com.chen.bankmanager; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner; public class BankManager extends Bank_Account { }
(4) Administrator login method:
//Administrator login page public void registerInterface(){ int password = 666; int i=2; Scanner input =new Scanner(System.in); System.out.println("=============="); System.out.println("Hello, please enter the password before the administrator logs on:"); int inpassword = input.nextInt(); if(inpassword==password){ inputaccounts(); System.out.println("Landing Success"); fristInterface(); }else { do { System.out.println("Password error: Also" + i + "Opportunity"); if(i==0){ System.out.println("System lock!"); break; } inpassword = input.nextInt(); if(inpassword==password){ break; } i--; }while (i>=0); if(inpassword==password) { inputaccounts(); System.out.println("Landing Success"); fristInterface(); //Method of Home Page } } }
(5) Main interface method:
//Main interface public void fristInterface(){ Scanner input=new Scanner(System.in); System.out.println("=============="); System.out.println("0,Show all accounts"); System.out.println("1,Add User"); System.out.println("2,Save money"); System.out.println("3,Draw money"); System.out.println("4,Transfer accounts"); System.out.println("5,Change Password"); System.out.println("6,Seller"); System.out.println("=============="); System.out.println("Please select:"); int x=input.nextInt(); if(x==0) { showAllAccountInfo(); //Method of displaying all accounts }else if(x==1) { addAccountInfo(); //Ways to increase your account }else if(x==2){ saveAccountMoney(); //Ways to save money }else if(x==3){ getMoney(); //Ways to get money }else if(x==4){ transMoney(); //Ways to transfer money }else if(x==5){ updatePwd(); //Password Change Method }else if(x==6){ deletePwd(); //Method of deleting account } }
(6) Ways to return to the main page
public void back(){ Scanner input =new Scanner(System.in); System.out.println("=========================="); System.out.println("Return to user interface 0:"); int x = input.nextInt(); if (x==0) { fristInterface(); } }
(7) Method of displaying all accounts
//Show all account methods public void showAllAccountInfo(){ for(int i=0;i<accounts.length;i++){ if(accounts[i]!=null) { System.out.print("Number:" + (i + 1)); System.out.print(" Account name:" + accounts[i].aname); System.out.print(" account Id: " + accounts[i].aid); System.out.print(" Account Password:" + accounts[i].Password); System.out.print(" Account balance:" + accounts[i].balance); System.out.println(); } } back(); }
(8) Method of adding account
//How to add an account public void addAccountInfo(){ Scanner input =new Scanner(System.in); System.out.println("================"); System.out.println("Please enter a new account ID(Do not enter 1, 2, 3:"); int inputID=input.nextInt(); //Judging legality for (int i=0;i<accounts.length;i++) { if(inputID==accounts[i].aid){ System.out.println("Account already exists, no opening is allowed!Illegal operation, exit program"); System.exit(0); } } System.out.println("Account is legal, continue to execute:"); int flag=0; //Judgement array is very empty //Processing input when array has spaces for (int i=0;i<accounts.length;i++) { if(accounts[i]==null) { flag=1; System.out.println("Please enter the account name:"); accounts[i].aname=input.next(); accounts[i].aid=inputID; System.out.println("Please enter your account password:"); accounts[i].Password=input.nextInt(); System.out.println("Please enter account balance:"); accounts[i].balance=input.nextFloat(); break; } } //When processing has no vacancies if(flag==0) { Bank_Account[] new_accounts= new Bank_Account[accounts.length+20]; System.arraycopy(accounts,0,new_accounts,0,accounts.length); for (int i=accounts.length;i<new_accounts.length;i++) { if (new_accounts[i]==null) { new_accounts[i] = new Bank_Account(); System.out.println("Please enter the account name:"); new_accounts[i].aid = inputID; new_accounts[i].aname = input.next(); System.out.println("Please enter your account password:"); new_accounts[i].Password = input.nextInt(); System.out.println("Please enter account balance:"); new_accounts[i].balance = input.nextFloat(); break; } } accounts=new_accounts; } back(); }
(9) Ways of saving money
//Savings Code public void saveAccountMoney(){ Scanner input= new Scanner(System.in); System.out.println("Please enter an account that needs to be saved ID: "); int intputID=input.nextInt(); int flag=0; int j=1;//Record subscript do { //Whole cycle for (int i = 0; i < accounts.length; i++) { //Determine legality if (intputID == accounts[i].aid) { flag = 1; j = i + 1; break; } } if (flag == 0) { //Error Resolution System.out.println("This account does not exist, please retry(0)Or return(1):"); int x = input.nextInt(); if (x == 0) intputID = input.nextInt(); else if (x==1) { fristInterface(); break; } }else { //Correct Conditions do { System.out.println("Please enter amount:"); float money = input.nextFloat(); if (money<=0){ //Amount Judgment System.out.println("Input amount is illegal!Re-enter:"); }else { System.out.println("Please input a password:"); int Password=input.nextInt(); if (Password==accounts[j-1].Password){ //Password judgment accounts[j-1].balance+=money; System.out.println("The balance is:"+accounts[j-1].balance); back(); }else { System.out.println("Wrong password!You have one more chance:"); Password=input.nextInt(); if(Password!=accounts[j-1].Password){ System.out.println("Wrong password!Close the program!"); System.exit(0); }else { accounts[j-1].balance+=money; System.out.println("The balance is:"+accounts[j-1].balance); back(); } } } }while (j!=0); } }while(j!=0); }
(10) Ways of withdrawing money
//Withdrawal code public void getMoney(){ Scanner input= new Scanner(System.in); System.out.println("Please enter an account to withdraw money ID: "); int intputID=input.nextInt(); int flag=0; int j=1;//Record subscript do { //Whole cycle for (int i = 0; i < accounts.length; i++) { //Determine legality if (intputID == accounts[i].aid) { flag = 1; j = i + 1; break; } } if (flag == 0) { //Error Resolution System.out.println("This account does not exist, please retry(0)Or return(1):"); int x = input.nextInt(); if (x == 0) intputID = input.nextInt(); else if (x==1) { fristInterface(); break; } }else { //Correct Conditions do { System.out.println("Please enter amount:"); float money = input.nextFloat(); if (money>accounts[j-1].balance){ //Amount Judgment System.out.println("Balance left:"+accounts[j-1].balance+" Re-enter:"); }else { System.out.println("Please input a password:"); int Password=input.nextInt(); if (Password==accounts[j-1].Password){ //Password judgment accounts[j-1].balance-=money; System.out.println("The balance is:"+accounts[j-1].balance); back(); }else { System.out.println("Wrong password!You have one more chance:"); Password=input.nextInt(); if(Password!=accounts[j-1].Password){ System.out.println("Wrong password!Close the program!"); System.exit(0); }else { accounts[j-1].balance-=money; System.out.println("The balance is:"+accounts[j-1].balance); back(); } } } }while (j!=0); } }while(j!=0); }
(11) Transfer method
//Transfer code public void transMoney(){ Scanner input=new Scanner(System.in); int j=1; //Initiate Tag Record int l=1; //Accept the following Tag Record int flag01 =0; //Initiation Account Target int flag02 =0; //Target Account System.out.println("Please enter a transfer account first ID:"); int inputID01=input.nextInt(); //Initiating Account do { for (int i=0;i<accounts.length;i++){ //Judging legality if (accounts[i].aid==inputID01) { flag01=1; j=i+1; break; } } if(flag01==1){ System.out.println("For legitimate accounts, please continue to enter the other party's account:"); int inputID02=input.nextInt(); for (int i=0;i<accounts.length;i++){ //Judging legality if (accounts[i].aid==inputID02) { flag02=1; l=i+1; break; } } if(flag02==1){ //Enter amount correctly System.out.println("Please enter amount:"); do{ float money=input.nextFloat(); if(money>accounts[j-1].balance){ System.out.println("Amount over value, remaining:"+accounts[j-1].balance+"Please re-enter:"); }else { //Password Entry in Success Status System.out.println("Please input a password:"); int password=input.nextInt(); if(password!=accounts[j-1].Password){ System.out.println("Wrong password, you have one more chance:"); password=input.nextInt(); if(password!=accounts[j-1].Password){ System.out.println("Password error, program end!"); System.exit(0); } else { //Password Secondary Correct accounts[j-1].balance-=money; accounts[l-1].balance+=money; System.out.println("Account remaining:"+accounts[j-1].balance); System.out.println("Counterparty account remaining:"+accounts[l-1].balance); back(); } }else { //The password is correct once accounts[j-1].balance-=money; accounts[l-1].balance+=money; System.out.println("Account remaining:"+accounts[j-1].balance); System.out.println("Counterparty account remaining:"+accounts[l-1].balance); back(); } } }while (j!=0); }else{ //Target Account Error System.out.println("Account does not exist, please confirm and log in again:"); back(); } }else { System.out.println("Account does not exist, please re-enter (0) or return (1):"); int x= input.nextInt(); if (x==0){ inputID01=input.nextInt(); }else { fristInterface(); } } }while(j!=0); }
(12) Method of modifying password
//Change Password public void updatePwd(){ Scanner input = new Scanner(System.in); System.out.println("Please enter an account that needs to be reclassified ID: "); int inputID = input.nextInt(); int flag=0; int j=1; //Account Subscript do { for (int i = 0; i < accounts.length; i++) { if (inputID == accounts[i].aid) { //ID find flag = 1; j=i+1; break; } } if(flag==0){ System.out.println("Account error, re-enter:"); inputID = input.nextInt(); }else{ //ID The Right Situation System.out.println("Legal account, enter the original password:"); int password01=input.nextInt(); if(password01==accounts[j-1].Password){ //Password correct, enter new password do { System.out.println("Please enter a new password:"); int password02=input.nextInt(); System.out.println("Please enter the new password again:"); int password03=input.nextInt(); if(password02==password03&&password02!=password01){ //Password Check accounts[j-1].Password=password02; System.out.println("Password modified successfully!"); back(); }else if(password02==password01){ System.out.println("The new password cannot be the same as the old password. Please re-enter:"); }else if(password02!=password03){ System.out.println("Passwords are different twice Please re-enter:"); } }while (j!=0); }else{ System.out.println("Password error, program termination!"); System.exit(0); } } }while (j!=0); }
(13) Method of account cancellation
//Log off account code public void deletePwd(){ System.out.println("Please enter an account that needs to be deleted ID: "); Scanner input = new Scanner(System.in); int inputID=input.nextInt(); int flag = 0; int j=1; //Below Tag Record for (int i = 0; i < accounts.length; i++) { //test ID Legality if (inputID == accounts[i].aid) { flag = 1; j=i+1; break; } } if(flag==1){ System.out.println("Legitimate account, please enter the password:"); int password = input.nextInt(); if(password==accounts[j-1].Password) //Password Passed { for(int i=j-1;i<accounts.length-1;i++){ accounts[i]=accounts[i+1]; } accounts=Arrays.copyOf(accounts,accounts.length-1); System.out.println("Delete succeeded"+(j-1)); back(); }else { System.out.println("Wrong password!Program terminated!"); System.exit(0); } }else { System.out.println("Account does not exist!"); back(); } }
(14) AccountTest test class
package com.chen.bankmanager; public class AccountTest extends BankManager{ public static void main(String[] arg){ BankManager bank = new BankManager(); bank.registerInterface(); } }