In: Computer Science
/******************************************************************************** * INSTRUCTIONS * * This program calculates the total fee charged for multiple accounts * * Fix the errors so the program compiles and can produce the following output * * How many accounts do you have?: 2 * * Enter number of checks for account 1: 10 * * Enter number of checks for account 2: 10 * * The sum of the fees for 2 accounts is $22.00 * *********************************************************************************/ import java.util.Scanner; public class BankCharges { public static void main(String[] args) { //Variable declarations int numChecks; double perCheckFee; double totalFee; double totalFeesAllAccounts; //accumulator int numAccounts; //Constant declarations for base fee and per check fees final double BASE_FEE = 10.0; final double LESS_THAN_20_FEE = 0.10; final double TWENTY_TO_THIRTYNINE_FEE = 0.08; final double FORTY_TO_FIFTYNINE_FEE = 0.06; final double SIXTY_OR_MORE_FEE = 0.04; // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); //Getting the number of accounts System.out.print("How many accounts do you have?: "); numAccounts = keyboard.nextInt(); //validating number of accounts while (numAccounts <= 0) { System.out.println("There must be at least 1 account. Try again: "); numAccounts = keyboard.nextInt(); } //Running total loop to calculate fees for multiple accounts for(int i = 0; i <= numAccounts; i++) { // Get the number of checks written. System.out.print("Enter number of checks for account " + (i + 1) + ": "); numChecks = keyboard.nextInt(); // Determine the appropriate per-check fee. if (numChecks < 20) perCheckFee = LESS_THAN_20_FEE; else if (numChecks <= 39) perCheckFee = TWENTY_TO_THIRTYNINE_FEE; else if (numChecks <= 59) perCheckFee = FORTY_TO_FIFTYNINE_FEE; else perCheckFee = SIXTY_OR_MORE_FEE; //Calculate the total fee totalFee = BASE_FEE + numChecks * perCheckFee; //Totaling the fees from multiple accounts totalFeesAllAccounts = totalFee; }//end for loop //Display the sum of the fees for all of the accounts System.out.printf("The sum of the fees for %d accounts is $%,.2f\n", numAccounts, totalFeesAllAccounts); } }
Please find your solution below and if any doubt comment and do upvote.
CODE:
import java.util.Scanner;
public class BankCharges
{
public static void main(String[] args)
{
//Variable declarations
int numChecks;
double perCheckFee;
double totalFee;
double totalFeesAllAccounts = 0; //accumulator
int numAccounts;
//Constant declarations for base fee and per check fees
final double BASE_FEE = 10.0;
final double LESS_THAN_20_FEE = 0.10;
final double TWENTY_TO_THIRTYNINE_FEE = 0.08;
final double FORTY_TO_FIFTYNINE_FEE = 0.06;
final double SIXTY_OR_MORE_FEE = 0.04;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Getting the number of accounts
System.out.print("How many accounts do you have?: ");
numAccounts = keyboard.nextInt();
//validating number of accounts
while (numAccounts <= 0)
{
System.out.println("There must be at least 1 account. Try again: ");
numAccounts = keyboard.nextInt();
}
//Running total loop to calculate fees for multiple accounts
for(int i = 0; i <numAccounts; i++)
{
// Get the number of checks written.
System.out.print("Enter number of checks for account " + (i + 1) + ": ");
numChecks = keyboard.nextInt();
// Determine the appropriate per-check fee.
if (numChecks < 20)
perCheckFee = LESS_THAN_20_FEE;
else if (numChecks <= 39)
perCheckFee = TWENTY_TO_THIRTYNINE_FEE;
else if (numChecks <= 59)
perCheckFee = FORTY_TO_FIFTYNINE_FEE;
else
perCheckFee = SIXTY_OR_MORE_FEE;
//Calculate the total fee
totalFee = BASE_FEE + numChecks * perCheckFee;
//Totaling the fees from multiple accounts
totalFeesAllAccounts += totalFee;
}//end for loop
//Display the sum of the fees for all of the accounts
System.out.printf("The sum of the fees for %d accounts is $%,.2f\n", numAccounts, totalFeesAllAccounts);
}
}
OUTPUT: