Re: Euphoria vs. C and OOP

new topic     » goto parent     » topic index » view thread      » older message » newer message

CChris wrote:
> 
> Bill Reed wrote:

> >   ====================================================================
> >   ==  Member banking.e                                              ==
> >   ====================================================================
> > }}}
<eucode>
> > 
> >   --*----------------------------------------------------------------*
> >   --*  banking.e             bank account program                    *
> >   --*                                                                *
> >   --*    Public                                                      *
> >   --*      function  BankProgram                                     *
> >   --*                                                                *
> >   --*    Private (or member functions)                               *
> >   --*      function  CreateAcct                                      *
> >   --*      function  Withdrawal                                      *
> >   --*      function  Deposit                                         *
> >   --*      procedure Balance                                         *
> >   --*                                                                *
> >   --*  example of account:                                           *
> >   --*                                                                *
> >   --*  a = {015505, "John Smith", 12000.15}                          *
> >   --*  a[1] = acctno                                                 *
> >   --*  a[2] = acctname                                               *
> >   --*  a[3] = balance                                                *
> >   --*----------------------------------------------------------------*
> > 
> > global constant
> >     acctno   = 1,
> >     acctname = 2,
> >     balance  = 3,
> >     blank_acct = {0, "", 0}
> > 
> > function CreateAcct()               -- Private function returns account
> >   sequence User_account
> > 
> >   User_account           = blank_acct
> >   User_account[acctno]   = 111000              -- assign next account #
> >   User_account[acctname] = prompt_string("Enter name : ")
> > 
> >   return User_account
> > 
> > end function
> > 
> > function Withdrawal(sequence account, atom amount)  -- Private function
> >   account[balance] -= amount
> > 
> >   return account
> > 
> > end function
> > 
> > function Deposit(sequence account, atom amount)    -- Private function
> >   account[balance] += amount
> > 
> >   return account
> > 
> > end function
> > 
> > procedure Balance(sequence account)                -- Private procedure
> >   printf(1, "account : %d  %s  Balance : %.2f\n", {account[acctno],
> >                                                    account[acctname],
> >                                                    account[balance]})
> > 
> > end procedure
> > 
> > global function BankProgram(sequence parms)
> >   --*----------------------------------------------------------------*
> >   --*  Public because it is global                                   *
> >   --*----------------------------------------------------------------*
> > 
> >   sequence command, User_account, ret_account
> >   atom amount
> > 
> >   --*----------------------------------------------------------------*
> >   --*  parms[1] = command                                            *
> >   --*  parms[2] = account (optional when creating new account)       *
> >   --*  parms[3] = optional amount                                    *
> >   --*  parms[4]... optional and expandable                           *
> >   --*----------------------------------------------------------------*
> > 
> >   command  = parms[1]
> > 
> >   if    equal(command, "new") then
> >     ret_account  = CreateAcct()
> >   elsif equal(command, "Balance") then
> >     User_account = parms[2]
> >     Balance(User_account)
> >     ret_account  = {}
> >   elsif equal(command, "Deposit") then
> >     User_account = parms[2]
> >     amount       = parms[3]
> >     ret_account  = Deposit(User_account, amount)
> >   elsif equal(command, "Withdrawal") then
> >     User_account = parms[2]
> >     amount       = parms[3]
> >     ret_account  = Withdrawal(User_account, amount)
> >   else
> >     puts(1, "Unknown command\n")
> >     return {}
> >   end if
> > 
> >   return ret_account
> > 
> > end function
> > </eucode>
{{{

> > 
> >   ====================================================================
> >   ==  end of Member banking.e                                       ==
> >   ====================================================================
> > 
> > 
> >   ====================================================================
> >   ==                main program:                                   ==
> >   ====================================================================
> > }}}
<eucode>
> > 
> >   include get.e
> >   include graphics.e
> >   include banking.e
> > 
> >   sequence myAccount, null_status
> > 
> >   text_color(WHITE) bk_color(BLUE)
> >   clear_screen()
> > 
> >   myAccount   = BankProgram({"new"})
> >   myAccount   = BankProgram({"Deposit",    myAccount, 12000.00})
> >   myAccount   = BankProgram({"Withdrawal", myAccount, 150.00})
> >   null_status = BankProgram({"Balance",    myAccount})
> > 
> > </eucode>
{{{

> > 
> >   Other functions and features can be added from this basic
> >   framework.  Features such as checking for overdraft status, adding
> >   extra fees, crediting or debiting accounts, etc.
> 
> This is the sort of hack you are unfortunately saddled with when using the 
> language. Removing this sort of global namespace pollution is a priority 
> issue IMHO.
> If only because, once you have defined a constant "balance" to number the 
> relevant field in your sequence, you can no longer have another "structure"
> 
> use the same name, as the field index name must be global. This makes no 
> sense.
> 
> CChris

For example, The above program written using structured sequences and PBR
is not only simpler, but introduces only a single id (account)
into the global namespace!
--*----------------------------------------------------------------*
   --*  banking.e             bank account program                    *
   --*                                                                *
   --*    Public structures
--*       sequence account                                         *         
                                            *
   --*                                                                *
--*    Public operations (or member functions)                     *         
          *
   --*      function  Init                                            *
   --*      function  Withdrawal                                      *
   --*      function  Deposit                                         *
   --*      procedure Balance                                         *
   --*                                                                *
   --*----------------------------------------------------------------*
 
 
   public sequence account is
       atom balance
       integer acctno
       sequence name
   end sequence

   public function account.Init()
       account.acct_no = 111000
       account.name = prompt_string("Enter name : ")
       account.balance = 0
   end function
	      
   public procedure account.Deposit(atom amount)
       account.balance += amount
   end procedure

   public procedure account.Withdrawal(atom amount)
       account.balance -= amount
   end procedure
						   
   public procedure account.Balance()
       printf(1, "account : %d  %s  Balance : %.2f\n",
                  {account.acctno, account.name, account.balance})
   end procedure

   ====================================================================
   ==  end of Member banking.e                                       ==
   ====================================================================


   ====================================================================
   ==                main program:                                   ==
   ====================================================================
  
   include get.e
   include graphics.e
   include banking.e
 
   account myAccount
 
   text_color(WHITE) bk_color(BLUE)
   clear_screen()

   myAccount.Init()
   myAccount.Deposit(12000.00)
   myAccount.Withdrawal(150.00)
   myAccount.Balance()


KtB

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu