BankAccount类的成员变量: 账户名、账号、余额、账户等级 用枚举类型BankAccountGrade表示账户的等级。共有两种等级: VIP和GENERAL,给出代码示例
时间: 2024-10-16 07:05:35 浏览: 50
在Java中,我们可以创建一个名为`BankAccount`的类,包含指定的成员变量,其中账户等级使用`enum`类型来表示。以下是详细的代码示例:
```java
public class BankAccount {
private String accountName;
private String accountNumber;
private double balance;
private BankAccountGrade accountGrade;
// 定义账户等级枚举类型
public enum BankAccountGrade {
VIP, // 贵宾级别
GENERAL // 普通级别
}
// 构造函数
public BankAccount(String name, String number, double initialBalance, BankAccountGrade grade) {
this.accountName = name;
this.accountNumber = number;
this.balance = initialBalance;
this.accountGrade = grade;
}
// 获取和设置方法
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance");
}
}
public BankAccountGrade getAccountGrade() {
return accountGrade;
}
public void setAccountGrade(BankAccountGrade accountGrade) {
this.accountGrade = accountGrade;
}
// 示例:VIP账户额外功能
public boolean isPremiumFeatureAvailable() {
return accountGrade == BankAccountGrade.VIP;
}
// 用于演示目的 - 打印账户信息
@Override
public String toString() {
return "Account Name: " + accountName + ", Account Number: " + accountNumber +
", Balance: " + balance + ", Grade: " + accountGrade;
}
}
// 使用示例
BankAccount generalAccount = new BankAccount("John Doe", "123456", 1000, BankAccountGrade.GENERAL);
System.out.println(generalAccount); // 输出普通账户的信息
BankAccount vipAccount = new BankAccount("Jane Smith", "789012", 5000, BankAccountGrade.VIP);
System.out.println(vipAccount); // 输出贵宾账户的信息
```
在这个例子中,`BankAccount`类包含了账户的基本属性和操作,如名称、号码、余额以及账户等级。VIP账户通过`isPremiumFeatureAvailable()`方法提供了额外的功能。
阅读全文