|
|
Project 6
Due December 12, 2006
11:59:59pm
Updates
- Dec 4. Clarifiaction: In the output, "Current balance" always
means the balance after the transaction has been carried out.
- Dec 3. Clarified what happens if you try to open an account that
doesn't exist, but you do it with a non-zero amount.
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 p6.tar.gz file in the usual place, which
contains an almost-empty directory with just a .submit file
in it.
The Simulation
In your simulation, you will create one thread for each ATM. All of
your ATM threads will execute simulatenously, 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
Acct Trans Amount
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,
- A single space separates each field and a digit ends the line
- Acct is an eight-digit account number (it is always
exactly eight digits, even if there are leading zeros)
- Trans is either O, C, D, W (open, close, deposit or withdrawal) or N
(Notify withdrawal)
- 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)
- Leading zeros are ok; a decimal point is required
Since the transactions are formatted by the ATM, you may assume the
syntax is correct, although you have to check that transaction amounts
are between the specified limits, that withdrawls are in 10 dollar
increments, and that the account exists and balances are sufficient
depending upon the type of transaction.
Simulation Output
Each ATM will read in transaction 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 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.
-
O - Open an account. (That's the letter O, not a zero.)
Check whether the account already exists at the bank. If not, and if
the amount is 0.00, add the account to the bank, and print out
ATM file, OP, account number, 0.00, 0.00
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 adds the account to bank. A later D transaction will
add money to account.) If the account already exists, or if the
transaction has a non-zero Amount, 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 so, print out
ATM file, CL, account number, current balance, 0.00
Also, remove the account number from the bank. If the account does
not exists, print out
ATM file, XC, account number, 0.00, 0.00
- W - Withdraw from the bank. If the amount 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, 00000000, 0.00, 0.00
- D - Deposit. Check whether the account exists. If so,
then print out
ATM file, DE, account number, amount deposited, current balance
If the deposit is too large, 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.) If the account number is not
valid, print out
ATM file, XD, 00000000, 0.00, 0.00
- 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 that is waiting cannot process any more transactions until the
N transaction is completed. 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.
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 synchronization, as you
prefer.
- You'll want to use await and signalAll or
wait and notifyAll to implement the N
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.
If there are any cases that we haven't covered, your implementation
may do anything sensible. Your implementaiton 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.
|