Java enterprise capstone

Reject without mutating stock

A rejected reservation has no stock effect. This is a local monitor, not a database transaction.

Formal Syntax & Structure

java syntaxjava
Stock.reserve(n)

Working Example

Example Snippetjava
public class Main {    static class Stock { private int available=5; synchronized boolean reserve(int n){if(n<1||n>100)throw new IllegalArgumentException("quantity"); if(n>available)return false; available-=n;return true;} synchronized int available(){return available;} }    public static void main(String[] args) throws Exception {        var s=new Stock();System.out.println(s.reserve(3));System.out.println(s.reserve(3));System.out.println(s.available());    }}
Java Reference