# Generate Java classes

Create an interface and class implementation in Java.

|---|---|
| You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly. - For an example of using the Vertex AI API, see [Quickstart using the Vertex AI API](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/api-quickstart). - To view this sample in the Cloud console: [Go to Google Cloud console](https://console.cloud.google.com/vertex-ai/generative/language/prompt-examples/Generate%20Java%20classes) <br /> ||
| |---| | **System instructions** | | You are a senior Java developer teaching a junior developer and will need to provide a single paragraph at the end of your response summarizing the generated code. | #### Prompt Freeform In Java, generate two classes: VenusCard and Card. Card is an interface with the following methods: - 'int pay(int amount, String name)' - 'int withdraw(int amount)' - 'int deposit(int amount)' VenusCard is a concrete class that implements the Card interface. VenusCard does the following: - Create a receipt for each payment. - Includes pay() and withdraw() logic to add a 5% fee to any payments or withdrawals if the balance is below 0. - If a transaction would cause the balance to fall below the overdraft limit, the transaction should be rejected and return -1. #### Response Of course. Let's walk through creating a contract for a card's behavior using an interface, and then building a specific implementation with its own business rules. This is a very common and powerful pattern in object-oriented programming. First, we'll define the `Card` interface. Think of an interface as a blueprint or a contract. It dictates *what* a class should be able to do, but not *how* it does it. Any class that claims to be a `Card` *must* provide implementations for these methods. ### Card.java /** * The Card interface defines the essential contract for any type of payment card. * It specifies the core functionalities of paying, withdrawing, and depositing funds. */ public interface Card { /** * Processes a payment to a named entity. * * @param amount The amount to be paid. Must be a positive value. * @param name   The name of the recipient (e.g., "Supermarket"). * @return The new balance after the transaction, or -1 if the transaction is rejected. */ int pay(int amount, String name); /** * Withdraws a specified amount from the card's balance. * * @param amount The amount to be withdrawn. Must be a positive value. * @return The new balance after the transaction, or -1 if the transaction is rejected. */ int withdraw(int amount); /** * Deposits a specified amount into the card's balance. * * @param amount The amount to be deposited. Must be a positive value. * @return The new balance after the deposit. */ int deposit(int amount); } Next, we'll create the `VenusCard` class. This is a *concrete class* because it provides the actual logic for the methods defined in the `Card` interface. It manages its own state (like the balance and overdraft limit) and implements the specific business rules you requested. ### VenusCard.java /** * VenusCard is a concrete implementation of the Card interface. * It includes business logic for an overdraft limit, fees on negative balances, * and receipt generation for payments. */ public class VenusCard implements Card { // A constant for the overdraft limit. Using 'static final' makes it a class-level constant. private static final int OVERDRAFT_LIMIT = -1000; private static final double FEE_RATE = 0.05; // 5% fee // This field holds the state (current balance) for each VenusCard instance. private int balance; /** * Constructor to create a new VenusCard with an initial balance. * @param initialBalance The starting balance of the card. */ public VenusCard(int initialBalance) { this.balance = initialBalance; System.out.println("VenusCard created with initial balance: " + this.balance); } @Override public int pay(int amount, String name) { int fee = 0; // Check if a fee should be applied (if balance is already negative) if (this.balance < 0) { fee = (int) (amount * FEE_RATE); System.out.println("Applying a 5% fee of " + fee + " due to negative balance."); } int totalDeduction = amount + fee; // Check if the transaction would exceed the overdraft limit if ((this.balance - totalDeduction) < OVERDRAFT_LIMIT) { System.out.println("Transaction REJECTED: Payment of " + amount + " exceeds overdraft limit."); return -1; // Indicate a rejected transaction } // Process the transaction this.balance -= totalDeduction; createReceipt(amount, name, fee); return this.balance; } @Override public int withdraw(int amount) { int fee = 0; // Check if a fee should be applied (if balance is already negative) if (this.balance < 0) { fee = (int) (amount * FEE_RATE); System.out.println("Applying a 5% fee of " + fee + " due to negative balance."); } int totalDeduction = amount + fee; // Check if the transaction would exceed the overdraft limit if ((this.balance - totalDeduction) < OVERDRAFT_LIMIT) { System.out.println("Transaction REJECTED: Withdrawal of " + amount + " exceeds overdraft limit."); return -1; // Indicate a rejected transaction } // Process the transaction this.balance -= totalDeduction; System.out.println("Successfully withdrew " + amount + ". New balance: " + this.balance); return this.balance; } @Override public int deposit(int amount) { this.balance += amount; System.out.println("Deposited " + amount + ". New balance: " + this.balance); return this.balance; } /** * A private helper method to handle receipt creation for payments. */ private void createReceipt(int amount, String name, int fee) { System.out.println("--- RECEIPT ---"); System.out.println("Paid to: " + name); System.out.println("Amount: " + amount); if (fee > 0) { System.out.println("Fee: " + fee); } System.out.println("New Balance: " + this.balance); System.out.println("---"); } // A simple getter to check the balance without a transaction public int getBalance() { return this.balance; } } *** ** * ** *** This code establishes a clear separation between the contract and the implementation. The `Card` interface defines a standard set of operations, ensuring any card type we create in the future will have a consistent structure. The `VenusCard` class then implements this contract, adding its unique business logic such as applying a 5% fee on transactions when the balance is negative, rejecting any transaction that would push the balance below a fixed overdraft limit of -1000, and generating a simple console receipt for every successful payment. | |---|---| | **Model:** | gemini-2.5-pro | | **Temperature:** | 0.2 | | **Max output tokens:** | 8192 | | **TopK:** | 40.0 | | **TopP:** | 0.95 | |