In: Computer Science
You have been hired as a programmer by a major bank. Your first project is a small banking transaction system. Each account consists of a number and a balance. The user of the program (the teller) can create a new account, as well as perform deposits, withdrawals, balance inquiries, close accounts, etc..
Initially, the account information of existing customers is to be read into an array of BankAccount objects. The private data members of the BankAccount Class will include: first name, last name, social security number, account number, account type (Checking, Savings, or CD), and account balance. The bank can handle up to MAX_NUM accounts. Use the following function to read in the data values:
public static int readAccts(BankAccount[] account, int maxAccts);
This method fills up the array (up to maxAccts) and returns the actual number of accounts read in (referred to as numAccts).
After initialization, print the initial database of accounts. Use method printAccts() described below.
The program then allows the user to select from the following menu of transactions:
Select one of the following: W - Withdrawal D - Deposit N - New account B - Balance I - Account Info X - Delete Account Q - Quit
Use the following method to produce the menu: public static void menu()
This method only displays the menu. The main program then
prompts the user for a selection. You should verify that the user
has typed in a valid selection (otherwise print out an error
message and repeat the prompt).
Once the user has entered a selection, one of the following methods
should be called to perform the specific transaction. At the end,
before the user quits, the program prints the contents of the
database.
public static int findAcct(BankAccount[] account, int numAccts, int reqAccount);
This method returns the index of reqAccount in the BankAccount array if the account exists, and -1 if it doesn't. It is called by all the remaining methods.
public static void withdrawal(BankAccount[] account, int num_accts);
This method prompts the user for the account number. If the account does not exist, it prints an error message. Otherwise, it asks the user for the amount of the withdrawal. If the account does not contain sufficient funds, it prints an error message and does not perform the transaction.
public static void deposit(BankAccount[] account, int num_accts);
This method prompts the user for the account number. If the account does not exist, it prints an error message. Otherwise, it asks the user for the amount of the deposit.
public static int newAcct(BankAccount[] account, int num_accts); This method prompts the user for a new account number. If the account already exists, it prints an error message. Otherwise, it adds the account to the database. The method then prompts the user to enter the new depositor’s first name, last name, social security number, the account type (Checking, Savings, or CD), and the initial opening deposit.. The method returns the new number of accounts in the database.
public static int deleteAcct(BankAccount[] account, int num_accts); This method prompts the user for an account number. If the account does not exist, or if the account exists but has a non-zero balance, it prints an error message. Otherwise, it closes and deletes the account. It returns the new number of accounts.
public static void balance(BankAccount[] account, int num_accts); This method prompts the user for an account number. If the account does not exist, it prints an error message. Otherwise, it prints the account balance.
public static void accountInfo(BankAccount[] account, int num_accts); This method prompts the user for a social security number (SSN). If no account exists for this SSN, it prints an error message. Otherwise, it prints the complete account information for all of the accounts with this SSN.
public static void printAccts(BankAccount[] account, int num_accts); This method prints a table of the complete account information for every active account.
Make sure that there is at least one depositor that has multiple accounts at the bank.
#1: Use nested classes: 1. A BankAccount consists of a Depositor, an account number, an account type, and a balance. 2. A Depositor has a Name and a social security number. 3. A Name consists of first and last names.
#2: Use a constructor to initialize the data members of a new account (including the initial accounts of the database). Hint: a constructor is a method that can be called.
Notes: 1. All output must be file directed 2. Only output must go to the file - not interactive prompts and menus. 3. No global variables are allowed 4. The program and all methods must be properly commented. 5. All data members of classes are to be private 6. Add accessor (getter) methods and mutator (setter) methods to all classes as appropriate 7. All I/O should be done outside of the BankAccount class implementation. 8. All I/O should be done within the methods of the class that contains the main() method.
Sample input:
Doe John M brown 34 lawyer 96345.87 Gold Jane F blonde 43 doctor 123456.78 Dillon Tom M black 34 teacher 87654.32
Code-
BankAccount class-
public class BankAccount {
//attributes for Bankaccount class
private String firstname;
private String lastname;
private int ssn;
private int acno;
private String actype;
private int acb;
//Constructor for the initialization
public BankAccount(String firstname, String lastname,
int ssn, int acno, String actype, int acb) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.ssn = ssn;
this.acno = acno;
this.actype = actype;
this.acb = acb;
}
//Getter and setter methods for the attributes
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getSsn() {
return ssn;
}
public void setSsn(int ssn) {
this.ssn = ssn;
}
public int getAcno() {
return acno;
}
public void setAcno(int acno) {
this.acno = acno;
}
public String getActype() {
return actype;
}
public void setActype(String actype) {
this.actype = actype;
}
public int getAcb() {
return acb;
}
public void setAcb(int acb) {
this.acb = acb;
}
}
Banking Transaction System class- (Containing main method and all the other methods)
import java.util.*;
public class Bts {
public static void main(String[] args) {
//Scanner class to receive the inputs
Scanner sc=new Scanner(System.in);
//Variable to store the maximum value
int MAX_NUM,i=0,num_accts;
char ch;
System.out.println("Enter the maximum number of bank
accounts that can be inserted");
MAX_NUM = sc.nextInt();
BankAccount[] account=new BankAccount[MAX_NUM];
while(sc.hasNext())
{
account[i].setFirstname(sc.next());
account[i].setLastname(sc.next());
account[i].setSsn(sc.nextInt());
account[i].setAcno(sc.nextInt());
account[i].setActype(sc.next());
account[i].setAcb(sc.nextInt());
i++;
}
// To store the actual number of Customers
(Module1)
num_accts=readAccts(account,MAX_NUM);
//Then printing the data (Module2)
printAccts(account, num_accts);
// Menu method
//calling the menu method to validate the input
menu();
}
// method to read the input of the Bank account
details
public static int readAccts(BankAccount[] account, int
maxAccts)
{
int count=0;
//iterating over the bank account
object array to know the actual number of details stored
for(BankAccount x:account)
{
if(x.getFirstname()==null)
return count;
else
count++;
}
return count;
}
// method to print the database information of all the
customers at the bank
public static void printAccts(BankAccount[] account,
int num_accts)
{
for(BankAccount i:account)
{
//Printing all
the details of a particular Customer
System.out.println(i.getFirstname()+" "+i.getLastname()+"
"+i.getSsn()+" "+i.getAcno()+" "+i.getActype()+i.getAcb());
System.out.println();
}
}
// Method to display a menu
public static void menu()
{
Scanner sc=new
Scanner(System.in);
char ch;
System.out.println("Select an
operation");
ch = sc.next().charAt(0);
if(ch=='W')
withdrawal(account,num_accts);
else if(ch=='D')
deposit(account,
num_accts);
else if(ch=='N')
newAcct(account,
num_accts);
else if(ch=='B')
balance(account,num_accts);
else if(ch=='I')
accountInfo(account,num_accts);
else if(ch=='X')
deleteAcct(account,num_accts);
else if(ch=='Q')
break;
else
System.out.println("Enter a valid selection");
}
// Withdrawal method
public static void withdrawal(BankAccount[] account,
int num_accts)
{
Scanner sc=new
Scanner(System.in);
int a,wd;
//Receiving the A/c number from the
user
System.out.println("Enter the
account number");
a=sc.nextInt();
for(BankAccount i:account)
{
if(a==i.getAcno())
{
System.out.println("Enter the withdrawal amount");
wd=sc.nextInt();
//Checking if
there are funds
if(wd>i.getAcb())
{
System.out.println("Insufficient funds");
break;
}
//updating the
account balance
else
{
i.setAcb(i.getAcb()-wd);
break;
}
}
}
//After complete search
System.out.println("Account number doesnt exist");
}
//Deposit method
public static void deposit(BankAccount[] account, int
num_accts)
{
Scanner sc=new
Scanner(System.in);
int a,wd;
//Receiving the A/c number from the
user
System.out.println("Enter the
account number");
a=sc.nextInt();
for(BankAccount i:account)
{
if(a==i.getAcno())
{
System.out.println("Enter the deposit amount");
wd=sc.nextInt();
//Checking if
there are funds
if(wd>i.getAcb())
{
System.out.println("Insufficient funds");
break;
}
//updating the
account balance
else
{
i.setAcb(i.getAcb()+wd);
break;
}
}
}
//After complete search
System.out.println("Account number doesnt exist");
}
//New Account updation
public static int newAcct(BankAccount[] account, int
num_accts)
{
Scanner sc=new
Scanner(System.in);
int a;
//Receiving the A/c number from the
user
System.out.println("Enter the
account number");
a=sc.nextInt();
//checking if the account
exists
for(BankAccount i:account)
{
if(a==i.getAcno())
{
System.out.println("Account already exists in
the database");
break;
}
}
//After the for loop, it is clear
that the account doesn't exist, therefore new account will be
added
account[num_accts].setFirstname(sc.next());
account[num_accts].setLastname(sc.next());
account[num_accts].setSsn(sc.nextInt());
account[num_accts].setAcno(sc.nextInt());
account[num_accts].setActype(sc.next());
account[num_accts].setAcb(sc.nextInt());
//returning the updated number of
customers
return num_accts+1;
}
//Retrieving the balance of a particular account
public static void balance(BankAccount[] account, int
num_accts)
{
Scanner sc=new
Scanner(System.in);
int a;
//Receiving the A/c number from the user
System.out.println("Enter the account number");
a=sc.nextInt();
//checking if the account exists
for(BankAccount i:account)
{
if(a==i.getAcno())
{
//Printing the
account balance if account number is found
System.out.println(i.getAcb());
break;
}
}
//After the for loop, if Ac number isn't found the
control shifts here
System.out.println("Account number doesnt
exist");
}
//Retrieving the account information of a particular
customer
public static void accountInfo(BankAccount[] account,
int num_accts)
{
Scanner sc=new
Scanner(System.in);
int a;
//Receiving the A/c number from the
user
System.out.println("Enter the
Social security number");
a=sc.nextInt();
//checking if the account exists
and printing all the records with the existing SSN
for(BankAccount i:account)
{
if(a==i.getSsn())
{
//Printing the account balance if account number
is found
System.out.println(i.getFirstname()+"
"+i.getLastname()+" "+i.getSsn()+" "+i.getAcno()+"
"+i.getActype()+i.getAcb());
break;
}
}
}
public static int deleteAcct(BankAccount[] account,
int num_accts)
{
Scanner sc=new
Scanner(System.in);
int a;
//Receiving the A/c number from the
user
System.out.println("Enter the
account number");
a=sc.nextInt();
//checking if the account
exists
for(BankAccount i:account)
{
//Checking if
the account exists and has a zero balance before deletion of the
account
if(a==i.getAcno() && i.getAcb()==0)
{
//updating the attributes of the object to null
(Deleting the object)
i.setFirstname(null);
i.setLastname(null);
i.setSsn(0);
i.setAcno(0);
i.setActype(null);
break;
}
}
//updating the number of customers
after deletion
return num_accts-1;
}
}