银行卡是日常生活重要组成部分。每张银行卡信息包含账号、持卡人姓名、身份证号码、地址、存款余额。本任务只要求编写一个方法 showCardInfo (),完成存款、取款、查询。用Java实现
时间: 2024-09-21 11:05:17 浏览: 40
银行卡账户是一个金融实体,在Java中,我们可以创建一个BankAccount类来模拟这个概念,包含相关的属性和方法。这里是一个简单的实现:
```java
public class BankAccount {
private String accountNumber;
private String(holderName);
private String idNumber;
private String address;
private double balance;
// 构造函数
public BankAccount(String accountNumber, String holderName, String idNumber, String address) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.idNumber = idNumber;
this.address = address;
this.balance = 0.0; // 初始余额设为0
}
// 属性访问方法
public String getAccountNumber() { return accountNumber; }
public String getHolderName() { return holderName; }
public String getIdNumber() { return idNumber; }
public String getAddress() { return address; }
public double getBalance() { return balance; }
// 存款方法
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
} else {
System.out.println("无效存款金额");
}
}
// 取款方法
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
} else {
System.out.println("取款金额超过余额");
return false;
}
}
// 查询方法
public void query() {
System.out.printf("账户信息:\n"
+ "账号: %s\n"
+ "户名: %s\n"
+ "身份证号: %s\n"
+ "地址: %s\n"
+ "当前余额: %.2f",
accountNumber, holderName, idNumber, address, balance);
}
// 示例showCardInfo方法
public void showCardInfo() {
System.out.println("显示卡片信息:");
query();
}
}
// 使用示例
public static void main(String[] args) {
BankAccount card = new BankAccount("123456789", "张三", "123456789012345678", "北京市朝阳区");
card.deposit(1000.0);
card.withdraw(500.0);
card.showCardInfo();
}
```
阅读全文