|
| 1 | +// Module focus: Modeling related data and behavior with structured types. |
| 2 | +// Why it matters: practicing structs and classes patterns makes exercises and checkpoints easier to reason about. |
| 3 | + |
| 4 | +import java.util.List; |
| 5 | +import java.util.Locale; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + record Coordinate(int x, int y) { |
| 9 | + int manhattanDistanceFromOrigin() { |
| 10 | + // Records are a good fit for small immutable values with derived behavior. |
| 11 | + return Math.abs(x) + Math.abs(y); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + static class Wallet { |
| 16 | + private final String owner; |
| 17 | + private double balance; |
| 18 | + |
| 19 | + Wallet(String owner, double initialBalance) { |
| 20 | + // Constructors are the first chance to normalize or reject invalid state. |
| 21 | + String cleanOwner = owner == null ? "" : owner.trim(); |
| 22 | + this.owner = cleanOwner.isEmpty() ? "Unknown" : cleanOwner; |
| 23 | + this.balance = Math.max(0.0, initialBalance); |
| 24 | + } |
| 25 | + |
| 26 | + boolean deposit(double amount) { |
| 27 | + // Invalid deposits are rejected instead of silently changing state. |
| 28 | + if (amount <= 0.0) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + balance += amount; |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + boolean withdraw(double amount) { |
| 36 | + // Methods protect the balance invariant by rejecting impossible updates. |
| 37 | + if (amount <= 0.0 || amount > balance) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + balance -= amount; |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + String owner() { |
| 45 | + // Accessor methods expose read-only views of private fields. |
| 46 | + return owner; |
| 47 | + } |
| 48 | + |
| 49 | + double balance() { |
| 50 | + return balance; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | + Locale.setDefault(Locale.US); |
| 56 | + // Prepare sample inputs that exercise the record path. |
| 57 | + List<Coordinate> route = List.of(new Coordinate(2, 3), new Coordinate(-1, 4), new Coordinate(5, -2)); |
| 58 | + |
| 59 | + // Report values so learners can verify the structs and classes outcome. |
| 60 | + System.out.println("Coordinates (record example):"); |
| 61 | + for (Coordinate point : route) { |
| 62 | + System.out.println( |
| 63 | + "Point (%d, %d), Manhattan distance = %d" |
| 64 | + .formatted(point.x(), point.y(), point.manhattanDistanceFromOrigin())); |
| 65 | + } |
| 66 | + |
| 67 | + Wallet wallet = new Wallet("Maya", 120.0); |
| 68 | + // Run controlled mutations through methods rather than editing fields directly. |
| 69 | + wallet.deposit(35.0); |
| 70 | + wallet.withdraw(40.0); |
| 71 | + |
| 72 | + System.out.println(); |
| 73 | + System.out.println("Wallet (class example):"); |
| 74 | + System.out.println("Owner: " + wallet.owner()); |
| 75 | + System.out.printf("Balance: %.2f%n", wallet.balance()); |
| 76 | + } |
| 77 | +} |
0 commit comments