Assignment 10 — The bank server
BankLab, part 2 of 4: a server in front of the bank from A9. Clients connect over the network and send one line at a time; each client is served on a thread of its own, and all of them share one bank.
Due Nov 10, 2026
BankLab, part 2. A9 built the bank; this week it gets a server, a program that waits on a port as Wednesday’s servers did and answers the bank’s protocol for any number of clients at once. A11 moves the accounts into a database, and A12 is all of it together. The BankLab rubric says how the finished project is assessed; this week’s work is most of its first row, and the half of its second that keeps the protocol apart from the bank’s rules.
This one has its own repo. Accept it the same way you accepted A9, clone it, open the folder in IntelliJ and let it finish loading. It is laid out as A9 was. Run SessionTest from the arrow beside its name: the red tests are the methods you are about to write, and a task is done when its tests are green, yours included.
What is given
A9’s bank, finished: Bank, MemoryBank, Account, Entry and the exceptions, written to A9’s text. The server uses nothing but Bank’s seven methods, so any bank that keeps A9’s rules will do. If you would rather build on your own A9 bank, put your MemoryBank.java and Account.java in place of these two, and commit that on its own, before anything else. Either way costs nothing.
BankClient, the bank’s client. Each line you type goes to the bank exactly as you typed it, and the client prints the bank’s whole answer. Run it from its arrow once the server is running. To run two at once: Run ▸ Edit Configurations…, pick BankClient, then Modify options ▸ Allow multiple instances, as on Wednesday.
Session, one client’s conversation with the bank, is yours. Its constructor takes the bank, and
returns the answer to one line from the client. Until you write it, it answers ERROR not written yet to everything. BankServer, whose main is empty, is yours too.
Keeping the conversation in Session and out of the server means a test can hold a conversation with no network at all: lines in, answers out.
The protocol
| the client sends | the bank answers |
|---|---|
REGISTER alice secret |
OK, and the client is logged in to the new account |
LOGIN alice secret |
OK |
BALANCE |
BALANCE 12.50 |
DEPOSIT 12.50 |
OK |
WITHDRAW 2.50 |
OK |
TRANSFER bob 2.50 |
OK |
HISTORY |
a line for each entry, oldest first, then OK |
- An answer ends with a line that is
OK, or starts withBALANCEorERROR. That is how a client knows the answer is over. - A refusal is one line:
ERROR, a space, and what went wrong. When the bank refuses, by throwing aBankException, what went wrong is the exception’sgetMessage(). - Every command but
REGISTERandLOGINneeds a login first, and before one answersERROR not logged in. ALOGINthat is refused keeps whatever login the client had. - Commands are in capitals and words are separated by one space. A line with the wrong number of words is refused.
- An amount is one to nine digits of dollars and then, if there is a dot, one or two digits of cents:
12.50,12.5and12are all twelve dollars fifty, or twelve dollars. Anything else is refused. - A
HISTORYline is whatEntry’stoStringwrites:7 TRANSFER_TO bob 12.50.
A conversation, a line from the client and then the bank’s answer:
BALANCE
ERROR not logged in
REGISTER alice secret
OK
DEPOSIT 12.50
OK
WITHDRAW 20
ERROR alice holds 12.50, less than 20.00
TRANSFER bob 2.5
OK
HISTORY
1 DEPOSIT 12.50
2 TRANSFER_TO bob 2.50
OK(This one assumes bob has registered from another client. The words after ERROR are the bank’s own and need not match these.)
Each task ends with a test of your own, at the bottom of SessionTest: a case the given tests do not try, with the answer worked out by hand first. It is done when it is green, and it goes red when you change the answer it expects (then put it back).
The tasks
The server, refusing everything. In
BankServer’smain: aServerSocketonPORT, and for each client that connects, a thread of its own with aSessionof its own. All the sessions share the one bank thatmainmakes. Each line the client sends goes tosession.handle, and every line of the answer goes back. Section 4 of Wednesday’s page,ThreadedEchoServer, is the shape.Run it, then
BankClient: every command is answered withERROR not written yet. This is a server already, and the rest of the assignment is the answers.Done when two clients connected at once both get that answer at once.
REGISTERandLOGIN. Inhandle, split the line into words; the first says which command it is. A field ofSessionholds the name the client is logged in to,nulluntil aREGISTERor aLOGINsucceeds. Aswitchon the first word, with the wholeswitchinside onetrythat catchesBankException, turns every refusal of the bank’s into anERRORline in one place.Your test: registering a name that is taken is refused, and a
LOGINwith the first password still answersOK.Amounts:
cents. The protocol writes dollars and cents, and the bank counts cents in along, soturns one into the other:
12.50and12.5are1250,12is1200. Anything else throwsNumberFormatException:"",12.,.5,1.234,-5,1e3,twelve, ten digits of dollars.indexOf('.')finds the dot, or gives -1 when there is none, andsubstringcuts the two parts. Check that each part is all digits,0to9, beforeLong.parseLong, which would also take-5and+5. One digit after the dot is tens of cents:12.5is 1250.Your test: an amount of your own, worked out by hand, and a refusal of your own, with
assertThrows.BALANCE,DEPOSITandWITHDRAW.BALANCEanswersBALANCEand the balance in dollars, whichEntry.dollars(cents)writes for you. An amount thatcentsrefuses is anERRORline too: itsNumberFormatExceptionis caught beside theBankException.Your test: a withdrawal of one cent more than the balance is refused, and
BALANCEanswers what it did before.TRANSFERandHISTORY.TRANSFER bob 2.50moves money from the account logged in to bob’s.HISTORYanswers a line for each entry and thenOK, so its answer is a list longer than one.Your test: a transfer to an account that does not exist, and one to yourself, are refused, and change no balance.
No line ends a session. A server must not stop, or stop serving a client, because of what that client sent: a client can be anybody’s program, buggy or worse. So every line gets an answer: an empty line, a line of spaces, a command in small letters, extra words, an amount too big for a
long. Watch for" ".split(" "):splitdrops empty words at the end, so a line of spaces has no words at all, andwords[0]throws.An exception that escapes
handleends that client’s thread, as one bad line did to Wednesday’s calculator.Your test: a list of bad lines of your own, each answered by exactly one
ERRORline, and then a good line that still works.Many clients at once. One bank, shared by every client’s thread: the reason A9’s bank had to be safe for many threads. Test it the way the server uses it: four
Sessions on oneMemoryBank, each on a thread of its own, each logged in to an account of its own holding 100.00, and each transferring 0.01 to the next one in a ring, 1,000 times.handlethrows nothing, so a thread’s lambda needs notry.Your test: that one: after the
joins, the four balances add up to 400.00, which is 40000 cents.Done when the test is green, and two
BankClients at once, logged in to two accounts, move money between them and see it arrive.Say what works. Fill in What works, and what does not in your repository’s
README.md, as in A9: for each command and each kind of refusal, the names of your tests that check it, and anything you know does not work.Done when the section names at least one test of yours for each of tasks 2 to 7.
Optional — robots at the bank
The rubric’s third row asks for a run in which many clients move money at once, with the bank’s total compared before and after. Write that run: BankRobots, a program of its own that starts robot clients over the network. Build it in this order:
- A robot is a client with no keyboard. A class
Robot implements Runnable, which opens its ownSocket,ScannerandPrintWriterin its constructor, and has a methodString ask(String line)that sends one line and returns the answer. Robots never sendHISTORY, so every answer they get is one line. - Set the robots up in
main, before any thread starts. Robotiregisters asrobotandi, and deposits 100.00. A transfer to a robot that has not registered yet would be refused. The server forgets every account when it stops, so start it afresh before each run, orREGISTERwill answer that the name is taken. - Then each robot’s
run: 1,000 times, pick another robot at random (new Random().nextInt(n), again if it picks itself), andTRANSFER0.01 to it. Count the answers that are notOK; there should be none, since no robot can send away more than 10.00. - After the
joins, ask every robot for itsBALANCEand add them up. With 4 robots: 400.00 before, 4,000 transfers, 400.00 after. - Then time it,
System.nanoTime()before the threads start and after thejoins, with 1, 2, 4 and 8 robots, three runs each. Does twice the robots take twice the time? Write what you see in yourREADME.md.
Handing in
Commit and push. The last commit before the deadline is what gets marked.
Every push is graded, one check at a time, and the result lands on your feedback pull request, so push early and read what failed. The checks hold conversations of their own with your Session, start your BankServer and connect to it, two clients and then eight at once; then they run SessionTest, which needs your six tests and at least one that starts threads. A green check is a floor, not the mark: the mark is read off your code too, as the rubric’s rows describe.
A student presents this one in class on Wednesday, November 11.