CMSC 330, Fall 2007

Organization of Programming Languages

We meet in CSI 1115 on Tuesdays and Thursdays

Project 5

Assigned: Monday, Nov 19, 2007
Due: Tuesday, Dec 11, 2007 11:59:59pm

Clarifications

  • Note: for transaction N and T, after waiting, you should check again the avalability of the required account (it might be closed by other ATM during the waiting). If it does not exist, abort the transaction and recording it as XW for N and XT for T.
  • In transaction N and T, the ATM will wait at most 3 seconds.
  • If the amount to withdraw is less than 10, consider it as 10.

Introduction

In this project, you will develop a Java program that uses multiple threads to simulate a bank with a network of ATMs.

What to Submit

You should submit a set of Java source files implementing the simulation. We will run your simulation by

java Bank <file1> <file2> ... <filen>

Each <filei> is the name of a file containing a list of ATM transactions, representing the sequence of events that occur on one ATM. (So your will simulate as many ATMs as there are files). You will at least need to write a class Bank.java with a main() method. We recommend also writing another class Atm.java to model the actions of an ATM, and you may also want to write an Account.java class to store information about accounts. The exact design is up to you, however.

You can find a p5.tar.gz file here, which contains an almost-empty directory with just a .submit file in it.

http://www.cs.umd.edu/~atif/Teaching/Fall2007/project5/p5.tar.gz

The Simulation

In your simulation, you will create one thread for each ATM. All of your ATM threads will execute simultaneously, except when they explicitly need to be blocked to enforce atomicity. Each file passed on the command line to your Bank represents one ATM, and contains a list of lines of the form

Acct1 Trans Amount [Acct2]

Each line represents one transaction, and your thread for that ATM should simulate the transactions in order, from the beginning to the end of the file, and then that thread should exit.

In a transaction,

  1. A single space separates each field and a digit ends the line
  2. Acct1 is an eight-digit account number (it is always exactly eight digits, even if there are leading zeros)
  3. Trans is either O, C, D, W (open, close, deposit or withdrawal), N (Notify withdrawal) or T (transfer)
  4. Amount is a dollar amount of the form \d+\.\d\d (as a Ruby regexp):
    • A deposit is from 0.00 to 9999.99.
    • A withdrawal is from 10.00 to 300.00 (in 10 dollar increments). If the user attempts to withdraw more than $300, withdraw only $300.
    • Leading zeros are ok; a decimal point is required
  5. Acct2 is an eight-digit account number (same format as Acct1). Note that it only appears in transaction T, which is why it is enclosed in [] above. The [] are not part of the file syntax.

Since the transactions are formatted by the ATM, you may assume that the syntax is correct, although you have to check that (1) transaction amounts are between the specified limits, (2) withdrawals are in 10 dollar increments, (3) accounts exist, and (4) balances are sufficient depending upon the type of transaction.

Simulation Output

Each ATM will read transactions from its file and contact the bank to perform the transaction. The bank will send to standard output a "trace" of actions in the order that they occur at the bank. Depending on the scheduler, you may get different traces, but you should never get an invalid trace, e.g., one in which a withdrawal would make the balance go negative.

Important! In order to make sure your trace always looks valid to our grading scripts, print the output for each action and call System.out.flush() before releasing the lock. Otherwise even though you are carrying out the actions in the right order, the trace may appear to be invalid.

Here is what should occur for each transaction type. For all print statements, put ", " (comma followed by space) between each field, and end each line with a digit. Amounts printed do not have leading zeros, but account numbers may have leading zeros. In the output, "current balance" always means the balance after the transaction has been carried out.

  • O - Open an account (that's the letter O, not a zero). Check whether the account already exists at the bank.

    If it does not exist, then create the account at the bank and print out

      ATM file, OP, account number, 0.00, amount
    
    For example, if the file atm1.txt contained the line 12345678 O 0.00 and the account 12345678 did not already exist, then your output would be atm1.txt, OP, 12345678, 0.00, 0.00 (This creates a new account at the bank. A later D transaction will add money to account.)

    If the account already exists, print out

      ATM file, XO, account number, 0.00, current balance
    
    For example, given the same transaction as above but the account 12345678 was already open with $37.52 in it, the output would be atm1.txt, XO, 12345678, 0.00, 37.52
  • C - Close an account. Check whether the account exists.

    If the account exists, print out

      ATM file, CL, account number, current balance, 0.00
    
    and remove the account number from the bank.

    If the account does not exist, print out

      ATM file, XC, account number, 0.00, 0.00
    
  • W - Withdraw from the bank. If the amount is less than 10, count it as 10. If the amount is larger than 10 and is not a multiple of 10, truncate to the nearest multiple of 10 that is less than the specified amount, and continue as usual.

    Check whether the account exists with sufficient funds. If so, print out

      ATM file, WI, account number, amount withdrawn, current balance
    
    and update the balance in the bank.

    If the balance is too low, print out

      ATM file, WX, account number, 0.00, current balance
    
    and do not change the amount in the account.

    If the account does not exist, print out

      ATM file, XW, account number, 0.00, 0.00
    
  • D - Deposit. Check whether the account exists. If the account does not exist, print out
      ATM file, XD, account number, 0.00, 0.00
    

    If the deposit is too large (more than 9999.99), then add $9999.99 to the account, and print out

      ATM file, DE, account number, 9999.99, current balance
    
    (Wait---what happened to the rest of the money I deposited?! It's just a simulation, so don't worry about it.)

    otherwise (account exists and amount is less than 9999.99) print out

      ATM file, DE, account number, amount deposited, current balance
    
  • N - Notify withdrawal. The semantics for this transaction are the same as W, with the following exception: If the account exists but the balance is too low to perform the withdrawal (which would be recorded as a WI), then
    • This ATM should wait until the balance becomes high enough (via deposits from other ATMs), and then carry out the transaction as a WI.
    • If this ATM is waiting and 20 or more transactions occur at the bank but the N transaction still cannot execute, then abort the N transaction, recording it as a WX. All transactions, whether valid or invalid, count as part of these 20.
    • If this ATM is waiting and fewer than 20 transactions occur, but all the other ATMs complete (and so there can't be any more transactions), then abort the N transaction, recording it as a WX.
    • The ATM waits at most 3 seconds. If it is still unable to proceed with the transaction, then abort the N transaction, recording it as a WX.
    The ATM that is waiting cannot process any more transactions until the N transaction is completed or time out. After the N transaction either succeeds as a WI or aborts as a WX, then the ATM continues with the next transaction in its list.
  • T - Transfer money between accounts.
    • If any of the accounts do not exist, print out (for each non-existent account number),
        ATM file, XT, account number, 0.00, 0.00
      
    • If both of the accounts exist (you can assume that they are different accounts) and Acct1 has sufficient balance for the required transfer, print out
        ATM file, TR, account number 1, amount transfered, current balance
        ATM file, TR, account number 2, amount transfered, current balance
      
      and update the balance in the bank.
    • If both accounts exist and the balance for Acct1 is too low, then wait (as in transition N above).
      • If the wait results in a successful transfer, then print out
          ATM file, TR, account number 1, amount transfered, current balance
          ATM file, TR, account number 2, amount transfered, current balance
        
      • If aborted then print out
          ATM file, TX, account number 1, 0.00, current balance
          ATM file, TX, account number 2, 0.00, current balance
        
        and do not change the amount in any of the accounts.

Hints

  • Do not use floating point numbers to represent account balances. Because floating point numbers use base 2, they cannot represent most decimal fractions exactly. Instead, you should represent dollars using integers (either one integer, and you divide or mod by 100 where appropriate, or as a pair of integers).
  • You may use Java 1.5 or Java 1.4 style synchronisation, as you prefer.
  • You'll want to use await and signalAll or wait and notifyAll to implement the N and T transactions. Since this is trickier than the rest of the project, we recommend you start with the first four actions (O, C, W, and D), and then once those are working add N and T.

If there are any cases that we haven't covered, your implementation may do anything sensible. Your implementation must always terminate the simulation within a reasonable amount of time given any input files.

Academic Integrity

The Campus Senate has adopted a policy asking students to include the following statement on each assignment in every course: "I pledge on my honor that I have not given or received any unauthorized assistance on this assignment." Consequently your program is requested to contain this pledge in a comment near the top.

Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible cooperation on projects, use of disallowed materials or resources, or unauthorized use of computer accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension or expulsion from the University. Be sure you understand what you are and what you are not permitted to do in regards to academic integrity when it comes to project assignments. These policies apply to all students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating them. Full information is found in the course syllabus---please review it at this time.

Valid HTML 4.01!