zoukankan      html  css  js  c++  java
  • 我的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();
        }
    }
  • 相关阅读:
    「学习笔记」Min25筛
    hash索引
    Thread的setDaemon(true)方法的作用
    Reactor模式详解
    题目整理
    jstat命令查看jvm的GC情况 (以Linux为例)
    jvm参数调优
    以网游服务端的网络接入层设计为例,理解实时通信的技术挑战
    Java 工程师成神之路
    ID生成 雪花算法
  • 原文地址:https://www.cnblogs.com/chentianxingdeblog/p/12155335.html
Copyright © 2011-2022 走看看