In: Computer Science
please with java and in JOptionPane if possible
.bank charges 10.00 per month plus the following fees for a
checking account:
.10 each for less than 20 checks
. 08 each for 20 – 39 checks
.06 each for 40 – 59 checks
.04 each for 60 or more checks
The bank also charges an extra 15.00 if the balance of the account
falls below $400 (before any check fees are applied).
Write a program that reads the following:
The account number ( a String )
The beginning balance of an account
The number of checks written
The total amount of the checks
The program should subtract the total amount of the checks from the
balance; it should calculate the bank fees (all that apply); and it
should print an “end of month statement” which includes the
following:
The account number
The beginning balance
The total amount of the checks
The total charges
The final balance -- if the final balance
is negative, print a warning
Here is the code to the given problem.I have taken inputs from user and printed end of month statement using JOptionPane as asked.As per the question, only check amount needs to be deducted from balance, so bank charges are not deducted from balance.
Code:
import javax.swing.*;
public class Bank {
public static void main(String[] args){
double initialBalance,finalBalance,checksAmount=0,bankCharges=10;
int numOfChecks;
String accountNumber;
accountNumber= JOptionPane.showInputDialog("Enter account number :"); //prompts the user to enter account number
initialBalance=Double.parseDouble(JOptionPane.showInputDialog("Enter beginning balance:")); //prompts the user to enter beginning balance
numOfChecks=Integer.parseInt(JOptionPane.showInputDialog("Enter the total number of checks written:")); //prompts the user to enter number of checks
checksAmount=Double.parseDouble(JOptionPane.showInputDialog("Enter the total amount of checks :")); //prompts the user to enter the total amount of checks
if(initialBalance<400) //checks if initial balance is less tha $400
{
bankCharges+=15; //increments bankCharges by $15
}
if(numOfChecks<20) //checks if the number of checks is less than 20
{
bankCharges+=numOfChecks*0.10; //calculates check fees and increment bankCharges
}
else if(numOfChecks>=20 && numOfChecks<=39) //checks if there are 20-39 checks
{
bankCharges+=numOfChecks*0.08; //calculates check fees and increment bankCharges
}
else if(numOfChecks>=40 && numOfChecks<=59) //checks if there are 40-59 checks
{
bankCharges+=numOfChecks*0.06; //calculates check fees and increment bankCharges
}
else if(numOfChecks>=60) //checks if number of checks is greater than 60
{
bankCharges+=numOfChecks*0.04; //calculates check fees and increment bankCharges
}
finalBalance=initialBalance-checksAmount; //calculates final balance by subtracting check amount from initial balance
JOptionPane.showMessageDialog(null,"END OF MONTH STATEMENT \n"+
"Account number : "+accountNumber+
"\nBeginning Balance : $"+initialBalance+
"\nTotal Amount of checks : $"+checksAmount+
"\nTotal charges : $"+String.format("%.2f",bankCharges)+
"\nFinal Balance : $"+String.format("%.2f",finalBalance)+
(finalBalance<0?"\nWARNING! You have negative balance":"")); //prints end of month statement in a message dialog
}
}
Sample output: