Wednesday, April 20, 2011

Object oriented programming (OOP) explained with an example

Create a class that keeps track of a bank account balance. Then write a program to use this class.

Steps involved:

  • Run the class builder utility (SE24).

  • Create a class called ZACCOUNTxx, where xx is the last two digits of your logon ID.

  • Declare a PRIVATE attribute BALANCE of type DMBTR to store the account balance.

  • Create the following PUBLIC methods:

    • SET_BALANCE (Sets the balance to a new value)

      • IMPORTING NEW_BALANCE TYPE DMBTR

    • DEPOSIT (Adds a deposit amount to the balance and returns the new balance)

      • IMPORTING AMOUNT TYPE DMBTR

      • RETURNING NEW_BALANCE TYPE DMBTR

    • WITHDRAW (Subtracts a deposit amount from the balance and returns the new balance.)

      • IMPORTING AMOUNT TYPE DMBTR

      • RETURNING NEW_BALANCE TYPE DMBTR

      • EXCEPTIONS INSUFFICIENT_FUNDS

  • Activate all elements of your class.

  • Write a program called Z_USE_ACCOUNT_xx, where xx is the last two digits of your logon ID. This program should do the following:

    • Instantiate an instance of the Account class.

    • Set the account balance to some initial value.

    • Make several deposits and withdrawals, printing the new balance each time. Do not allow the balance to become less than zero. (Use the exception to detect this.)

  • Test and debug your program.

"Extra Credit": If you have extra time, try any of the following:

  • Replace the SET_BALANCE method with a constructor. Pass the opening balance when you instantiate the account object.

  • Create a static attribute and methods to set and get the name of the bank that holds the accounts.

Step-by-step approach with screen-shots

Go to SE24 (Class builder)

Type in ZACCOUNTAA as the name of the class and press Create.

Define 3 methods DEPOSIT, SET_BALANCE and WITHDRAW.

Place the mouse cursor in DEPOSIT and hit the Parameters button.

Write the parameters imported / exported for DEPOSIT method.

Similarly for SET_BALANCE

And WITHDRAW

For withdraw we define an exception.

We can see the attributes and methods by pressing “Display object list” button on top.

Now we IMPLEMENT the 3 methods. Double click the method DEPOSIT.



Write the required code. Similarly for SET_BALANCE

Similarly for WITHDRAW.

Now we are almost done creating the object. Press CTRL + F3 to activate or hit the Matchstick.

We will see this in the status

Now we are done building the global class we can test it. Press F8.

Click SET_BALANCE. Write the NEW_BALANCE and press ENTER.

We come back to Initial Screen. Now click DEPOSIT.

We see the return Values now.

Now let’s WITHDRAW 4000

Now the BALANCE is 2000

Lets try withdrawing 3000 now.

We get an exception.

Given below is an example code for using the global class we defined.

REPORT ZGB_OOPS_BANK .

DATA: acct1 type ref to zaccountaa.

DATA: bal type i.

create object: acct1.

selection-screen begin of block a.
parameters: p_amnt type dmbtr,
p_dpst type dmbtr,
p_wdrw type dmbtr.
selection-screen end of block a.

start-of-selection.

call method acct1->set_balance( p_amnt ).
write:/ 'Set balance to ', p_amnt.

bal = acct1->deposit( p_dpst ).
write:/ 'Deposited ', p_dpst ,' bucks making balance to ', bal.

bal = acct1->withdraw( p_wdrw ).
write:/ 'Withdrew ', p_wdrw ,' bucks making balance to ', bal.

This is the output.




No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites