我的Java語言學習日志1_"簡單的銀行管理系統實現"


 

設計步驟:
注意:本篇代碼,直接建兩個類( Bank_Account、BankManage)在BankManage往里面填方法就行。是為了讓和我一樣自學朋友可以更快的接受。但是代碼優化的空間還是很大的(比如判斷ID合法性完全可以寫成一個方法,由於是為了學習打的代碼,全是用的邏輯語,以后有空再改改吧),望使用的朋友可以融匯貫通下。運行在最后的AccountTest測試類中。
歡迎思考型的白嫖黨,拒絕拿來主義的伸手黨!


一、總體架構:
(1)設計 Bank_Account類與BankManage類,前者儲存數據,后者實現方法

注意:從(3)BankManage類主體之后的代碼均為可以直接單獨用的方法,每個方法中可以單獨拿出來的部分我也沒有優化,是最獨立的,可以直接看(當然方法調用是離不開方法的,稍微看下,應該能懂吧,都有注釋)

目錄:

  1. Bank_Account類:
  2. BankManage類主體
  3. 管理員登錄方法
  4. 主界面方法
  5. 返回主頁面的方法
  6. 顯示所有賬戶的方法
  7. 添加賬戶的方法
  8. 存錢的方法
  9. 取錢的方法
  10. 轉賬的方法
  11. 修改密碼的方法
  12. 注銷賬戶的方法
  13. AccountTest測試類

二、實現代碼
(2) Bank_Account類:

package com.chen.bankmanager;

public class Bank_Account {
    String aname;  //賬戶名
    float balance;  //賬戶余額
    int Password; //賬戶密碼
    int aid;   //賬戶ID
    Bank_Account[] accounts = new Bank_Account[3];
    public void inputaccounts(){                //為測試顯示賬戶功能,預存部分
        for (int i=0;i<accounts.length;i++)
        {
            accounts[i]=new Bank_Account();   //對象數組使用前必須實例化
            accounts[i].aname="預存賬戶";
            accounts[i].aid=i+1;
            accounts[i].Password=666;
            accounts[i].balance=0;
        }
    }
}

 

(3)BankManage類主體(其他方法往里面挨個填即可):

在這里插入代碼片
package com.chen.bankmanager;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class BankManager extends Bank_Account {

}

 

(4)管理員登錄方法:

//管理員登錄頁面
public void registerInterface(){
        int password = 666;
        int i=2;
        Scanner input =new Scanner(System.in);
        System.out.println("==============");
        System.out.println("您好,管理員登陸前請輸入密碼:");
        int inpassword = input.nextInt();
        if(inpassword==password){
            inputaccounts();
            System.out.println("登陸成功");
            fristInterface();
        }else {
            do {
                System.out.println("密碼錯誤:還有" + i + "機會");
                if(i==0){
                    System.out.println("系統鎖死!");
                    break;
                }
                inpassword = input.nextInt();
                if(inpassword==password){
                    break;
                }
                i--;
            }while (i>=0);
            if(inpassword==password)
            {
                inputaccounts();
                System.out.println("登陸成功");
                fristInterface();       //主頁面的方法
            }
        }
    }

 



(5)主界面方法:

//主界面
    public void fristInterface(){
        Scanner input=new Scanner(System.in);
        System.out.println("==============");
        System.out.println("0、顯示所有賬戶");
        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("==============");
        System.out.println("請選擇:");
        int x=input.nextInt();
        if(x==0) {
            showAllAccountInfo();       //顯示所有賬戶的方法
        }else if(x==1) {
            addAccountInfo();           //增加賬戶的方法
        }else if(x==2){
            saveAccountMoney();         //存錢的方法
        }else if(x==3){
            getMoney();                   //取錢的方法
        }else if(x==4){
            transMoney();                 //轉錢的方法
        }else if(x==5){
            updatePwd();                   //改密碼的方法
        }else if(x==6){
            deletePwd();                  //刪除賬戶的方法
        }
    }

 



(6)返回主頁面的方法

public void back(){
        Scanner input =new Scanner(System.in);
        System.out.println("==========================");
        System.out.println("返回用戶界面0:");
        int x = input.nextInt();
        if (x==0)
        {
            fristInterface();
        }
    }

 



(7)顯示所有賬戶的方法

//顯示所有賬戶方法
    public void  showAllAccountInfo(){
        for(int i=0;i<accounts.length;i++){
            if(accounts[i]!=null) {
                System.out.print("編號:" + (i + 1));
                System.out.print("     賬戶名:" + accounts[i].aname);
                System.out.print("     賬戶Id:" + accounts[i].aid);
                System.out.print("     賬戶密碼:" + accounts[i].Password);
                System.out.print("     賬戶余額:" + accounts[i].balance);
                System.out.println();
            }
        }
        back();
    }

 



(8)添加賬戶的方法

//添加賬戶的方法
    public void addAccountInfo(){
        Scanner input =new Scanner(System.in);
        System.out.println("================");
        System.out.println("請輸入新賬號ID(請勿輸入1,2,3):");
        int inputID=input.nextInt();
        //判斷合法性
        for (int i=0;i<accounts.length;i++)
        {
            if(inputID==accounts[i].aid){
                System.out.println("已存在賬戶,不允許開戶!非法操作,退出程序");
                System.exit(0);
            }
        }
        System.out.println("賬戶合法,繼續執行:");
        int flag=0;  //判斷數組十分有空位
        //處理數組有空位時的錄入
        for (int i=0;i<accounts.length;i++)
        {
            if(accounts[i]==null)
            {
                flag=1;
                System.out.println("請輸入賬戶名:");
                accounts[i].aname=input.next();
                accounts[i].aid=inputID;
                System.out.println("請輸入賬戶密碼:");
                accounts[i].Password=input.nextInt();
                System.out.println("請輸入賬戶余額:");
                accounts[i].balance=input.nextFloat();
                break;
            }
        }
        //處理沒有空位時
        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("請輸入賬戶名:");
                    new_accounts[i].aid = inputID;
                    new_accounts[i].aname = input.next();
                    System.out.println("請輸入賬戶密碼:");
                    new_accounts[i].Password = input.nextInt();
                    System.out.println("請輸入賬戶余額:");
                    new_accounts[i].balance = input.nextFloat();
                    break;
                }
            }
            accounts=new_accounts;
        }
        back();
    }

 


(9)存錢的方法

//存錢代碼
    public void saveAccountMoney(){
        Scanner input= new Scanner(System.in);
        System.out.println("請輸入需要存錢的賬戶ID:");
        int intputID=input.nextInt();
        int flag=0;
        int j=1;//記錄下標
        do {                             //整體循環
            for (int i = 0; i < accounts.length; i++) {       //確定合法性
                if (intputID == accounts[i].aid) {
                    flag = 1;
                    j = i + 1;
                    break;
                }
            }
            if (flag == 0) {                              //錯誤情況解決
                System.out.println("不存在此賬戶,請重輸(0)或返回(1):");
                int x = input.nextInt();
                if (x == 0)
                    intputID = input.nextInt();
                else if (x==1) {
                    fristInterface();
                    break;
                }
            }else {                                       //正確情況
                do {
                    System.out.println("請輸入金額:");
                    float money = input.nextFloat();
                    if (money<=0){                          //金額判斷
                        System.out.println("輸入金額不合法!重新輸入:");
                    }else {
                        System.out.println("請輸入密碼:");
                        int Password=input.nextInt();
                        if (Password==accounts[j-1].Password){          //密碼判斷
                            accounts[j-1].balance+=money;
                            System.out.println("余額為:"+accounts[j-1].balance);
                            back();
                        }else {
                            System.out.println("密碼錯誤!你還有1次機會:");
                            Password=input.nextInt();
                            if(Password!=accounts[j-1].Password){
                                System.out.println("密碼錯誤!關閉程序!");
                                System.exit(0);
                            }else {
                                accounts[j-1].balance+=money;
                                System.out.println("余額為:"+accounts[j-1].balance);
                                back();
                            }
                        }
                    }
                }while (j!=0);
            }
        }while(j!=0);
    }

 


(10)取錢的方法

//取錢代碼
    public void getMoney(){
        Scanner input= new Scanner(System.in);
        System.out.println("請輸入需要取錢的賬戶ID:");
        int intputID=input.nextInt();
        int flag=0;
        int j=1;//記錄下標
        do {                             //整體循環
            for (int i = 0; i < accounts.length; i++) {       //確定合法性
                if (intputID == accounts[i].aid) {
                    flag = 1;
                    j = i + 1;
                    break;
                }
            }
            if (flag == 0) {                              //錯誤情況解決
                System.out.println("不存在此賬戶,請重輸(0)或返回(1):");
                int x = input.nextInt();
                if (x == 0)
                    intputID = input.nextInt();
                else if (x==1) {
                    fristInterface();
                    break;
                }
            }else {                                       //正確情況
                do {
                    System.out.println("請輸入金額:");
                    float money = input.nextFloat();
                    if (money>accounts[j-1].balance){                          //金額判斷
                        System.out.println("余額僅剩:"+accounts[j-1].balance+"   重新輸入:");
                    }else {
                        System.out.println("請輸入密碼:");
                        int Password=input.nextInt();
                        if (Password==accounts[j-1].Password){          //密碼判斷
                            accounts[j-1].balance-=money;
                            System.out.println("余額為:"+accounts[j-1].balance);
                            back();
                        }else {
                            System.out.println("密碼錯誤!你還有1次機會:");
                            Password=input.nextInt();
                            if(Password!=accounts[j-1].Password){
                                System.out.println("密碼錯誤!關閉程序!");
                                System.exit(0);
                            }else {
                                accounts[j-1].balance-=money;
                                System.out.println("余額為:"+accounts[j-1].balance);
                                back();
                            }
                        }
                    }
                }while (j!=0);
            }
        }while(j!=0);
    }

 


(11)轉賬的方法

//轉賬代碼
    public void transMoney(){
        Scanner input=new Scanner(System.in);
        int j=1;              //發起下標記錄
        int l=1;               //接受下標記錄
        int flag01 =0;                      //發起賬戶目標
        int flag02 =0;                     //目標賬戶
        System.out.println("請先輸入轉賬賬戶ID:");
        int inputID01=input.nextInt();   //發起賬戶
        do {
        for (int i=0;i<accounts.length;i++){   //判斷合法性
            if (accounts[i].aid==inputID01)
            {
                flag01=1;
                j=i+1;
                break;
            }
        }
        if(flag01==1){
            System.out.println("合法賬戶,請繼續輸入對方賬戶:");
            int inputID02=input.nextInt();
            for (int i=0;i<accounts.length;i++){   //判斷合法性
                if (accounts[i].aid==inputID02)
                {
                    flag02=1;
                    l=i+1;
                    break;
                }
            }
            if(flag02==1){                //正確情況下金額輸入
                System.out.println("請輸入金額:");
                do{
                float money=input.nextFloat();
                if(money>accounts[j-1].balance){
                    System.out.println("金額超值,剩余:"+accounts[j-1].balance+"請重新輸入:");
                }else {                                        //成功狀態下密碼輸入
                    System.out.println("請輸入密碼:");
                    int password=input.nextInt();
                    if(password!=accounts[j-1].Password){
                        System.out.println("密碼錯誤,你還有一次機會:");
                        password=input.nextInt();
                        if(password!=accounts[j-1].Password){
                            System.out.println("密碼錯誤,程序結束!");
                            System.exit(0);
                        }
                        else {                         //密碼二次正確情況
                            accounts[j-1].balance-=money;
                            accounts[l-1].balance+=money;
                            System.out.println("賬戶剩余:"+accounts[j-1].balance);
                            System.out.println("對方賬戶剩余:"+accounts[l-1].balance);
                            back();
                        }
                    }else {                    //密碼一次正確的情況
                        accounts[j-1].balance-=money;
                        accounts[l-1].balance+=money;
                        System.out.println("賬戶剩余:"+accounts[j-1].balance);
                        System.out.println("對方賬戶剩余:"+accounts[l-1].balance);
                        back();
                    }
                }
                }while (j!=0);
            }else{                                               //目標賬戶錯誤
                System.out.println("賬戶不存在,請確認后再次登錄:");
                back();
            }
        }else {
            System.out.println("賬戶不存在,請重新輸入(0)或者返回(1):");
            int x= input.nextInt();
            if (x==0){
                inputID01=input.nextInt();
            }else {
                fristInterface();
            }
        }
        }while(j!=0);
    }

 


(12)修改密碼的方法

//修改密碼
    public void updatePwd(){
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入需要改密的賬戶ID:");
        int inputID = input.nextInt();
        int flag=0;
        int j=1;             //賬戶下標
        do {
            for (int i = 0; i < accounts.length; i++) {
                if (inputID == accounts[i].aid) {          //ID找到
                    flag = 1;
                    j=i+1;
                    break;
                }
            }
            if(flag==0){
                System.out.println("賬戶有誤,重新輸入:");
                inputID = input.nextInt();
            }else{             //ID正確的情況
                System.out.println("合法賬戶,輸入原密碼:");
                int password01=input.nextInt();
                    if(password01==accounts[j-1].Password){            //密碼正確,輸入新密碼
                        do {
                            System.out.println("請輸入新密碼:");
                            int password02=input.nextInt();
                            System.out.println("請再次輸入新密碼:");
                            int password03=input.nextInt();
                            if(password02==password03&&password02!=password01){           //密碼核對
                                accounts[j-1].Password=password02;
                                System.out.println("密碼修改成功!");
                                back();
                            }else if(password02==password01){
                                System.out.println("新密碼不能和舊密碼相同,請重新輸入:");
                            }else if(password02!=password03){
                                System.out.println("兩次密碼不同請重新輸入:");
                            }
                        }while (j!=0);
                    }else{
                        System.out.println("密碼錯誤,程序終止!");
                        System.exit(0);
                    }
            }
        }while (j!=0);
    }

 


(13)注銷賬戶的方法

//注銷賬戶代碼
    public void deletePwd(){
        System.out.println("請輸入需要刪除的賬戶ID:");
        Scanner input = new Scanner(System.in);
        int inputID=input.nextInt();
        int flag = 0;
        int j=1;          //下標記錄
        for (int i = 0; i < accounts.length; i++) {         //測試ID合法性
            if (inputID == accounts[i].aid) {
                flag = 1;
                j=i+1;
                break;
            }
        }
        if(flag==1){
            System.out.println("合法賬戶,請輸入密碼:");
            int password = input.nextInt();
            if(password==accounts[j-1].Password)  //密碼通過
            {
                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("刪除成功"+(j-1));
                back();
            }else {
                System.out.println("密碼錯誤!程序終止!");
                System.exit(0);
            }
        }else {
            System.out.println("賬戶不存在!");
            back();
        }

    }

 

(14)AccountTest測試類

package com.chen.bankmanager;

public class AccountTest extends BankManager{
    public static void main(String[] arg){
        BankManager bank = new BankManager();
        bank.registerInterface();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM