Task 2 : Classes, Instance Attributes and Methods

Post Updated on: Sep. 26, 2020

Task Objectives
  1. Defining a class with instance attributes and methods
  2. Accessing instance attributes from methods
  3. How to call methods from out side the object
  4. How to decided what additional attributes we may need to fullfil some some requirement
  5. How object oriented design help us to write code that is better maintainable.
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 performed, 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.

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. Update the deposit method definition to implement this strategy.

Part 4

Lets further practice how class concept 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 below below 50,000. If so, ask user "As 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.

____

Note: Above scenarios are just examples to practice and brainstorm, where we shall write code to implement a specific strategy or requirement and how object oriented design help us to write better software.

Comments

  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.

    ReplyDelete

Post a Comment