Stay organized with collections
Save and categorize content based on your preferences.
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.
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.
In Java, generate two classes: VenusCard and Card.
Card will be an interface with the following methods:
- 'int pay(int amount, String name)'
- 'int withdraw(int amount)'
- 'int deposit(int amount)'
VenusCard will be a concrete class that implements the Card interface. It should do the following:
- Create a receipt for reach 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
// Define the Card interface
interface Card {
int pay(int amount, String name);
int withdraw(int amount);
int deposit(int amount);
}
//VenusCard class that implements Card interface
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;
public VenusCard() {
this.balance = 0;
this.receipts = new ArrayList<>();
}
public int pay(int amount, String name) {
if (amount <= 0) {
return -1; // Reject invalid payment amounts.
}
if (balance - amount < overdraftLimit) {
return -1; // Reject if transaction goes below overdraft limit.
}
balance -= amount;
double fee = 0;
if (balance < 0) {
fee = amount * 0.05;
balance -= fee;
}
receipts.add("Payment of " + amount + " to " + name + " made. Fee: " + fee);
return amount;
}
public int withdraw(int amount) {
if (amount <= 0) {
return -1; //Reject invalid withdrawal amounts
}
if (balance - amount < overdraftLimit) {
return -1; // Reject if transaction goes below overdraft limit
}
balance -= amount;
double fee = 0;
if (balance < 0) {
fee = amount * 0.05;
balance -= fee;
}
receipts.add("Withdrawal of " + amount + " made. Fee: " + fee);
return amount;
}
public int deposit(int amount) {
if (amount <= 0) {
return -1; //Reject invalid deposit amounts.
}
balance += amount;
receipts.add("Deposit of " + amount + " made.");
return amount;
}
public int getBalance() {
return balance;
}
public List<String> getReceipts() {
return receipts;
}
public static void main(String[] args){
VenusCard card = new VenusCard();
System.out.println(card.deposit(1000));
System.out.println(card.pay(500, "Store A"));
System.out.println(card.getBalance());
System.out.println(card.withdraw(800));
System.out.println(card.getBalance());
System.out.println(card.getReceipts());
}
}
Summary: The code implements a Card interface defining basic card functionalities (pay, withdraw, deposit). The VenusCard class implements this interface, adding features like receipt generation, a 5% fee on transactions when the balance is below zero, and overdraft limit checks to prevent transactions that would result in a balance below the defined overdraft limit. The main method provides a simple example of using the VenusCard class. Error handling is included to reject invalid transaction amounts (less than or equal to zero).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-03 UTC."],[],[]]