2、 设计一个Account类表示账户,自行设计该类中的属性和方法,并利用这个类创建一个账号为998866,余额为2000,年利率为4.5%的账户,然后从该账户中存入150,取出1500。打印出帐号、余额、年利率、月利率、月息。
时间: 2023-11-27 15:50:45 浏览: 112
以下是一个可能的Account类的实现,以及对应的测试代码:
```python
class Account:
def __init__(self, account_number, balance, annual_interest_rate):
self.account_number = account_number
self.balance = balance
self.annual_interest_rate = annual_interest_rate
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print('Insufficient balance!')
else:
self.balance -= amount
def get_monthly_interest_rate(self):
return self.annual_interest_rate / 12
def get_monthly_interest(self):
return self.balance * self.get_monthly_interest_rate()
def __str__(self):
return f'Account {self.account_number}, balance: {self.balance}, annual interest rate: {self.annual_interest_rate}'
# 测试代码
account = Account('998866', 2000, 0.045)
print(account)
account.deposit(150)
account.withdraw(1500)
print(f'Account number: {account.account_number}')
print(f'Balance: {account.balance}')
print(f'Annual interest rate: {account.annual_interest_rate}')
print(f'Monthly interest rate: {account.get_monthly_interest_rate()}')
print(f'Monthly interest: {account.get_monthly_interest()}')
```
输出结果为:
```
Account 998866, balance: 2000, annual interest rate: 0.045
Account number: 998866
Balance: 650
Annual interest rate: 0.045
Monthly interest rate: 0.00375
Monthly interest: 2.4375
```
阅读全文
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)