<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_9_-_OOP_Class_BankAccount_example</id>
	<title>Worksheet 9 - OOP Class BankAccount example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_9_-_OOP_Class_BankAccount_example"/>
	<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_9_-_OOP_Class_BankAccount_example&amp;action=history"/>
	<updated>2026-07-02T07:46:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.4</generator>
	<entry>
		<id>http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_9_-_OOP_Class_BankAccount_example&amp;diff=10322&amp;oldid=prev</id>
		<title>EdmondLascaris: Created page with &quot;= Bank Account Class = Here’s another simple object-oriented programming (OOP) example to help students practice. This one involves creating a Bank Account class, which simulates the basic functionality of a bank account. Like the previous example, it's designed for easy expansion.   &lt;syntaxhighlight lang=&quot;python&quot;&gt; # Simple Bank Account Class Example  class BankAccount:     def __init__(self, account_holder, balance=0):         &quot;&quot;&quot;Initialize the account holder's name a...&quot;</title>
		<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_9_-_OOP_Class_BankAccount_example&amp;diff=10322&amp;oldid=prev"/>
		<updated>2024-10-27T10:40:09Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Bank Account Class = Here’s another simple object-oriented programming (OOP) example to help students practice. This one involves creating a Bank Account class, which simulates the basic functionality of a bank account. Like the previous example, it&amp;#039;s designed for easy expansion.   &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; # Simple Bank Account Class Example  class BankAccount:     def __init__(self, account_holder, balance=0):         &amp;quot;&amp;quot;&amp;quot;Initialize the account holder&amp;#039;s name a...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Bank Account Class =&lt;br /&gt;
Here’s another simple object-oriented programming (OOP) example to help students practice. This one involves creating a Bank Account class, which simulates the basic functionality of a bank account. Like the previous example, it's designed for easy expansion.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Simple Bank Account Class Example&lt;br /&gt;
&lt;br /&gt;
class BankAccount:&lt;br /&gt;
    def __init__(self, account_holder, balance=0):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Initialize the account holder's name and balance.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        self.account_holder = account_holder   # Account holder's name&lt;br /&gt;
        self.balance = balance                 # Initialize with an optional balance (default is 0)&lt;br /&gt;
&lt;br /&gt;
    def deposit(self, amount):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Deposit money into the account.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        if amount &amp;gt; 0:&lt;br /&gt;
            self.balance += amount&lt;br /&gt;
            print(f&amp;quot;Deposited ${amount}. New balance: ${self.balance}.&amp;quot;)&lt;br /&gt;
        else:&lt;br /&gt;
            print(&amp;quot;Deposit amount must be positive!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    def withdraw(self, amount):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Withdraw money from the account.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        if amount &amp;gt; 0 and amount &amp;lt;= self.balance:&lt;br /&gt;
            self.balance -= amount&lt;br /&gt;
            print(f&amp;quot;Withdrew ${amount}. Remaining balance: ${self.balance}.&amp;quot;)&lt;br /&gt;
        elif amount &amp;gt; self.balance:&lt;br /&gt;
            print(&amp;quot;Insufficient funds!&amp;quot;)&lt;br /&gt;
        else:&lt;br /&gt;
            print(&amp;quot;Withdrawal amount must be positive!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    def display_balance(self):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Display the current balance.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        print(f&amp;quot;Account balance: ${self.balance}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if __name__ == '__main__':&lt;br /&gt;
    # Create an instance of the BankAccount class&lt;br /&gt;
    my_account = BankAccount('John Doe', 100)  # Initial deposit of $100&lt;br /&gt;
&lt;br /&gt;
    # Display the current balance&lt;br /&gt;
    my_account.display_balance()&lt;br /&gt;
&lt;br /&gt;
    # Perform a deposit&lt;br /&gt;
    my_account.deposit(50)  # Deposit $50&lt;br /&gt;
&lt;br /&gt;
    # Perform a withdrawal&lt;br /&gt;
    my_account.withdraw(30)  # Withdraw $30&lt;br /&gt;
&lt;br /&gt;
    # Attempt to withdraw more than the balance&lt;br /&gt;
    my_account.withdraw(150)  # Insufficient funds example&lt;br /&gt;
&lt;br /&gt;
    # Display the final balance&lt;br /&gt;
    my_account.display_balance()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Explanation of the Code ==&lt;br /&gt;
=== Class Definition: BankAccount ===&lt;br /&gt;
* The class is defined to simulate a simple bank account.&lt;br /&gt;
* The __init__ method initializes the account holder’s name and an optional balance (default is 0).&lt;br /&gt;
&lt;br /&gt;
=== Methods: ===&lt;br /&gt;
* '''deposit(self, amount):''' Adds money to the account, ensuring the amount is positive.&lt;br /&gt;
* '''withdraw(self, amount):''' Withdraws money from the account, ensuring the amount does not exceed the balance.&lt;br /&gt;
* '''display_balance(self):''' Prints the current account balance.&lt;br /&gt;
&lt;br /&gt;
=== Main Script: ===&lt;br /&gt;
* An instance of the BankAccount class is created with an initial deposit.&lt;br /&gt;
* The program demonstrates depositing and withdrawing money and checks for insufficient funds.&lt;br /&gt;
* It prints the balance after each transaction.&lt;br /&gt;
&lt;br /&gt;
== Expanding the Program ==&lt;br /&gt;
Students can expand this example in several ways:&lt;br /&gt;
&lt;br /&gt;
* Add Interest: Implement a method to apply interest to the account balance.&lt;br /&gt;
* Overdraft Protection: Add an overdraft limit and handle what happens when a withdrawal exceeds the overdraft limit.&lt;br /&gt;
* Multiple Accounts: Create a system to manage multiple bank accounts (e.g., savings and checking).&lt;br /&gt;
* Transaction History: Add a feature that tracks and prints all deposits and withdrawals.&lt;br /&gt;
&lt;br /&gt;
This example builds upon the basics of OOP and encourages students to experiment with new features and functionalities.&lt;/div&gt;</summary>
		<author><name>EdmondLascaris</name></author>
	</entry>
</feed>