Task 2 : Classes, Instance Attributes and Methods

Post Updated on: Sep. 22, 2021

Task Objectives

  1. Creating class with instance attributes and methods
  2. Accessing instance attributes from methods
  3. Calling methods from outside the object and from whithin same object
  4. Appreciating how object oriented design help to write maintainable code
  5. Planning how and where we add new requirements in object oriented design
This task explains only one program, parts are made to make it easy to understand and impelement step by step, only for learning objective. Do not make different programs for each part.

Part 1

Make BankAccount class that shall have balance and name attributes of type double and String. Implement void deposit(double amount) and void withdraw(double amount) methods inside the BankAccount class. The deposit method shall increase the balance by passed value and withdraw method should decrease the balance with passed amount.

Make BankAccountTest class. Inside its main(String args[]) method, create a new object of BankAccount class. Get balance and name of account holder from user input and use user entered data to initilize object instance attributes. 

After the object is created and initialized, show this menu to user:

Press 1: To Deposit an amount
Press 2: To Withdraw an amount
Press 3: To View the current balance
Press 4: To Close this program

If user press 1: your program shall show following:
Enter the amount you want to desposit in your account >

For example. if user enters 500, you shall call desposit method of BankAccount object passing 500 to it.  On same pattern, call the withdraw method when user choose option 2. If user enter 3 from menu, print the current balance on command line. If user press 4, your program shall terminate.

Part 2

Add below functionality in your program. Think yourself, what methods and data you need for it and where it shall be defined in your program you did in Part 1.

As user would perform multiple deposit and withdraw transactions. When user choose to close the program, your program shall print: account title, total number of transactions performed, how many deposit transactions were done and how many withdraw transactions were done, and it shall also print the balance of the account. For example, if user did 5 deposits and 3 withdraws, your program shall print:

Account Title: Asif Shahzad
Total deposits: 5
Total withdraws: 3
Balance: 5000
(The numbers in above are just example, you shall print genuine values as per how user operated your program).

Part 3

The banks like it when people deposit more and withdraw less. Lets assume, the bank manager devised a strategy to motivate people to deposit more. The strategy is, whenever an account holder with minimum balance 100,000 deposit an amount, the banks gives additional 1% of the deposited amount to account holder, by directly adding in your account balance, at the time of deposit transaction.

For example, before a transaction, assume the balance was 200,000. The account holder deposited 30,000. Normally the balance shall become 230,000 but after you have implemented the new strategy, the balance shall become 230300... as 300 is 1% of 30,000. Decide yourself, should you add a new method or change an earlier method to implement this strategy.

Keep in mind, OOP objective is not login building but identifying and creating components that are near to real life, making cohesive and loosly coupled components to make them highly reusable and esaily maintainable.

Part 4

Lets further practice how OOP help us to write better code.

Assume, Bank want customer to maintain at least 50,000 balance, for that bank devised a strategy i.e. deducting 2% tax on each withdrawl transaction when balance is below 50,000. You are supposed to plan, what and where you need to make changes to implement new tax strategy.

Moreover, when user choose to make withdraw transaction and enters the amount to withdraw. Before performing that transaction, check whether it would make the balance below 50,000. If so, ask user "Asre you sure you want to withdraw, it would make your balance below 50,000. Press 1 to continue and 0 to abort". You should post the transaction, based on user input.

Print balance before performing transaction and after performing transaction. To see the accuracy your implemented logic.

Part 5

Again, I have only shared the requirement and not how it shall be implemented. The only hint is, you need to define some additonla instance variables and methods to achieve this objective.

When a new account is opened, bank give option to account holder to opt SMS Alerts and Debit Card. If the customer (when opening new account) subscribe to these options (account holder can activate SMS Alerts or Debit Card, or both), bank deduct an annual fee i.e. 2000 for SMS Alerts and 5000 for Debit Card. There there are two type of accounts i.e. STANDARD and PREMIUM. The listed rates are only charged once a year (say on Dec. 31) from STANDARD account holders. The fee is deducted from the account balance. Bank charge no fee for both options from PREMIUM account holders. A PREMIUM account is one that have minimum 3 Million balance when the fee for options is charged.

You shall define a behavior for account, when invoked it shall, deduct the fee as per account details and options already chosen. When you invoke this behavior from main method, first print account information e.g. balances and options subscribed, after deducting the fee, if any, again print the account balance.


Note: Above scenarios have nothing to do with how banking work in real life. Its just an example to practice and brainstorm, where we shall write code to implement a specific strategy or requirement and how object oriented design help us doing so.


  1. I myself is a great fan of java programming and just right now as I’ve gone through this post. It has really amazed me, I really like the concept of coding done in it.


Post a Comment