java银行存取款系统类
时间: 2023-12-08 16:39:45 浏览: 211
以下是一个简单的Java银行存取款系统类的实现,包括存款、取款、转账和余额查询等功能:
```java
public class Bank {
private String accountNumber; // 账号
private String password; // 密码
private double balance; // 余额
// 构造函数
public Bank(String accountNumber, String password, double balance) {
this.accountNumber = accountNumber;
this.password = password;
this.balance = balance;
}
// 存款
public void deposit(double amount) {
balance += amount;
System.out.println("成功存入 " + amount + " 元,当前余额为 " + balance + " 元。");
}
// 取款
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("成功取出 " + amount + " 元,当前余额为 " + balance + " 元。");
} else {
System.out.println("余额不足,无法取款。");
}
}
// 转账
public void transfer(Bank other, double amount) {
if (balance >= amount) {
balance -= amount;
other.balance += amount;
System.out.println("成功转出 " + amount + " 元,当前余额为 " + balance + " 元。");
} else {
System.out.println("余额不足,无法转账。");
}
}
// 余额查询
public void checkBalance() {
System.out.println("当前余额为 " + balance + " 元。");
}
}
```
阅读全文