Design a class Bank that contains a number of bank accounts. Each account has an
ID: 644681 • Letter: D
Question
Design a class Bank that contains a number of bank accounts. Each account has an account number and a current balance. Add an accountNumber field to the BankAccount class. Store the bank accounts in an array list. Write a readFile method of the Bank class for reading the file with the format:
accountNumber1 balance1
accountNumber2 balance2
Implement read methods for the Bank and BankAccount classes. Write a sample program to read in a file with bank accounts, then prints the account with the highest balance. If the file is not properly formatted, give the user a chance to selecct another file.
Partial design of classes BankAcount, Bank, and BankReader.
import java.util.Scanner;
import java.io.IOException;
import java.util.NoSuchElementException;
/**
A bank account has a balance that can be changed by deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private int accountNumber;
/**
Constructs a bank account with a zero balance.
*/
/**
Constructs a bank account with a given account number and a initial balance.
*/
/**
Reads an account number and balance.
@param in the scanner
@return true if the data was read
false if the end of the stream was reached
*/
public void read(Scanner in) ___________________________
{
try
{
} catch
{
throw new _____________________________
}
}
Note: You need to check for incompatible input.
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
/**
Gets the current balance of the bank account.
@return the current balance
*/
/**
Gets the account number of the bank account.
@return the account number
*/
}
___________________________________________________
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
import java.util.NoSuchElementException;
/**
A bank contains account numbers and balances of each customer.
*/
public class Bank
{
private ArrayList<BankAccount> accountList;
/**
Construct a Bank object.
*/
/**
Reads a file with account numbers and balances and adds the accounts
to the bank.
@param filename the name of the file
*/
public void readFile(String filename) ________________________
{ //Create File object, a scanner object to read data from the file.
//call the method read
File reader = new File(filename);
Scanner in = new Scanner(reader);
try
{
}
finally
{
}
}
Note: call the method read
/**
Read a file with account numbers and balances and adds the accounts
to the bank.
@param in the scanner for reading the input
*/
private void read(Scanner in)___________________________
{
while (in.hasNext())
{//Create a BankAccount object
//input data for the BankAccount object
//Add the BankAccount object to the accountlist
}
}
/**
Add an account to the bank.
@param b the BankAccount reference
*/
/**
Gets the account with the highest balance.
@return the account with the highest balance
*/
public BankAccount getHighestBalance()
{
}
}
________________________________________________
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
/**
Reads bank accounts from a file and prints highest balance.
*/
public class BankReader
{
/**
Prompts for and reads name of file to process.
@param in Scanner from which to read
@return file name
*/
private static String getFileName(Scanner in)
{
String filename = null;
System.out.println("Enter file to process: ");
if (in.hasNext())
{
filename = in.next();
}
return filename;
}
public static void main(String[] args)
{
Bank bank = new Bank();
Scanner in = new Scanner(System.in);
String filename = getFileName(in);
boolean done = false;
while (____________________________________)
{
try
{//read the file
//display the highest balance
}
//catch file not found exception andget the file name again
catch
{
System.err.println(filename + " not found");
__________________________________
}
//catch for improperly formatted line and get the file name again
catch (IOException e)
{
System.err .println(filename + " contains improperly formatted line");
_______________________________________
}
}
}
}
Explanation / Answer
import java.io.*; class Curr_acct //CURRENT ACCOUNT CLASS { 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; //Intializing Methodpublicvoid initialize() { for(int i=0;imax_limit) { System.out.println(" Sorry we cannot admit you in our bank... "); permit=false; } if(permit = true) //Allows to create new entry { totRec++; // Incrementing Total Record 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) { 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(" Your Balance has gone down and so penalty is calculated"); //Bank policy is to charge 20% on total difference of balAmt and min_bal to be 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 //SAVING ACCOUNT CLASS { 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; //Intializing Methodpublicvoid initialize() { for(int i=0;imax_limit) { System.out.println(" Sorry we cannot admit you in our bank... "); permit=false; } if(permit = true) //Allows to create new entry { totRec++; // Incrementing Total Record 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]="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) { 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//For Current Account { 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 : //New Record Entry curr_obj.newEntry(); break; case 2 : //Displaying Record Details curr_obj.display(); break; case 3 : //Deposit... curr_obj.deposit(); break; case 4 : //Withdraw... 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//For Saving Account { 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 : //New Record Entry sav_obj.newEntry(); break; case 2 : //Displaying Record Details sav_obj.display(); break; case 3 : //Deposit... sav_obj.deposit(); break; case 4 : //Withdraw... 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.