Kotlin Class: BankAccount class with deposit and withdrawal functions
Write a Kotlin program that creates a class 'BankAccount' with properties for account number, balance, and account holder name. Include deposit and withdrawal functions.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Classes in Kotlin: A class is a blueprint for creating objects. It defines properties and functions related to an object.
- Constructors: The primary constructor initializes an object's properties when an instance is created.
- Mutable and Immutable Properties: Properties declared with val are immutable (cannot be changed), whereas var properties are mutable (can be modified).
- Functions in a Class: Functions inside a class allow objects to perform specific actions like deposit and withdrawal operations.
- Conditional Statements: The if statement is used to check conditions, such as whether there is enough balance for a withdrawal.
Hints (Try Before Looking at the Solution!)
Try to solve the problem with these hints:
- Hint 1: Create a class called BankAccount with properties for accountNumber, balance, and accountHolderName.
- Hint 2: Define a function called deposit() that increases the balance by the deposited amount and prints a confirmation message.
- Hint 3: Define a function called withdrawal() that checks if the balance is sufficient before deducting the amount.
- Hint 4: If there is not enough balance, print an error message instead of deducting the amount.
- Hint 5: Create an instance of the BankAccount class in the main() function and perform deposit and withdrawal operations.
- Hint 6: Use the displayAccountDetails() function to print account details before and after transactions.
Sample Solution:
Kotlin Code:
class BankAccount(
val accountNumber: String,
var balance: Double,
val accountHolderName: String
) {
fun deposit(amount: Double) {
balance += amount
println("Deposit of $amount successful. New balance: $balance")
}
fun withdrawal(amount: Double) {
if (balance >= amount) {
balance -= amount
println("Withdrawal of $amount successful. New balance: $balance")
} else {
println("Insufficient balance ($balance). Withdrawal ($amount) failed.")
}
}
fun displayAccountDetails() {
println("Account Holder: $accountHolderName")
println("Account Number: $accountNumber")
println("Balance: $balance")
}
}
fun main() {
val account = BankAccount("SB-12340", 2000.0, "Lotte Nazir")
account.displayAccountDetails()
account.deposit(500.0)
account.withdrawal(400.0)
account.withdrawal(2500.0)
}
Sample Output:
Account Holder: Lotte Nazir Account Number: SB-12340 Balance: 2000.0 Deposit of 500.0 successful. New balance: 2500.0 Withdrawal of 400.0 successful. New balance: 2100.0 Insufficient balance (2100.0). Withdrawal (2500.0) failed.
Explanation:
In the above exercise -
In this program, we define a class "BankAccount" with three properties: accountNumber, balance, and accountHolderName. The class includes three functions: deposit, withdrawal, and displayAccountDetails.
The "deposit()" function takes an amount and adds it to the account's balance. It then prints a success message along with the new balance.
The "withdrawal()" function takes an amount as input and checks if the account has a sufficient balance. If it does, it subtracts the amount from the balance and prints a success message along with the new balance. Otherwise, it prints an insufficient balance message.
The "displayAccountDetails()" function prints the account holder's name, account number, and balance.
In the "main()" function, we create an instance of the "BankAccount" class named account with initial values for the properties. We then call various functions on the account object to show deposit and withdrawal operations. Finally, we call the "displayAccountDetails()" function to print the updated account information.
Kotlin Editor:
Previous: Book class with display function.
Next: Circle class with circumference calculation.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics