Java银行开户,取钱,存钱,查询余额,退出。。。。。

一:上码

package com.wyj.two;

import java.util.Scanner;

/**
 * 封装的练习
 */
public class Demo8 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Account account = new Account();
        System.out.println("欢迎来到杰哥银行");
       // menu();

        boolean flag = true;
        while (flag) {

            menu();
            System.out.println("请输入你的服务选项:");
            int option = in.nextInt();

            switch (option) {
                case 1: {
                    account.open_account();
                    break;
                }

                case 2: {
                    account.save_money();
                    break;
                }
                case 3: {
                    account.draw_money();
                    break;
                }
                case 4: {
                    account.check();
                    break;
                }
                case 5: {
                    System.out.println("感谢您的使用!!");
                    flag = false;
                    break;
                }
            }

        }

    }

    //菜单栏
    public static void menu() {
        System.out.println("1.设置用户的基本信息(姓名,新建账户,新建密码)");
        System.out.println("2.存钱");
        System.out.println("3.取钱");
        System.out.println("4.查询余额");
        System.out.println("5.退出");
    }

}

class Account {
    private String name;
    private double balance;//余额
    private String acc;//账户
    private String password;//密码
    Scanner in = new Scanner(System.in);

    public void setName(String name) {
        if (name.length() >= 2 && name.length() <= 4)
            this.name = name;
        else {
            System.out.println("请输入正确格式的姓名,默认姓名:无名");
            this.name = "无名";
        }
    }

    public void setBalance(double balance) {
        if (balance > 20)
            this.balance = balance;
        else {
            System.out.println("余额不足,余额默认为:20");
            this.balance = 20;
        }
    }

    public void setPassword(String password) {
        if (password.length() == 6)
            this.password = password;
        else {
            System.out.println("密码格式有误,密码为6为字符,默认为123456");
            this.password = "123456";
        }
    }

    public void setAcc(String acc) {
        this.acc = acc;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public String getPassword() {
        return password;
    }

    public String getAcc() {
        return acc;
    }

    //开户
    public void open_account(){
        System.out.println("请输入姓名:");
        String name = in.next();//不用in.nextLine();他会把一些无效字符给读进去
        this.name = name;


        System.out.println("请输入新建账号:");
        // in.nextLine();//将换行符读掉
        String acc = in.next();
        this.acc =acc;

        System.out.println("请输入新建密码:");
        // in.nextLine();//将换行符读掉
        String pas = in.next();
        this.password = pas;

        System.out.println("恭喜您开户成功!");
    }

    //存钱
    public void save_money() {

        System.out.print("请输入您的账号:");
        String acc1 = in.next();
        System.out.print("请输入您的密码:");
        String pas1 = in.next();
        System.out.print("请输入存钱的金额:");
        double money = in.nextDouble();

        boolean flag = true;
        while (flag) {

            if (acc1.equals(this.acc) && pas1.equals(this.password)) {
                if (money < 0)
                    money = 0;
                this.balance += money;
                System.out.println("存钱成功!");
                flag = false;
            } else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {
                System.out.println("您输入的账号有误!请重新输入您的账号");
                acc1 = in.nextLine();
            } else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {
                System.out.println("您输入的密码有误,请重新输入您的密码");
                pas1 = in.nextLine();
            }
        }

    }

    //取钱
    public void draw_money() {
        System.out.println("请输入您的账号:");
        in.nextLine();//将换行符读掉
        String acc1 = in.next();
        System.out.println("请输入您的密码:");
        in.nextLine();//将换行符读掉
        String pas1 = in.next();
        System.out.println("请输入取钱的金额:");
        double money = in.nextDouble();

        boolean flag = true;
        while (flag) {

            if (acc1.equals(this.acc) && pas1.equals(this.password)) {
                if (money < 0)
                    money = 0;

                if(this.balance < money){
                    System.out.println("对不起您的余额不足!");
                }else{
                    this.balance -= money;
                    System.out.println("取钱成功!");
                }
                flag = false;
            } else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {
                System.out.println("您输入的账号有误!请重新输入您的账号");
                acc1 = in.nextLine();
            } else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {
                System.out.println("您输入的密码有误,请重新输入您的密码");
                pas1 = in.nextLine();
            }
        }
    }

    //查询余额
    public void check() {
        System.out.println("请输入您的账号:");
        String acc1 = in.next();
        System.out.println("请输入您的密码:");
        String pas1 = in.next();

        boolean flag = true;
        while (flag) {

            if (acc1.equals(this.acc) && pas1.equals(this.password)) {

                System.out.println("您的余额为:" + this.balance);

                flag = false;
            } else if (!acc1.equals(this.acc) && pas1.equals(this.password)) {
                System.out.println("您输入的账号有误!请重新输入您的账号");
                acc1 = in.nextLine();
            } else if (acc1.equals(this.acc) && !pas1.equals(this.password)) {
                System.out.println("您输入的密码有误,请重新输入您的密码");
                pas1 = in.nextLine();
            }
        }
    }


    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", balance=" + balance +
                ", password='" + password + '\'' +
                '}';
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
其他极限条件自行测试,有问题直接留言,我速到!!!!!!!!!!!!!!!

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天向上的菜鸡杰!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值