Java — Inventory project with boundary tests
This Java inventory example represents each transaction as an Event record. Tests check that accepted shipments reduce stock and rejected transactions leave it unchanged.
A simple example
Trace one stock change
- Start with 3 in stock
- Do this Take 1 away
- Result 2
class Main {
public static void main(String[] args) throws Exception {
int stock = 3;
stock -= 1;
System.out.println(stock);
}
}
2
Build an inventory event processor
Keep the event-processing function separate from printing and testing. An invalid event changes only the rejection count. The example uses small, bounded integer quantities.
The first delivery brings five units and a shipment takes two. A request to ship nine and an unknown event are both rejected, so stock stays at three. The final delivery adds four and closes the ledger at seven.
Run the example
Java 17 or later. Run these commands from the directory containing the example.
java Main.java
import java.util.List;
class Main {
record Event(String kind, int units) {}
record Result(int stock, int rejected) {}
static Result process(List<Event> events) {
int stock = 0, rejected = 0;
for (Event event : events) {
int n = event.units();
if ("in".equals(event.kind()) && n > 0) stock += n;
else if ("out".equals(event.kind()) && n > 0 && n <= stock) stock -= n;
else rejected++;
}
return new Result(stock, rejected);
}
static void check(Result actual, Result expected) {
if (!actual.equals(expected)) throw new AssertionError(actual);
}
public static void main(String[] args) {
var events = List.of(new Event("in", 5), new Event("out", 2), new Event("out", 9),
new Event("?", 1), new Event("in", 4));
check(process(List.of()), new Result(0, 0));
check(process(List.of(new Event("in", 2), new Event("out", 2))), new Result(0, 0));
var result = process(events);
check(result, new Result(7, 2));
System.out.println(result.stock() + " " + result.rejected());
}
}
7 2
Start with zero units. Receive 5 and ship 2 to leave 3. Reject the shipment of 9 and the unknown event. Finally receive 4. The two output numbers are the ending stock of 7 and the two rejected events.
Buy supplies for the next shift
A breakfast kiosk has 28 cups left, expects 64 orders tomorrow and keeps four spare cups for spills. The supervisor subtracts usable stock from the target and buys sealed sleeves of ten, rounding the order up to a whole sleeve.
class Main {
public static void main(String[] args) throws Exception {
int orders = 64;
int spare = 4;
int onHand = 28;
int shortage = Math.max(0, orders + spare - onHand);
int sleeves = (shortage + 9) / 10;
System.out.println(Integer.toString(sleeves) + " sleeves");
}
}
4 sleeves
The target is 68 cups. After counting the 28 on hand, the kiosk needs 40 more, exactly four sleeves of ten.
Try it yourself
Append a shipment of 7, followed by another shipment of 1.
Check your answer
The first shipment exactly empties stock. The next one is rejected. Update the expected result to 0 units and 3 rejections. Keep the existing checks for the empty event list and exact depletion.