/*
Format Strings using format specifiers
For the full story see the API documentation:
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Formatter.html
*/
void main() {
// Sample Data
final String product = "Gaming Laptop";
final int quantity = 3;
final double unitPrice = 1199.9945;
final double total = quantity * unitPrice;
final boolean inStock = true;
// 1. Format specifiers for basic data types
// %s = String, %d = Integer, %f = Floating-point, %b = Boolean, %n = Platform-specific newline
IO.println("Product: %s%n".formatted(product));
IO.println("Quantity ordered: %d units%n".formatted(quantity));
IO.println("In Stock Status: %b%n".formatted(inStock));
// 2. Precision Formatting for Decimals
// %.2f rounds the float/double to exactly 2 decimal places
IO.println("Raw total price: %f%n".formatted(total));
IO.println("Receipt total price: $%.2f%n".formatted(total));
// 3. Width Alignment & Tables (Right-aligned by default)
// %15s ensures the string takes up at least 15 spaces, right-aligned
IO.println ();
IO.println("--- Right-Aligned Receipt ---");
IO.println("%15s: %10s%n".formatted("Item", product));
IO.println("%15s: %10d%n".formatted("Qty", quantity));
IO.println("%15s: $%10.2f%n".formatted("Total Cost", total));
// 4. Left-Alignment with Flags
// %-15s uses the minus flag (-) to force left-alignment within the 15-character block
IO.println ();
IO.println("--- Left-Aligned Data Table ---");
IO.println("%-15s %-5s %-10s%n".formatted("ITEM", "QTY", "PRICE"));
IO.println("%-15s %-5d $%-10.2f%n".formatted(product, quantity, total));
IO.println("%-15s %-5d $%-10.2f%n".formatted("Wireless Mouse", 12, 24.50));
// 5. Numeric Grouping & Padding Flags
// %,f adds thousands separators; %05d pads an integer with leading zeros to 5 digits
final int orderId = 425;
final double highBudget = 1250500.75;
IO.println ();
IO.println("--- Number Flags ---");
IO.println("Padded Order ID: #%05d%n".formatted(orderId));
IO.println("Formatted Company Budget: $%,.2f%n".formatted(highBudget));
}