写一个银行活期储蓄项目用java语言
时间: 2024-12-23 15:14:17 浏览: 10
创建一个简单的银行活期储蓄账户的Java项目,通常会包含以下几个关键部分:
1. **账户类 Account**:作为基类,它可能有如下的属性和方法:
- 属性:账户编号(accountNumber),持卡人姓名(cardHolder),初始余额(initialBalance)等。
- 方法:存款(deposit),取款(withdraw),以及显示余额(displayBalance)。
```java
public class Account {
private String accountNumber;
private String cardHolder;
private double balance;
public Account(String accountNum, String cardHolder, double initialBalance) {
this.accountNumber = accountNum;
this.cardHolder = cardHolder;
this.balance = initialBalance;
}
// 其他方法...
}
```
2. **活期储蓄账户类 CheckingAccount**:继承自`Account`,可能会添加一些特定于活期账户的功能,如年费处理。
```java
public class CheckingAccount extends Account {
private double annualFee;
public CheckingAccount(String accountNum, String cardHolder, double initialBalance, double annualFee) {
super(accountNum, cardHolder, initialBalance);
this.annualFee = annualFee;
}
// 实现存款、取款方法,并考虑年费的影响...
}
```
3. **用户界面**:用于操作账户,包括创建新账户、存取款等。
```java
import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CheckingAccount account = new CheckingAccount("123456", "John Doe", 0, 0);
while (true) {
System.out.println("选择操作:1.存款,2.取款,3.查看余额,4.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
account.deposit(scanner.nextDouble());
break;
case 2:
account.withdraw(scanner.nextDouble());
break;
case 3:
System.out.println("当前余额:" + account.displayBalance());
break;
case 4:
System.exit(0); // 结束程序
break;
default:
System.out.println("无效的选择,请重试");
}
}
}
}
```
阅读全文