Java: An inventory with checked rules

Design a small program properly: a class that guards its rules, and a main that only talks to the user.

What to build

A stock-keeping program for a small shop.

Requirements

  • Write class Inventory with add(item, qty), sell(item, qty) and report(), and an unchecked InventoryException. Rules: quantities must be at least 1 (bad quantity), you cannot sell an unknown item (unknown ITEM) or more than is in stock (only N ITEM left).
  • Commands: add ITEM QTY, sell ITEM QTY, report. Print error: MESSAGE for a broken rule (nothing changes). report prints ITEM: QTY for each item with stock above 0, alphabetically, then units: TOTAL.

Input

Line 1: q. Then q commands.

Output

Error lines and reports.

Constraints

1 ≤ q ≤ 200; quantities are whole numbers that may be 0 or negative in the input.

Example

Input:

6
add pen 10
add book 3
sell pen 4
sell book 5
sell lamp 1
report

Output:

error: only 3 book left
error: unknown lamp
book: 3
pen: 6
units: 9

Why it works

The class checks every rule before changing its map, so a failed command leaves the stock as it was, and main stays a thin layer of input and output.

Examples

Example 1 · Input
6
add pen 10
add book 3
sell pen 4
sell book 5
sell lamp 1
report
Expected output
error: only 3 book left
error: unknown lamp
book: 3
pen: 6
units: 9
Example 2 · Input
4
add tea 0
add tea 2
sell tea 2
report
Expected output
error: bad quantity
units: 0

Hints

Hint 1

class Inventory with a private Map<String, Integer> and methods that throw an InventoryException before changing anything.

Hint 2

main only parses commands, calls the methods and prints messages.

Solution

Show one correct solution (try it yourself first)
import java.util.*;

public class Main {
    static class InventoryException extends RuntimeException {
        InventoryException(String message) { super(message); }
    }

    static class Inventory {
        private final Map<String, Integer> stock = new TreeMap<>();

        void add(String item, int qty) {
            if (qty < 1) throw new InventoryException("bad quantity");
            stock.merge(item, qty, Integer::sum);
        }

        void sell(String item, int qty) {
            if (qty < 1) throw new InventoryException("bad quantity");
            int have = stock.getOrDefault(item, -1);
            if (have < 0) throw new InventoryException("unknown " + item);
            if (qty > have) throw new InventoryException("only " + have + " " + item + " left");
            stock.put(item, have - qty);
        }

        List<String> report() {
            List<String> lines = new ArrayList<>();
            int units = 0;
            for (Map.Entry<String, Integer> e : stock.entrySet()) {
                if (e.getValue() > 0) lines.add(e.getKey() + ": " + e.getValue());
                units += e.getValue();
            }
            lines.add("units: " + units);
            return lines;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = Integer.parseInt(in.nextLine().trim());
        Inventory inv = new Inventory();
        for (int i = 0; i < q; i++) {
            String[] p = in.nextLine().split(" ");
            try {
                if (p[0].equals("add")) inv.add(p[1], Integer.parseInt(p[2]));
                else if (p[0].equals("sell")) inv.sell(p[1], Integer.parseInt(p[2]));
                else inv.report().forEach(System.out::println);
            } catch (InventoryException e) {
                System.out.println("error: " + e.getMessage());
            }
        }
    }
}

Lesson: Putting it together: designing a complete program →