This is my current code for ide program. There are 3 classes. test class do not need to…
- User
This is my current code for ide program. There are 3 classes. test class do not need to be edited. "public class EWallet {
private String id; private double balance; //complete this public EWallet(String id, double balance) { } //complete this public void deductAmount(double amount) throws InsufficientBalanceException { }}
public class InsufficientBalanceException extends Exception {
//complete this class public double getInsufficientAmount() { //complete this return 0; }}
public class Test {
public static void main(String[] args) { try { EWallet card = new EWallet("A123", 5); card.deductAmount(10); System.out.println("Test case failed"); } catch (InsufficientBalanceException ex) { try { assert ex.getInsufficientAmount() == 5; System.out.println("Test case passed"); } catch (AssertionError e) { System.out.println("Test case failed"); } } }}"
edit the EWallet class and InsufficientBalanceException class according to the following requirements so that when test class is run, the test case passes. "The EWallet class deductBalance(double amount) method subtract amount from balance. If there is insufficient balance in the EWallet object for deduction, it will throw InsufficientBalanceException. Task: A partial completed skeleton code for EWallet and InsufficientBalanceException are given. Look through the codes and complete the missing codes. Hints are given in the codes as comments."
- Assistant
In order to make the test case pass, you should implement the EWallet class constructor and the deductAmount method, as well as complete the InsufficientBalanceException class.
Here's the edited code:
public class EWallet { private String id; private double balance; public EWallet(String id, double balance) { this.id = id; this.balance = balance; } public void deductAmount(double amount) throws InsufficientBalanceException { if (balance >= amount) { balance -= amount; } else { throw new InsufficientBalanceException(balance); } } } public class InsufficientBalanceException extends Exception { private double insufficientAmount; public InsufficientBalanceException(double insufficientAmount) { this.insufficientAmount = insufficientAmount; } public double getInsufficientAmount() { return insufficientAmount; } } public class Test { public static void main(String[] args) { try { EWallet card = new EWallet("A123", 5); card.deductAmount(10); System.out.println("Test case failed"); } catch (InsufficientBalanceException ex) { try { assert ex.getInsufficientAmount() == 5; System.out.println("Test case passed"); } catch (AssertionError e) { System.out.println("Test case failed"); } } } }When you run the Test class, it should output "Test case passed" if the EWallet and InsufficientBalanceException classes are implemented correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.