Question: Learning objective: Take multiple actions based on a decision. Class to edit: Exercise6 When attempting this exercise, you should refer to the following Java documentation
Learning objective: Take multiple actions based on a decision.
Class to edit: Exercise6
When attempting this exercise, you should refer to the following Java documentation for class BankAccount:
Javadoc
Class BankAccount
Constructor Summary
| Constructor and Description |
|---|
| BankAccount() Creates a new bank account, asking the user to enter the account name and initial balance. |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| void | deposit(double amount) Deposits the specified amount of money into this account. |
| double | getBalance() Gets the balance of this account. |
| void | showBalance() Shows the balance of this account. |
| void | transfer(double amount, BankAccount targetAccount) Transfers the specified amount of money from this account into the target account. |
| void | withdraw(double amount) Withdraws the specified amount of money into this account. |
Write a program that executes the following steps:
Create a new bank account.
Show the balance of the bank account.
If the balance of the bank account is >= 20 dollars, do the following steps:
Print "Purchasing movie ticket." including the full stop at the end of the sentence.
Withdraw $20 from the account.
Show the balance of the bank account again.
Otherwise, print "You need more money to buy a movie ticket." including the full stop at the end of the sentence.
The following sample I/O shows how your program should behave when the account has at least $20:
Creating a new bank account Enter account name: user inputs Shelly Martin Enter initial balance: $ user inputs 567.00 Shelly Martin's account has $567.00 Purchasing movie ticket. Shelly Martin's account has $547.00
Otherwise, your program should behave as follows:
Creating a new bank account Enter account name: user inputs Shelly Martin Enter initial balance: $ user inputs 7.00 Shelly Martin's account has $7.00 You need more money to buy a movie ticket.
Hint: Apply a pair of "{" and "}" braces around the entire 3 steps of the if case, and then a separate pair of braces around the 1 step of the else case.
Input.java class
import java.util.*;
/** * The Input class provides a set of static methods for easily asking * the user questions and reading their answers. */ public class Input { private static final Scanner scanner = new Scanner(System.in); private Input() {}
/** * Asks the user the given question, waits for the user to enter a string * at the keyboard, and then returns this string. * * @param question the question to be printed * @return the string that the user entered as an answer to the question */ public static String askString(String question) { System.out.print(question + " "); return scanner.nextLine(); }
/** * Asks the user the given question, waits for the user to enter a integer * at the keyboard, and then returns this integer. * * @param question the question to be printed * @return the integer that the user entered as an answer to the question */ public static int askInt(String question) { System.out.print(question + " "); int result = scanner.nextInt(); scanner.nextLine(); return result; }
/** * Asks the user the given question, waits for the user to enter a double * at the keyboard, and then returns this double. * * @param question the question to be printed * @return the double that the user entered as an answer to the question */ public static double askDouble(String question) { System.out.print(question + " "); double result = scanner.nextDouble(); scanner.nextLine(); return result; }
/** * Asks the user the given question, waits for the user to enter a * single character at the keyboard, and then returns this character. * * @param question the question to be printed * @return the character that the user entered as an answer to the question */ public static char askChar(String question) { System.out.print(question + " "); return scanner.nextLine().charAt(0); }
/** * Asks the user the given question, waits for the user to enter a boolean * at the keyboard, and then returns this boolean. * * @param question the question to be printed * @return the boolean that the user entered as an answer to the question */ public static boolean askBoolean(String question) { System.out.print(question + " "); boolean result = scanner.nextBoolean(); scanner.nextLine(); return result; } } Exercise6.java class
import java.util.Scanner;
public class Exercise6 { public static void main(String[] args) { // INSERT YOUR CODE HERE
Scanner scanner = new Scanner(System.in); BankAccount account = new BankAccount(); System.out.print("Enter account name: "); String name = scanner.nextLine(); System.out.print("enter initial balance: $ "); double bal = scanner.nextDouble(); account.deposit(bal); System.out.println(name + "\'s account has $" + account.getBalance()); account.withdraw(20); if (account.getBalance() >= 20) { System.out.println("Purchasing movie ticket."); System.out.println(name + "\'s account has $" + account.getBalance()); } else { System.out.println("You need more money to buy a movie ticket."); }
} }
Output:

Question: It asks the question twice because the BankAccount account = new BankAccount(); line of code asks the question already. How can I adjust my code as it seems to add the two sets of prompts together.
Error message:
Creating a new bank account Enter account name: Jack Enter initial balance: $300.25 Enter account name: Jack enter initial balance: $300.25 Jack's account has $600.5 Purchasing movie ticket. Jack's account has $580.5 + Enter account name: Enter initial balance: $ Jack's account has $300.25 + Purchasing movie ticket. + Jack's account has $280.25
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
