Java练习题01-存取款

假设一个简单的在ATM的取款过程如下:首先提示用户输入密码(password),
最多只能输入3次,超过3次则提示用户"密码错误,请取卡"交易结束。
如果用户密码正确,再提示用户输入金额(amount),ATM只能输出100元的纸币,一次取钱数要求最低0元,最高1000元。
如果用户输入的金额符合上述要求,则打印输出用户取的钱数,最后提示用户"交易完成,请取卡",否则提示用户重新输入金额。假设用户密码是"111111"。
打印格式:
第一种情况:三次密码输入错误
请输入密码:2
密码错误,请重新输入:3
密码错误,请重新输入:4
密码错误,请取卡
第二种情况:密码输入正确,可以取款
请输入密码:2
密码错误,请重新输入:111111
请输入取款金额:3000
您输入的取款金额有误,请重新输入:999
请取走现金:999元
交易完成,请取卡

import java.util.Scanner;

public class Test8 {
   
    public static void main(String[] args) {
   
        int count = 0;
        boolean b = passWord()
Java基础案例中,关于银行存取款的操作通常会涉及到简单的账户管理系统。我们可以创建一个"Account"类作为基类,它包含账户的基本信息(如账号、余额)以及基本的存款和取款操作。然后,可以创建两个具体的子类,比如"SavingsAccount"(储蓄账户)和"CheckingAccount"(支票账户),它们可能会有额外的功能或利息计算。 以下是一个简化版的银行存取款例子: ```java // 基础账户类 public class Account { private String accountNumber; private double balance; public Account(String accountNumber, double initialBalance) { this.accountNumber = accountNumber; this.balance = initialBalance; } // 存款和取款方法 public void deposit(double amount) { if (amount > 0) { balance += amount; } } public void withdraw(double amount) { if (amount > 0 && balance >= amount) { balance -= amount; } else { System.out.println("Insufficient balance or invalid withdrawal amount."); } } // 获取余额方法 public double getBalance() { return balance; } } // 子类例如:储蓄账户 class SavingsAccount extends Account { // 可能添加年利率属性和利息计算方法 private double interestRate; public SavingsAccount(String accountNumber, double initialBalance, double interestRate) { super(accountNumber, initialBalance); this.interestRate = interestRate; } @Override public void deposit(double amount) { super.deposit(amount); // 计算并累加利息 double interest = amount * interestRate / 12; balance += interest; } } // 支票账户 class CheckingAccount extends Account { // 可能有额外的服务费用或转账限制 private boolean has overdraftProtection; public CheckingAccount(String accountNumber, double initialBalance) { super(accountNumber, initialBalance); has overdraftProtection = false; } public void withdraw(double amount) { // 考虑是否有透支保护 if (!hasOverdraftProtection || balance >= amount) { super.withdraw(amount); } else { System.out.println("Withdrawal not allowed due to insufficient balance."); } } } // 使用示例 public static void main(String[] args) { Account myAccount = new SavingsAccount("123456", 1000, 0.05); // 创建储蓄账户 myAccount.deposit(500); myAccount.withdraw(200); // 更换账户类型 myAccount = new CheckingAccount("789012", 500); // 创建支票账户 // 试图从支票账户提取超出余额的金额,将会提示错误 myAccount.withdraw(700); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值