0% found this document useful (0 votes)
4 views3 pages

Class & object-python

Preparation

Uploaded by

kjassica79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Class & object-python

Preparation

Uploaded by

kjassica79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem Statement:

You are tasked with creating a simple BankAccount class to simulate basic banking operations. The
class should have the following features:

1. Attributes:

o account_holder (string): The name of the account holder.

o balance (float): The current balance in the account.

2. Methods:

o __init__(self, account_holder, initial_balance): Initializes a new account with the


account holder's name and an initial balance.

o deposit(self, amount): Adds the specified amount to the account balance.

o withdraw(self, amount): Deducts the specified amount from the account balance if
sufficient funds are available.

o get_balance(self): Returns the current balance of the account.

o account_info(self): Prints the account holder's name and current balance.

3. Static Method:

o bank_info(): Prints general information about the bank.

Requirements:

1. Create the BankAccount class with the specified attributes and methods.

2. Instantiate at least two objects of the BankAccount class with different account holders and
initial balances.

3. Perform some deposit and withdraw operations on these accounts.

4. Print the account information and balance for each account.

5. Use the static method to print the bank information.

Example:

class BankAccount:

bank_name = "Python Bank" # Class attribute

def __init__(self, account_holder, initial_balance):

self.account_holder = account_holder

self.balance = initial_balance

def deposit(self, amount):


self.balance += amount

print(f"{amount} deposited. New balance is {self.balance}")

def withdraw(self, amount):

if amount > self.balance:

print("Insufficient funds!")

else:

self.balance -= amount

print(f"{amount} withdrawn. New balance is {self.balance}")

def get_balance(self):

return self.balance

def account_info(self):

print(f"Account Holder: {self.account_holder}, Balance: {self.balance}")

@staticmethod

def bank_info():

print("Welcome to Python Bank, your trusted financial partner.")

# Creating instances of BankAccount

account1 = BankAccount("Alice", 5000)

account2 = BankAccount("Bob", 3000)

# Performing transactions

account1.deposit(1000)

account1.withdraw(1500)

account1.account_info()

account2.deposit(2000)
account2.withdraw(500)

account2.account_info()

# Printing bank information

BankAccount.bank_info()

Expected Output:

1000 deposited. New balance is 6000

1500 withdrawn. New balance is 4500

Account Holder: Alice, Balance: 4500

2000 deposited. New balance is 5000

500 withdrawn. New balance is 4500

Account Holder: Bob, Balance: 4500

Welcome to Python Bank, your trusted financial partner.

Explanation:

1. Class Definition: The BankAccount class is defined with the required attributes and methods.

2. Static Method: The bank_info() static method provides general information about the bank.

3. Object Creation: Two instances of the BankAccount class are created with different account
holders and initial balances.

4. Transactions: Deposit and withdraw operations are performed on the accounts.

5. Information Display: The account information and current balance are printed, and the static
method is used to print the bank information.

You might also like