Java language programming:设计一个银行业务类

题目: 编写一个银行业务类BankBusiness,具有以下属性和方法:
(1)公有、静态的属性:银行名称bankName,初始值为“中国银行”。
(2)私有属性:账户名name、密码password、账户余额balance。
(3)银行对用户到来的欢迎(welcome)动作(静态、公有方法),显示“中国银行欢迎您的到来!”,其中“中国银行”自动使用bankName的值。
(4)银行对用户离开的提醒(welcomeNext)动作(静态、公有方法),显示“请收好您的证件和物品,欢迎您下次光临!”
(5)带参数的构造方法,完成开户操作。参数:账户名、密码。开户时的账户余额为0。
(6)用户的存款(deposit)操作(公有方法,参数:密码、交易额),密码不对时无法存款且提示“您的密码错误!”;密码正确、完成用户存款操作后,要提示用户的账户余额,例如“您的余额有1000.0元。”。
(7)用户的取款(withdraw)操作(公有方法,参数:密码、交易额)。密码不对时无法取款且提示“您的密码错误!”;密码正确但余额不足时提示“您的余额不足!”;密码正确且余额充足时扣除交易额并提示用户的账户余额,例如“请取走钞票,您的余额还有500.0元。”。

编写一个测试类Main,在main方法中,先后执行以下操作:
(1)调用BankBusiness类的welcome()方法。
(2)接收键盘输入的用户名、密码信息作为参数,调用BankBusiness类带参数的构造方法,从而创建一个BankBusiness类的对象account。
(3)调用account的存款方法,输入正确的密码,存入若干元。密码及存款金额从键盘输入。
(4)调用account的取款方法,输入错误的密码,试图取款若干元。密码及取款金额从键盘输入。
(5)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额大于余额)。密码及取款金额从键盘输入。
(6)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额小于余额)。密码及取款金额从键盘输入。
(7)调用BankBusiness类的welcomeNext()方法。

输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额

输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!

输入样例:
在这里给出一组输入。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:
张三 123456
123456 1000
654321 2000
123456 2000
123456 500

输出样例:
在这里给出相应的输出。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:
中国银行欢迎您的到来!
您的余额有1000.0元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有500.0元。
请收好您的证件和物品,欢迎您下次光临!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        BankBusiness.welcome();
        
        System.out.print("请输入开户需要的姓名:");
        String name = scanner.nextLine();
        System.out.print("请输入开户需要的密码:");
        String password = scanner.nextLine();

        scanner.nextLine();

        BankBusiness account = new BankBusiness(name, password);

        System.out.print("请输入正确的密码:");
        String depositPassword = scanner.nextLine();
        System.out.print("请输入存款金额:");
        double depositAmount = scanner.nextDouble();
        account.deposit(depositPassword, depositAmount);
        scanner.nextLine();

        System.out.print("请输入错误的密码:");
        String withdrawPasswordWrong = scanner.nextLine();
        System.out.print("请输入取款金额:");
        double withdrawAmountWrong = scanner.nextDouble();
        account.withdraw(withdrawPasswordWrong, withdrawAmountWrong);
        scanner.nextLine();

        System.out.print("请输入正确的密码:");
        String withdrawPasswordCorrect = scanner.nextLine();
        System.out.print("请输入大于余额的取款金额:");
        double withdrawAmountMoreThanBalance = scanner.nextDouble();
        account.withdraw(withdrawPasswordCorrect, withdrawAmountMoreThanBalance);
        scanner.nextLine();

        System.out.print("请输入正确的密码:");
        String withdrawPasswordCorrectAgain = scanner.nextLine();
        System.out.print("请输入小于余额的取款金额:");
        double withdrawAmountLessThanBalance = scanner.nextDouble();
        account.withdraw(withdrawPasswordCorrectAgain, withdrawAmountLessThanBalance);
        scanner.nextLine();

        BankBusiness.welcomeNext();
    }
}
class BankBusiness {
    public static String bankName = "中国银行";
    private String name;
    private String password;
    private double balance;
    public BankBusiness(String name, String password) {
        this.name = name;
        this.password = password;
        this.balance = 0.0;
    }
    public static void welcome() {
        System.out.println(bankName + "欢迎您的到来!");
    }
    public static void welcomeNext() {
        System.out.println("请收好您的证件和物品,欢迎您下次光临!");
    }

    public void deposit(String password, double amount) {
        if (!this.password.equals(password)) {
            System.out.println("您的密码错误!");
            return;
        }
        balance += amount;
        System.out.println("您的余额有" + balance + "元。");
    }

    public void withdraw(String password, double amount) {
        if (!this.password.equals(password)) {
            System.out.println("您的密码错误!");
            return;
        }
        if (amount > balance) {
            System.out.println("您的余额不足!");
            return;
        }
        balance -= amount;
        System.out.println("请取走钞票,您的余额还有" + balance + "元。");
    }

    public void showBalance() {
        System.out.println("您的余额有" + balance + "元。");
    }
}
  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Surgeon`

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

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

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

打赏作者

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

抵扣说明:

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

余额充值