A bank in your town updates its customers’ accounts at the end of each month. The bank offers two types of accounts: savings and checking. Every customer must maintain a minimum balance. If a customer’s balance falls below the minimum balance, there is a service charge of $10.00 for savings accounts and $25.00 for checking accounts. If the balance at the end of the month is at least the minimum balance, the account receives interest as follows:
a. Savings accounts receive 4% interest.
b. Checking accounts with balances of up to $5000 more than the minimum balance receive 3% interest; otherwise, the interest is 5%.
This Java program using Eclipse Indigo reads a customer’s account number ( int type ), account type ( char type; s or S for savings, c or C for checking), minimum balance that the account should maintain, and current balance. The program should then output the account number, account type, current balance, and new balance or an appropriate error message. Test program by running it five times, using the following data:
46728 S 1000 2700
87324 C 1500 7689
79873 S 1000 800
89832 C 2000 3000
98322 C 1000 750
[code]
import java.util.*;
public class bank
{
public static void main (String [] args)
{int num,error=1,itype=0;
char type =0;
double min,cur =0,balanc =0,rate=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter account number: ");
num=in.nextInt();
while(error==1)
{
System.out.println("Enter account type(s-savings or c-checking):");
type=in.next().charAt(0);
if(type=='c'||type=='C')
{itype=1;
error=0;
rate=3/100.;
}
else if(type=='s'||type=='S')
{itype=0;
error=0;
rate=4/100.;
}
if(error==1)
System.out.println("Invalid type-re enter");
}
System.out.println("Enter minimum balance: ");
min=in.nextDouble();
System.out.println("Enter current balance: ");
cur=in.nextDouble();
balance = cur;
if(itype==1)
{ if(cur>min+5000)
rate=5/100.;
else if(cur cur-=25;
}
else
if(cur cur-=10;
cur=cur+rate*cur;
System.out.printf("After interest and fees your balance is = $%.2f\n",cur);
System.out.println("Your account number: " + num);
System.out.println("Your account type: " + type);
System.out.printf("Your starting balance: $%.2f\n ", balanc);
System.out.printf("Your new balance: $%.2f\n", cur);
}
}
[/code]

Respuesta :

Answer:

This is your corrected code and the output of each test example. I have also added comments with the provided code to make the code understandable. I have also changed itype variable to from int to String in order to print the account type (Savings or Checking) in output.

import java.util.*;

public class bank{  //class name

public static void main (String [] args)  //start of main function body

{int num,error=1; //declare variables

String itype=" "; //stores Checking or Savings account type

char type =0;  //type variable which is one of savings S or checking C

double min,cur =0,balance =0,rate=0;

//declare variables for minimum balance, current balance, interest rate

Scanner in=new Scanner(System.in);

System.out.println("Enter account number: ");  //prompts user to enter acc no

num=in.nextInt();  //reads input account number

while(error==1) {  

//asks user to enter account type C or S

System.out.println("Enter account type(s-savings or c-checking):");

type=in.next().charAt(0);  //reads the input character of account type

if(type=='c'||type=='C')  //if user inputs c or C

{itype= "Checking";  //set itype to Checking when user input c or C

error=0;  //set value of error to 0 means user entered valid type input

rate=3/100.; }  // Savings accounts receives  3% interest

else if(type=='s'||type=='S')  //if user enters S or s that shows Savings account

{itype= "Savings";  //set itype to Savings when user input s or S

error=0;  //set error to 0 means there is no error

rate=4/100.; }  //Savings accounts receives 4% interest

if(error==1)  //in case of error in giving input

System.out.println("Invalid type-re enter"); }  //asks user to input again

System.out.println("Enter minimum balance: ");  //asks user to enter min bal

min=in.nextDouble();  //reads value of input minimum balance

System.out.println("Enter current balance: ");

// reads value of input current balance

cur=in.nextDouble();

balance = cur;  

if(itype=="Checking") //if the account type is checking

{ if(cur>min+5000) //Checking accounts interest is 5%

{rate=5/100.;

cur=cur+rate*cur; //computes new balance

System.out.printf("New balance: $%.2f\n", cur);} //returns new balance value

/*If a customer’s balance falls below the minimum balance, there is a service charge of $25.00 for checking accounts */

else if(cur<min)

{cur-=25;

System.out.printf("New balance: $%.2f\n", cur);} returns the value of new

}

if(itype=="Savings"){ //if account type is Savings

/*If a customer’s balance falls below the minimum balance, there is a service charge of $10.00 for savings accounts */

if(cur<min)

{cur-=10;

System.out.printf("New balance: $%.2f\n", cur);}

else

//Savings accounts receive 4% interest

{cur=cur+rate*cur;

System.out.printf("New balance: $%.2f\n", cur);}}

/* as the program should output account number, account type, current balance, and new balance so i have commented out the extra print statements below */

//System.out.printf("After interest and fees your balance is = $%.2f\n",cur);

System.out.println("Account Number: " + num);

System.out.println("Account type: " + itype);

System.out.printf("Current balance: $%.2f\n ", balance);  //the result is //displayed up to 2 decimal place .2f   } }

Explanation:

Following is the output of each test example:

46728 S 1000 2700

Account Number: 46728                                            

Account type: Savings                                                                                          

Current balance: $2700.00                                                                                

New balance: $2808.00

87324 C 1500 7689

Account Number: 87324                                                                                      

Account type: Checking                                                                                        

Current balance: $7689.00                                                                                  

New balance: $8073.45

79873 S 1000 800

Account Number: 79873                                                                                    

Account type: Savings                                                                                        

Current balance: $800.00  

New balance: $790

89832 C 2000 3000

Account Number: 89832                                                                                      

Account type: Checking                                                                                        

Current balance: $3000.00                                                                                  

New balance: $3090.00

98322 C 1000 750  

Account Number: 98322

Account Type: Checking  

Current balance: $1000.00

New Balance: $725.00

Ver imagen mahamnasir