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

public List<String> handle(String line)

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 with BALANCE or ERROR. 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 a BankException, what went wrong is the exception’s getMessage().
  • Every command but REGISTER and LOGIN needs a login first, and before one answers ERROR not logged in. A LOGIN that 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.5 and 12 are all twelve dollars fifty, or twelve dollars. Anything else is refused.
  • A HISTORY line is what Entry’s toString writes: 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

  1. The server, refusing everything. In BankServer’s main: a ServerSocket on PORT, and for each client that connects, a thread of its own with a Session of its own. All the sessions share the one bank that main makes. Each line the client sends goes to session.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 with ERROR 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.

  2. REGISTER and LOGIN. In handle, split the line into words; the first says which command it is. A field of Session holds the name the client is logged in to, null until a REGISTER or a LOGIN succeeds. A switch on the first word, with the whole switch inside one try that catches BankException, turns every refusal of the bank’s into an ERROR line in one place.

    Your test: registering a name that is taken is refused, and a LOGIN with the first password still answers OK.

  3. Amounts: cents. The protocol writes dollars and cents, and the bank counts cents in a long, so

    static long cents(String dollars)

    turns one into the other: 12.50 and 12.5 are 1250, 12 is 1200. Anything else throws NumberFormatException: "", 12., .5, 1.234, -5, 1e3, twelve, ten digits of dollars.

    indexOf('.') finds the dot, or gives -1 when there is none, and substring cuts the two parts. Check that each part is all digits, 0 to 9, before Long.parseLong, which would also take -5 and +5. One digit after the dot is tens of cents: 12.5 is 1250.

    Your test: an amount of your own, worked out by hand, and a refusal of your own, with assertThrows.

  4. BALANCE, DEPOSIT and WITHDRAW. BALANCE answers BALANCE and the balance in dollars, which Entry.dollars(cents) writes for you. An amount that cents refuses is an ERROR line too: its NumberFormatException is caught beside the BankException.

    Your test: a withdrawal of one cent more than the balance is refused, and BALANCE answers what it did before.

  5. TRANSFER and HISTORY. TRANSFER bob 2.50 moves money from the account logged in to bob’s. HISTORY answers a line for each entry and then OK, 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.

  6. 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(" "): split drops empty words at the end, so a line of spaces has no words at all, and words[0] throws.

    An exception that escapes handle ends 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 ERROR line, and then a good line that still works.

  7. 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 one MemoryBank, 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. handle throws nothing, so a thread’s lambda needs no try.

    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.

  8. 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 own Socket, Scanner and PrintWriter in its constructor, and has a method String ask(String line) that sends one line and returns the answer. Robots never send HISTORY, so every answer they get is one line.
  • Set the robots up in main, before any thread starts. Robot i registers as robot and i, 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, or REGISTER will 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), and TRANSFER 0.01 to it. Count the answers that are not OK; there should be none, since no robot can send away more than 10.00.
  • After the joins, ask every robot for its BALANCE and 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 the joins, with 1, 2, 4 and 8 robots, three runs each. Does twice the robots take twice the time? Write what you see in your README.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.