创建银行卡类,并设计银行卡的两个子类:储蓄卡和信用卡。
时间: 2024-10-12 15:06:57 浏览: 74
BAM银行账户管理系统(java类).pdf
创建银行卡类是一个基础的面向对象编程示例,可以用于模拟金融领域中的卡片服务。下面是一个简单的银行卡类以及其两个子类储蓄卡和信用卡的设计:
```java
// 银行卡类
public class BankAccount {
private String accountNumber;
private String holderName;
private double balance;
// 构造函数、getter和setter省略
// 操作方法,如存款、取款等
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("余额不足");
}
}
}
// 存储卡子类 - 储蓄卡
public class SavingsCard extends BankAccount {
public SavingsCard(String accountNumber, String holderName) {
super(accountNumber, holderName);
}
// 特有的储蓄卡功能,比如利息计算
public double calculateInterest(double annualRate, int months) {
double interest = balance * (annualRate / 12) * months;
return interest;
}
}
// 信用卡子类 - 信用卡
public class CreditCard extends BankAccount {
private int creditLimit;
private boolean isOverlimit;
public CreditCard(String accountNumber, String holderName, int creditLimit) {
super(accountNumber, holderName);
this.creditLimit = creditLimit;
this.isOverlimit = false;
}
// 透支处理
public void charge(double amount) {
if (balance + amount <= creditLimit) {
withdraw(amount);
} else {
isOverlimit = true;
System.out.println("已达到信用额度!");
}
}
// 查看是否超限
public boolean isOverCreditLimit() {
return isOverlimit;
}
}
```
阅读全文