CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
hello; bob opens the page and sees the same messageGET /api/messages, and gets the same answer/ada@example.combook, and its comment: Guestbook keeps one Book/api/messages: nothing in the path says which bookBook is an ADT: a change to how its data is kept can stay inside itBook ADT?115/** The messages posted so far, oldest first. */
116class Book {
117 private List<String> messages = new ArrayList<String>();Book's representation is its one field, messagesBookBook's client: Guestbook made one BookBooks, new, and Book, changed270/**
271 * @return the book of the user whose login is owner, or an empty
272 * Optional if that user has no book
273 */
274public Optional<Book> bookOf(String owner) {bookOf looks up a book by its owner's login: there may be noneBook knows its owner279/** An owner, and the messages posted so far, oldest first. */
280class Book {297public void add(User asker, String message) {User: a user who has signed in, named by their loginadd signs the message with the login of asker, the user postingadd signed with whatever name it was handed25* GET /api/books/OWNER/messages
26* OWNER's messages, as JSON
27* POST /api/books/OWNER/messages
28* signs OWNER's book with the message the
29* fields send, then OWNER's messages, as JSONBooks, where the earlier guestbook kept one BookOWNER stands for a login/ada@example.comhi ada, then opens his own:bob, at /ada@example.com
bob, at /bob@example.com
Definition: authentication
Authentication is establishing who sent a request.
ada@example.comhandle needs to know who sent every request, and asks User.from223public static Optional<User> from(HttpExchange exchange) {Optionalfrom reads the header: procedural abstractionfrom finds out can change, from a header to a cookie, saytailscale serve 9331 gives the guestbook an address in the tailnetTailscale-User-Logincurl -H sends that header itself, in Serve's placeDefinition: authorization
Authorization is deciding what the user who sent a request may do.
36const item = document.createElement("li");
37item.textContent = messages[i] + " ";
38if (me === owner) {
39 const button = document.createElement("button");
40 button.textContent = "Delete";
41 button.onclick = () => remove(i);
42 item.appendChild(button);
43}me: the login the server sent back for /api/meowner: the login in the page's own addressme holds the login of the book's ownerdelete takes no asker:305public void delete(int index) {
306 messages.remove(index);
307}ada posts two messages
bob opens ada's book, and posts
me holds the owner's logindelete takes no asker: it deletes for whoever calls it> me "bob@example.com" > me = "ada@example.com" "ada@example.com" > show() undefined
show draws a Delete button beside each of the three messageshello is gonehello, and bob signs ada's book hi adaBookBook.delete, we read only Book:308public void delete(User asker, int index)
309 throws NotOwnerException {
310 if (!asker.equals(owner)) {
311 throw new NotOwnerException(
312 asker.login() + " does not own this book");
313 }
314 messages.remove(index);
315}Book to readBook. Two facts:delete cannot be called without an asker: a call has to pass onemessages is private: in Book, one line adds, one line removesmessages() hands out a copy124try {
125 book.delete(owner, index);
126} catch (NotOwnerException e) {308public void delete(String asker, int index)
309 throws NotOwnerException {
310 if (!asker.equals(owner)) {
311 throw new NotOwnerException(
312 asker + " does not own this book");
313 }
314 messages.remove(index);asker and owner are both a String. Is the call right?javac accepts it, and bob deletesjavac accepts it, and prints nothingowner where asker belongsdelete compared the owner with the owner: anyone deletesUser, a type of its own209class User {
210 private final String login;
211 // RI: login is not null, and not empty.
212 // AF(this) = the user whose login is login.
213
214 private User(String login) {
215 this.login = login;
216 }private: only from, on a request, makes a Useradd and delete take a User. The owner in the path stays a String125 book.delete(owner, index);$ javac Guestbook.java Guestbook.java:125: error: incompatible types: String cannot be converted to User book.delete(owner, index); ^ ⋮ 1 error
Book keeps its owner in one fieldStringopen gives one only to a user who has noneUser turned a coding mistake into a compile-time error