NOTE: The completed code must pass in the following compiler. Please make absolu
ID: 3573355 • Letter: N
Question
NOTE: The completed code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch09/c09_exp_9_15
PLEASE also make sure of the following:
-Post this as text, not as an image.
-Avoid making alterations to the original code unless necessary.
-Post the passing output- from the this compiler specifically- as evidence this code was accepted and passed.
~
The System.out.printf method has predefined formats for printing integers, floating-point numbers, and other data types. But it is also extensible. If you use the S format, you can print any class that implements the Formattable interface. That interface has a single method:
void formatTo(Formatter formatter, int flags, int width, int precision)
In this exercise, you should make the BankAccount class implement the Formattable interface. Ignore the flags and precision and simply format the bank balance, using the given width. In order to achieve this task, you need to get an Appendable reference like this:
Appendable a = formatter.out();
Appendable is another interface with a method
void append(CharSequence sequence)
CharSequence is yet another interface that is implemented by (among others) the String class. Construct a string by first converting the bank balance into a string and then padding it with spaces so that it has the desired width. Pass that string to the append method.
Use the following class as your tester class:
Complete the following file:
BankAccount.java
Explanation / Answer
import java.io.*;
class Curr_acct
{
final int max_limit=20;
final int min_limit=1;
final double min_bal=500;
private String name[]=new String[20];
privateint accNo[]=newint[20];
private String accType[]=new String[20];
privatedouble balAmt[]=newdouble[20];
staticint totRec=0;
{
for(int i=0;i<max_limit;i++)
{
name[i]="";
accNo[i]=0;
accType[i]="";
balAmt[i]=0.0;
}
}
{
String str;
int acno;
double amt;
boolean permit;
permit=true;
if (totRec>max_limit)
{
System.out.println(" Sorry we cannot admit you in our bank... ");
permit=false;
}
if(permit = true)
{
totRec++;
System.out.println(" =====RECORDING NEW ENTRY=====");
try{
accNo[totRec]=totRec; //Created AutoNumber to accNo so no invalid id occurs
System.out.println("Account Number : "+accNo[totRec]);
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Name : ");
System.out.flush();
name[totRec]=obj.readLine();
accType[totRec]="Current Account";
System.out.println("Account Type : "+accType[totRec]);
do{
System.out.print("Enter Initial Amount to be deposited : ");
System.out.flush();
str=obj.readLine();
balAmt[totRec]=Double.parseDouble(str);
}while(balAmt[totRec]<min_bal);
System.out.println(" ");
}
catch(Exception e)
{}
}
}
{
String str;
int acno=0;
boolean valid=true;
System.out.println(" =====DISPLAYING DETAILS OF CUSTOMER===== ");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account number : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.println(" Account Number : "+accNo[acno]);
System.out.println("Name : "+name[acno]);
System.out.println("Account Type : "+accType[acno]);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
}
catch(Exception e)
{}
}
{
String str;
double amt;
int acno;
boolean valid=true;
System.out.println(" =====DEPOSIT AMOUNT=====");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account No : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.print("Enter Amount you want to Deposit : ");
System.out.flush();
str=obj.readLine();
amt=Double.parseDouble(str);
balAmt[acno]=balAmt[acno]+amt;
System.out.println(" After Updation...");
System.out.println("Account Number : "+acno);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
}
catch(Exception e)
{}
}
{
String str;
double amt,checkamt,penalty;
int acno;
boolean valid=true;
System.out.println(" =====WITHDRAW AMOUNT=====");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account No : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.println("Balance is : "+balAmt[acno]);
System.out.print("Enter Amount you want to withdraw : ");
System.out.flush();
str=obj.readLine();
amt=Double.parseDouble(str);
checkamt=balAmt[acno]-amt;
if(checkamt >= min_bal)
{
balAmt[acno]=checkamt;
System.out.println(" After Updation...");
System.out.println("Account Number : "+acno);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
else
{
System.out.println(" Your Balance has gone down and so penalty is calculated");
maintain
penalty=((min_bal - checkamt)*20)/100;
balAmt[acno]=balAmt[acno]-(amt+penalty);
System.out.println("Now your balance revels : "+balAmt[acno]+" ");
}
}
}
catch(Exception e)
{}
}
}
class Sav_acct
{
final int max_limit=20;
final int min_limit=1;
final double min_bal=500;
private String name[]=new String[20];
privateint accNo[]=newint[20];
private String accType[]=new String[20];
privatedouble balAmt[]=newdouble[20];
staticint totRec=0;
{
for(int i=0;i<max_limit;i++)
{
name[i]="";
accNo[i]=0;
accType[i]="";
balAmt[i]=0.0;
}
}
{
String str;
int acno;
double amt;
boolean permit;
permit=true;
if (totRec>max_limit)
{
System.out.println(" Sorry we cannot admit you in our bank... ");
permit=false;
}
if(permit = true)
{
totRec++;
System.out.println(" =====RECORDING NEW ENTRY=====");
try{
accNo[totRec]=totRec;
System.out.println("Account Number : "+accNo[totRec]);
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Name : ");
System.out.flush();
name[totRec]=obj.readLine();
accType[totRec]="Saving Account";
System.out.println("Account Type : "+accType[totRec]);
do{
System.out.print("Enter Initial Amount to be deposited : ");
System.out.flush();
str=obj.readLine();
balAmt[totRec]=Double.parseDouble(str);
}while(balAmt[totRec]<min_bal); //Validation that minimun amount must be 500
System.out.println(" ");
}
catch(Exception e)
{}
}
}
{
String str;
int acno=0;
boolean valid=true;
System.out.println(" =====DISPLAYING DETAILS OF CUSTOMER===== ");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account number : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.println(" Account Number : "+accNo[acno]);
System.out.println("Name : "+name[acno]);
System.out.println("Account Type : "+accType[acno]);
balAmt[acno]=balAmt[acno]+(balAmt[acno]/10);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
}
catch(Exception e)
{}
}
{
String str;
double amt;
int acno;
boolean valid=true;
System.out.println(" =====DEPOSIT AMOUNT=====");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account No : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.print("Enter Amount you want to Deposit : ");
System.out.flush();
str=obj.readLine();
amt=Double.parseDouble(str);
balAmt[acno]=balAmt[acno]+amt;
System.out.println(" After Updation...");
System.out.println("Account Number : "+acno);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
}
catch(Exception e)
{}
}
{
String str;
double amt,checkamt;
int acno;
boolean valid=true;
System.out.println(" =====WITHDRAW AMOUNT=====");
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account No : ");
System.out.flush();
str=obj.readLine();
acno=Integer.parseInt(str);
if (acno<min_limit || acno>totRec)
{
System.out.println(" Invalid Account Number ");
valid=false;
}
if (valid==true)
{
System.out.println("Balance is : "+balAmt[acno]);
System.out.print("Enter Amount you want to withdraw : ");
System.out.flush();
str=obj.readLine();
amt=Double.parseDouble(str);
checkamt=balAmt[acno]-amt;
if(checkamt >= min_bal)
{
balAmt[acno]=checkamt;
//Displaying Depsit Details
System.out.println(" After Updation...");
System.out.println("Account Number : "+acno);
System.out.println("Balance Amount : "+balAmt[acno]+" ");
}
else
{
System.out.println(" As per Bank Rule you should maintain minimum balance of Rs
500 ");
}
}
}
catch(Exception e)
{}
}
}
class Bank_improved
{
publicstaticvoid main(String args[])
{
String str;
int choice,check_acct=1,quit=0;
choice=0;
Curr_acct curr_obj = new Curr_acct();
Sav_acct sav_obj = new Sav_acct();
System.out.println(" =====WELLCOME TO BANK DEMO PROJECT===== ");
while( quit!=1)
{
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type 1 for Current Account and Any no for Saving Account : ");
System.out.flush();
str=obj.readLine();
check_acct=Integer.parseInt(str);
}
catch(Exception e) {}
if(check_acct==1)
{
do
{
System.out.println(" Choose Your Choices ...");
System.out.println("1) New Record Entry ");
System.out.println("2) Display Record Details ");
System.out.println("3) Deposit...");
System.out.println("4) Withdraw...");
System.out.println("5) Quit");
System.out.print("Enter your choice : ");
System.out.flush();
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
str=obj.readLine();
choice=Integer.parseInt(str);
switch(choice)
{
case 1 :
curr_obj.newEntry();
break;
case 2 :
curr_obj.display();
break;
case 3 :
curr_obj.deposit();
break;
case 4 :
curr_obj.withdraw();
break;
case 5 : System.out.println(" .....Closing Current Account.....");
break;
default : System.out.println(" Invalid Choice ");
}
}
catch(Exception e)
{}
}while(choice!=5);
}
else
{
do
{
System.out.println("Choose Your Choices ...");
System.out.println("1) New Record Entry ");
System.out.println("2) Display Record Details ");
System.out.println("3) Deposit...");
System.out.println("4) Withdraw...");
System.out.println("5) Quit");
System.out.print("Enter your choice : ");
System.out.flush();
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
str=obj.readLine();
choice=Integer.parseInt(str);
switch(choice)
{
case 1 :
sav_obj.newEntry();
break;
case 2 :
sav_obj.display();
break;
case 3 :
sav_obj.deposit();
break;
case 4 :
sav_obj.withdraw();
break;
case 5 : System.out.println(" .....Closing Saving Account.....");
break;
default : System.out.println(" Invalid Choice ");
}
}
catch(Exception e)
{}
}while(choice!=5);
}
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print(" Enter 1 for Exit : ");
System.out.flush();
str=obj.readLine();
quit=Integer.parseInt(str);
}catch (Exception e){}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.