Lab · Java Programming (Core to Spring Boot)
Lab: a UPI wallet class
The brief
You model a simple prepaid wallet like the ones inside UPI apps: a Wallet class that protects its balance, refuses bad payments, gives every payment a unique transaction id, and keeps a history of Payment records.
Why it matters
Wallets, accounts and orders are the core classes of every fintech and e-commerce backend. Keeping the balance private and changing it only through methods with checks is exactly how payment companies stop bugs from creating or losing money; a shared counter for transaction ids is the first taste of static state.
Skills you practise
- private fields and a constructor
- toString
- methods that enforce rules
- IllegalArgumentException
- static fields and methods
- records with a compact constructor
You need
- JDK 21 (Eclipse Temurin from adoptium.net or Oracle JDK; JDK 17 or any newer version also works)
- A text editor or IDE (VS Code with the Extension Pack for Java, or IntelliJ IDEA Community Edition) and a terminal
What you will have at the end
Wallet.java, Payment.java and WalletApp.java: an encapsulated wallet with rules, unique transaction ids and a payment history, plus the output of a full run.
How it is marked (rubric, 100 points)
| Correct behaviourEvery check passes: the balance, the declined payment, the rejected top-up, unique ids and the history. | 40 |
|---|---|
| Encapsulationbalance is private with no setter; it changes only through addMoney and pay, which check every amount. | 30 |
| static and records used rightlyOnly the shared counters are static; Payment is an immutable record with checks. | 15 |
| Readable codeClear names, small methods, messages that say what was wrong. | 15 |
0/4 steps done
Step 1 of 4
The Wallet class
Printing a new wallet shows Wallet[owner=Asha Verma, balance=500.00].
Give Wallet two private fields: owner (final, since it never changes) and balance. The constructor sets both, and throws IllegalArgumentException if the opening balance is negative, so a bad wallet can never exist. Without your own toString, println shows something like Wallet@1b6d3586 (the class name and a hash code). Override it with String.format so the balance has two decimals.
$ javac *.java
java WalletAppYour notes: Wallet.java, Payment.java, WalletApp.java
Write down each command you ran. It goes into your project summary.