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.

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

interface Card {
    int pay(int amount, String name);
    int withdraw(int amount);
    int deposit(int amount);
}

import java.util.ArrayList;
import java.util.List;

class VenusCard implements Card {
    private int balance;
    private int overdraftLimit = -500; //Example overdraft limit
    private List<String> receipts = new ArrayList<>();


    public VenusCard(int initialBalance) {
        this.balance = initialBalance;
    }

    @Override
    public int pay(int amount, String name) {
        if (balance - amount < overdraftLimit) {
            return -1; //Transaction rejected
        }
        balance -= amount;
        double fee = 0;
        if (balance < 0) {
            fee = amount * 0.05;
            balance -= (int) fee; // Cast to int to maintain integer balance.  Consider using BigDecimal for better precision with currency.
        }
        receipts.add("Payment of " + amount + " to " + name + " (Fee: " + fee + ")");
        return balance;
    }

    @Override
    public int withdraw(int amount) {
        if (balance - amount < overdraftLimit) {
            return -1; //Transaction rejected

        }
        balance -= amount;
        double fee = 0;
        if (balance < 0) {
            fee = amount * 0.05;
            balance -= (int) fee; // Cast to int to maintain integer balance. Consider using BigDecimal for better precision with currency.

        }
        receipts.add("Withdrawal of " + amount + " (Fee: " + fee + ")");
        return balance;
    }

    @Override
    public int deposit(int amount) {
        balance += amount;
        receipts.add("Deposit of " + amount);
        return balance;
    }

    public List<String> getReceipts() {
        return receipts;
    }

    public int getBalance() {
        return balance;
    }

    public void setOverdraftLimit(int overdraftLimit) {
        this.overdraftLimit = overdraftLimit;
    }
}

Summary: The code implements a Card interface and a VenusCard class. Card defines basic card functionality (pay, withdraw, deposit). VenusCard extends this, adding a 5% fee to transactions if the balance goes below zero and rejecting transactions that would violate the overdraft limit. It also maintains a list of receipts for each transaction. Note that using int for currency calculations can lead to precision loss; BigDecimal would be a more robust solution for real-world applications.

Model: gemini-1.5-flash-002
Temperature: 0.2
Max output tokens: 8192
TopK: 40.0
TopP: 0.95