Date: Wed, 08 Jan 1997 21:27:43 GMT Server: NCSA/1.4.2 Content-type: text/html First Smalltalk Assignment



next up previous
Next: About this document

CS341, Spring 1996
Smalltalk Assignment #0
Assigned 4/25, (not to hand in).



The Assignment (you will be helped through this in section):

  1. Log on to the NT workstation.
  2. Start Smalltalk.
  3. Change the path of the changes file, and save the image in your local file space. (Instructions in the lab, or on the Web.)
  4. Exit Smalltalk, and re-start it, but this time use your own Smalltalk image.
  5. Type in the code for a very simple ``bank account'' class, explained and listed below.
  6. Test the code (in a workspace) on a couple of simple examples of your own devising.
  7. File out your new category.



The Scenario:

For the Smalltalk part of the class we will be looking at banks and bank accounts again. With that in mind we will define some very simple code for manipulating bank accounts. We will define these classes:

On the next page you will find code for the Transaction and Account classes. In section you will learn how to enter these into the Smalltalk image.

========================================
This code defines the Transaction class
========================================

1.  Class definition for Transaction:

   Object subclass: Transaction
     instanceVariableNames: 'amount date'
     classVariableNames: ''

========================================

2.  Class method for Transaction:

    newAmount: anAmount date: aDate 
       "Answer an instance of a transaction object
        with the specified date and amount. 
        Sample:
            Transaction newAmount: 100 date: Date today."

    ^self new initializeAmount: anAmount date: aDate.

========================================

3.  Instance methods for Transaction:

    initializeAmount: anAmount date: aDate 
      "This method is called only when a new instance is 
       created.  It just sets the instance's variables."

        amount := anAmount.
        date := aDate.
        ^self.

   =================================

   displayOn: aTextCollector 
     "Print a single line describing this transaction."

    amount < 0
        ifTrue:  [aTextCollector show: 'Withdrawal of  $']
        ifFalse: [aTextCollector show: 'Deposit of  $'].
    aTextCollector show: amount printString; 
                   show: ' on '; 
                   show: date printString; 
                   cr.

========================================
This code defines the Account class
========================================

    Object subclass: Account
       instanceVariableNames: 'number balance displayer transactions'
       classVariableNames: 'NextNumber '


========================================
1.  Class methods for Account
========================================

     new
       "Answer a new instance of Account with next number,   
        balance 0, and empty transactions."

    ^self basicNew initialize.

   =====================================

     initialize
       "To initialize the Account class we set the 
        next account number to 1."

        NextNumber := 1.


========================================
2.  Instance methods for Account
========================================

     initialize
       "To initialize an instance of Account, we 
        initialize the instance variables and open 
        a window to display the transactions."

       number := NextNumber.
       NextNumber := NextNumber + 1.
       balance := 0.
       transactions := OrderedCollection new.
            "This sets up the display window"
       displayer := TextCollector new.
       TextCollectorView open: displayer label: 'Account Window'.

       ^self.

       ===================================
      
      number
        "Answer the receiver's account number"
        ^number.

       ===================================
      
      deposit: anAmount on: aDate
        "To deposit, adjust the balance, and make up 
         a new transaction object with the specified amount
         and date."

        balance := balance + anAmount.
        transactions addLast: (Transaction newAmount: anAmount date: aDate).

       ===================================

       withdraw:  anAmount on: aDate 
         "To withdraw, check to see if the balance is sufficient, 
          if so adjust balance and generate a new transaction.

         balance < anAmount ifTrue: 
            [ self error: 'Attempt to overdraw account.'].
         balance := balance - anAmount.
         transactions addLast: (Transaction newAmount: anAmount negated  
                                            date: aDate).

       
       ===================================


        displayTransactions
          "Display all the transactions on the account's window.
           First print a header."

          displayer show: 'Transactions for Account '; 
                    show: number printString; 
                    cr.

          transactions do: [:trans |  displayer show: '  '.  
                                      trans displayOn: displayer].




next up previous
Next: About this document



Steve Hanks
Wed Apr 24 14:21:00 PDT 1996