【Java基础】简单的银行管理系统

简单的银行管理系统

(新手代码、未连接数据库、大神请移步)

知识点:
				1、对象的使用
				2、自定义异常处理
				3、if  else语句 /  while语句
				4、参数、对象的引用等
效果图

提示开户
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

代码(自行导包 alt+enter)

Menu类
public class Menu {
    public static void main(String[] args)throws AllException, StopException {
        int flag=0;        //  记录用户是否开户(相当于判断用户是否登录)
        Scanner input = new Scanner(System.in);
        Users menu = new Users();

        // System.out.println(menu.toString());
        while (true)
        {
            System.out.println("**************************");
            System.out.println("\t银行管理系统");
            System.out.println("\t***********");
            System.out.println("    1.开户\t2.存钱");
            System.out.println("    3.取钱\t4.退出");
            System.out.print("请输入选项:");
            String choose;
            try
            {
                choose = input.next();
                if (choose.equals("1"))
                {
                    menu = menu.creat();
                    flag=1;
                }
                else if (choose.equals("2"))
                {
                    // 已经开户(登录)
                    if (flag==1)
                    {
                        menu.save(menu);

                    }
                    else
                    {
                        //  没有开户(登录)
                        throw new AllException("账户不存在, 请选择“1”开户!\n");
                    }
                }
                else if (choose.equals("3"))
                {
                    if (flag==1)
                    {
                        menu.draw(menu);
                    }
                    else
                    {
                        throw new AllException("账户不存在, 请选择“1”开户!\n");
                    }

                }
                else if (choose.equals("4"))
                {
                    System.out.println("欢迎下次光临!");
                    break;
                }
                else
                {
                    throw new AllException("您输入的序号不存在,请重新输入!\n");
                }
            }
            catch(AllException e)
            {
                System.out.println(e.getMessage());
            }

        }

    }
    
}

User类
import java.util.Scanner;

public class Users{

    String usercount;
    String name ;
    String passwd;
    int money;



    BankSystem user1 = new BankSystem();  //创建银行的对象
    Scanner input = new Scanner(System.in);

    // 开户(注册)
    public Users creat() throws AllException,StopException{

        //  写入数据库  初始化
        System.out.print("账号:");
        usercount = input.next();
        System.out.print("姓名:");
        name = input.next();
        System.out.print("密码:");
        passwd = input.next();
        money = 10;

        int numcount=3;
        boolean flag = user1.CreatCount(usercount,passwd,money);
        numcount--;
        //judge(flag, numcount);
        while (true)
        {
            try
            {
                if (flag==true)
                {
                    break;
                }
                else if (flag==false)
                {
                    throw new AllException("开户金额不足,请继续支付:");
                }

                if (numcount<=0)
                {
                    throw new StopException("账号或密码错误次数超过三次,已锁定账号!");
                }
            }
            catch(AllException e)
            {
                System.out.println(e.getMessage());
                money = input.nextInt();
                flag = user1.CreatCount(usercount,passwd,money);
                numcount--;
            }
            catch(StopException e)
            {
                System.out.println(e.getMessage());
                break;
            }
        }

        return new Users(usercount,name,passwd,money);
    }

    //存钱
    public void save(Users menu) throws AllException, StopException {

        if ( menu.name == null){
            System.out.println("未登录");
        }else {
            System.out.println("---------欢迎您, "+name+"---------");
        }
        //   可连接数据库校验
        System.out.println("存钱前,请输入账号密码和金额~~");
        System.out.print("账号:");
        String usercount1 = input.next();
        System.out.print("密码:");
        String passwd1 = input.next();
        System.out.print("金额:");
        int money1 = input.nextInt();
        int numcount=3;

       // System.out.println(menu.usercount+menu.passwd);

        boolean flag = user1.SaveMoney(usercount1,passwd1,money1,menu);
        numcount--;
        while (true)
        {
            try
            {
                if (flag==true)
                {
                    break;
                }
                else if (flag==false)
                {
                    if (numcount<=0)
                    {
                        throw new StopException("账号或密码错误次数超过三次,已锁定账号!");
                    }
                    throw new AllException("账号或密码错误,剩余输入次数"+numcount+"次,请重新输入");
                }

            }
            catch(AllException e)
            {
                System.out.println(e.getMessage());
                numcount--;
                System.out.print("账号:");
                usercount1 = input.next();
                System.out.print("密码:");
               passwd1 = input.next();
                System.out.print("金额:");
               money1 = input.nextInt();
                flag = user1.SaveMoney(usercount1, passwd1, money1,menu);
            }
            catch(StopException e)
            {
                System.out.println(e.getMessage());
                break;
            }
        }
    }

    //取钱
    public void draw(Users menu) throws AllException, StopException {
        //BankSystem user1 = new BankSystem(name);
        System.out.println("取钱前,请输入账号密码和金额##");
        System.out.print("账号:");
        String usercount = input.next();
        System.out.print("密码:");
        String passwd = input.next();
        System.out.print("金额:");
        int money = input.nextInt();
        int numcount=3;
        boolean flag = user1.DrawMoney(usercount,passwd,money,menu);
        numcount--;
        while (true)
        {
            try
            {
                if (flag==true)
                {
                    break;
                }
                else if (flag==false)
                {
                    if (numcount<=0)
                    {
                        throw new StopException("账号或密码错误次数超过三次,已锁定账号!");
                    }
                    throw new AllException("账号或密码错误,剩余输入次数"+numcount+"次,请重新输入");

                }

            }
            catch(AllException e)
            {
                System.out.println(e.getMessage());
                numcount--;
                System.out.print("账号:");
                usercount = input.next();
                System.out.print("密码:");
                passwd = input.next();
                System.out.print("金额:");
                money = input.nextInt();
                flag = user1.DrawMoney(usercount, passwd, money,menu);
            }
            catch(StopException e)
            {
                System.out.println(e.getMessage());
                break;
            }
        }
    }


    public Users(String usercount, String name, String passwd, int money) {
        this.usercount = usercount;
        this.name = name;
        this.passwd = passwd;
        this.money = money;
    }

    public Users() {
    }

    public Scanner getInput() {
        return input;
    }

    public void setInput(Scanner input) {
        this.input = input;
    }

    public String getUsercount() {
        return usercount;
    }

    public void setUsercount(String usercount) {
        this.usercount = usercount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public BankSystem getUser1() {
        return user1;
    }

    public void setUser1(BankSystem user1) {
        this.user1 = user1;
    }


    @Override
    public String toString() {
        return "Users{" +
                "input=" + input +
                ", usercount='" + usercount + '\'' +
                ", name='" + name + '\'' +
                ", passwd='" + passwd + '\'' +
                ", money=" + money +
                ", user1=" + user1 +
                '}';
    }
}
BankSystem类
import java.util.Scanner;

public class BankSystem {
    int money=0;
    String usercount;
    String passwd;

   //开户业务
    public boolean CreatCount(String usercount1, String passwd1, int money1){
        usercount = usercount1;
        passwd = passwd1;
        money += money1;

        if (money>=10)
        {
            //  开户费用10元
            money -= 10;
            System.out.println("开户成功, 账户余额为:"+money);
            return true;
        }
        else
        {

            System.out.println("开户金额的10元, 您支付的钱币的为"+money+"元, 还差"+(10-money)+"元。");
            return false;

        }
    }
    //存钱业务
    public boolean SaveMoney(String usercount1, String passwd1, int money1,Users menu){



         // 验证账号和密码   (可数据库查询)
        if (usercount1.equals(menu.usercount)&&passwd1.equals(menu.passwd))
        {
            money += money1;
            System.out.println(menu.name+"您好, 您的账号已存入"+money1+", 当前余额为:"+money+"元。");
            return true;
        }
        else
            return false;

    }
    //取钱业务
    public boolean DrawMoney(String usercount1,String passwd1, int money1,Users menu){

        // 验证账号和密码   (可数据库查询)
        if (usercount1.equals(menu.usercount)&&passwd1.equals(menu.passwd))
        {
            if (money1<=money)
            {
                money -= money1;
                System.out.println(menu.name+"您好, 您的账号已取出"+money1+", 当前余额为:"+money+"元。");
                return true;
            }
            else
            {
                System.out.println("余额不足,当前余额为"+money+"元,请重新输入取出金额:");
                while (money1>money)
                {
                    Scanner input = new Scanner(System.in);
                    money1 = input.nextInt();
                }
                money -= money1;
                System.out.println(menu.name+"您好, 您的账号已取出"+money1+", 当前余额为:"+money+"元。");
                return true;

            }
        }
        else
            return false;
    }
}

AllException
public class AllException extends Exception{
    public AllException(String msg)
    {
        super(msg);  
    }

}
StopException
public class StopException extends Exception {
    public StopException(String msg) {
        super(msg);
    }
}

参考文章:https://blog.csdn.net/changjiale110/article/details/78701989

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值