java上机实验五bank
时间: 2023-10-09 07:08:59 浏览: 97
Java上机实验五:银行账户管理系统
本实验要求使用Java面向对象的编程思想,实现一个简单的银行账户管理系统。在该系统中,每个账户都有一个账户号、账户余额、账户利率等信息,可以进行存款、取款、查询余额、计算利息、修改利率等基本操作。
实验要求:
1. 编写一个Account类,该类包含以下成员变量:
- 账户号(id)
- 账户余额(balance)
- 年利率(annualInterestRate)
2. Account类还包含以下方法:
- 无参构造方法
- 带参构造方法,可以为所有成员变量赋值
- 存款(deposit)方法,将指定金额加到账户余额中
- 取款(withdraw)方法,从账户余额中减去指定金额
- 查询余额(getBalance)方法
- 计算利息(getMonthlyInterest)方法,返回当前账户月利息
- 修改年利率(setAnnualInterestRate)方法
3. 编写一个测试类,测试Account类中的各个方法是否正确。
4. 扩展Account类,实现两个子类:
- CheckingAccount类,该类继承Account类,代表一个支票账户,每次取款都会收取一定手续费。
- SavingsAccount类,该类继承Account类,代表一个储蓄账户,每年会发生一次利息结算。
5. 编写一个Bank类,该类用于管理多个账户,实现以下方法:
- addAccount方法,添加一个账户
- removeAccount方法,删除一个账户
- getTotalBalance方法,返回所有账户的总余额
- getAverageBalance方法,返回所有账户的平均余额
- getNumberOfAccounts方法,返回账户的总数
6. 编写一个测试类,测试Bank类中的各个方法是否正确。
提示:
- 可以使用ArrayList来存储所有的账户。
- 在子类中可以使用super关键字来调用父类的构造方法或方法。
- 在SavingsAccount类中,可以使用Calendar类来获取当前的日期和时间,然后判断是否需要进行利息结算。
参考代码:(仅供参考,学生应自主完成实验)
Account类:
```java
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account() {
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getMonthlyInterest() {
return balance * annualInterestRate / 12;
}
}
```
CheckingAccount类:
```java
public class CheckingAccount extends Account {
private double fee;
public CheckingAccount() {
}
public CheckingAccount(int id, double balance, double annualInterestRate, double fee) {
super(id, balance, annualInterestRate);
this.fee = fee;
}
public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;
}
@Override
public void withdraw(double amount) {
super.withdraw(amount);
balance -= fee;
}
}
```
SavingsAccount类:
```java
import java.util.Calendar;
public class SavingsAccount extends Account {
private Calendar lastInterestDate;
public SavingsAccount() {
}
public SavingsAccount(int id, double balance, double annualInterestRate) {
super(id, balance, annualInterestRate);
lastInterestDate = Calendar.getInstance();
}
public void addInterest() {
Calendar now = Calendar.getInstance();
if (now.get(Calendar.MONTH) != lastInterestDate.get(Calendar.MONTH)) {
double interest = getMonthlyInterest() * 12;
balance += interest;
lastInterestDate = now;
}
}
}
```
Bank类:
```java
import java.util.ArrayList;
public class Bank {
private ArrayList<Account> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void addAccount(Account account) {
accounts.add(account);
}
public void removeAccount(Account account) {
accounts.remove(account);
}
public double getTotalBalance() {
double total = 0;
for (Account account : accounts) {
total += account.getBalance();
}
return total;
}
public double getAverageBalance() {
double total = getTotalBalance();
return total / accounts.size();
}
public int getNumberOfAccounts() {
return accounts.size();
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
Account account1 = new Account(1, 1000, 0.01);
account1.deposit(100);
account1.withdraw(200);
System.out.println("Account 1 balance: " + account1.getBalance());
System.out.println("Account 1 monthly interest: " + account1.getMonthlyInterest());
CheckingAccount account2 = new CheckingAccount(2, 1000, 0.01, 10);
account2.withdraw(200);
System.out.println("Account 2 balance: " + account2.getBalance());
System.out.println("Account 2 monthly interest: " + account2.getMonthlyInterest());
SavingsAccount account3 = new SavingsAccount(3, 1000, 0.01);
account3.addInterest();
System.out.println("Account 3 balance: " + account3.getBalance());
System.out.println("Account 3 monthly interest: " + account3.getMonthlyInterest());
Bank bank = new Bank();
bank.addAccount(account1);
bank.addAccount(account2);
bank.addAccount(account3);
System.out.println("Total balance: " + bank.getTotalBalance());
System.out.println("Average balance: " + bank.getAverageBalance());
System.out.println("Number of accounts: " + bank.getNumberOfAccounts());
}
}
```
阅读全文