Java Playground
A starter program to experiment with. Edit it here, press Copy, paste it into a file called Main.java, and run java Main.java in a terminal (Java 25 or newer). Lesson 2 shows how to install Java.
Output
Notebook 4 x 3.50 = 14.00 Pen 10 x 1.20 = 12.00 Backpack 1 x 39.90 = 39.90 Total: 65.90
Ideas to try
- Apply a 10% discount when the total is over 50 (lesson 13).
- Sort the basket by item total, highest first (lesson 43).
- Group items into "cheap" and "expensive" with
partitioningBy(lesson 47). - Read the items from a CSV file instead (lesson 51).
- Reject items with a negative price in a compact constructor (lesson 32).
Running Java without installing it
If you can't install a JDK, search for an online Java compiler that supports Java 25 or later, paste the code, and run it there. On older Java versions, the traditional public class Main form used here works everywhere from Java 16 (records) onwards.