JAVA PROGRAM • LEVEL 18 — PLACEMENT PROGRAMS & MINI PROJECTS
Build an Inventory Manager in Java
Learn build an inventory manager in java with a complete, compiler-ready Java 17 example.
PROBLEM UNDERSTANDING
Input and expected output
No input required
P1 Keyboard 7 P2 Mouse 15
COMPLETE JAVA 17 PROGRAM
Complete Java 17 implementation
import java.util.*;record Item(String name,int quantity){}class Inventory{private final Map<String,Item> items=new TreeMap<>();void add(String id,String name,int quantity){items.put(id,new Item(name,quantity));}void sell(String id,int quantity){Item item=items.get(id);if(item!=null&&item.quantity()>=quantity)items.put(id,new Item(item.name(),item.quantity()-quantity));}void print(){items.forEach((id,item)->System.out.println(id+" "+item.name()+" "+item.quantity()));}}class Main{public static void main(String[] args){Inventory inventory=new Inventory();inventory.add("P1","Keyboard",10);inventory.add("P2","Mouse",15);inventory.sell("P1",3);inventory.print();}}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
P1 Keyboard 7 P2 Mouse 15
PROGRAM EXPLANATION
Algorithm and explanation
- Initialize the deterministic sample values used for build an inventory manager in java.
- Create the required object and apply Map and Record to its state or behavior.
- Print the final result and compare it with the documented sample output.
This Java 17 example demonstrates Map, Record, and Business rule through a complete, executable program. The values are intentionally small so students can trace each statement, verify the output, and then modify the example safely in the online compiler.
EFFICIENCY
Time and space complexity
O(n log n)
O(n)
DEBUGGING CHECKLIST
Common mistakes
Keep the class name Main and the entry method signature public static void main(String[] args) when using the online compiler.
Create an object before accessing instance members; static members can be accessed through the class name.
Java is case-sensitive, and every statement that requires a semicolon must end with one.
