95.5k views
1 vote
Write an interactive Java Program named BankBalanceDoWhile.java which makes use of JOptionPane for input. The program should request you to enter an investment amount. After the investment amount has been entered, it should ask you if you wish to see the balance of your investment after exactly a year. If you wish to see the balance of your investment after exactly a year, you must enter integer 1. The program will ask again after displaying the requested balance if you wish to see your balance after another year. You will keep inputting the integer 1 if you wish to see successive annual balances until the point you input integer 0 if you no longer want to view the next balance. Any other number entered is an error and the program should indicate that error to you, the user. If an error input is received, the program should not terminate but give you a message that you have provided invalid input and should return you to the correct year of balance calculation. For example, if you had entered, say 6, to indicate that you wanted to see the balance for year the program should tell you that the input you provided is not valid. It should then ask you if you still want to view your balance for year 4. This program should exclusively implement the Do... While loop and should not implement any methods apart from main().​

User ItsASecret
by
7.5k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

import javax.swing.JOptionPane;

public class BankBalanceDoWhile {

public static void main(String[] args) {

double investmentAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter the investment amount:"));

int option;

do {

option = Integer.parseInt(JOptionPane.showInputDialog("Do you wish to see the balance of your investment after exactly a year?\\" +

"Enter 1 to see the balance for the next year, or 0 to exit."));

if (option == 1) {

// Calculate balance for the next year

double balance = calculateBalance(investmentAmount);

JOptionPane.showMessageDialog(null, "Balance after one year: $" + balance);

} else if (option != 0) {

JOptionPane.showMessageDialog(null, "Invalid input. Please enter either 1 or 0.");

}

} while (option != 0);

JOptionPane.showMessageDialog(null, "Program exited. Thank you!");

}

public static double calculateBalance(double investmentAmount) {

// Perform the calculation for the next year's balance

// You can modify this method based on your specific calculation logic

return investmentAmount * 1.05; // Assuming a 5% annual interest rate

}

}

In this program, the main() method prompts the user to enter the investment amount using JOptionPane.showInputDialog(). Then, inside the do-while loop, it asks the user if they want to see the balance for the next year using JOptionPane.showInputDialog(). If the user enters 1, it calculates the balance for the next year by calling the calculateBalance() method and displays it using JOptionPane.showMessageDialog(). If the user enters any value other than 1 or 0, it displays an error message. The loop continues until the user enters 0 to exit the program.

Note: The calculateBalance() method is a placeholder for your specific calculation logic. You can modify it based on your requirements.

Make sure to save the program with the filename "BankBalanceDoWhile.java" and compile and run it using a Java compiler or IDE.

User Tkbx
by
8.8k points