This needs to be written in Java BankAccount class -logic for addBankAccount( )
ID: 3724215 • Letter: T
Question
This needs to be written in Java
BankAccount class -logic for addBankAccount( )
BankAccount Class data members:
--> protected int accNumber;
-->protected Person accHolder;
-->protected double balance;
Methos addBankAccount( ):
READ valid accountNumber from user (positive integer only)
(PUT message to enter accountNumber
GET accountNumber
WHILE accountNumber is not valid
PUT error message...re-enter
GET accountNumber
ENDWHILE
)
READ firstName from user
READ lastname from user
READ phoneNumber from user
READ emailAddress from user
READ valid balance from user
Create accHolder by invoking constructor
Class Person: This class will contain common data members for a person f lastName, phoneNumber emailAddress. rstName Methods constructor () to create accHolder object. o o Required getters Class BankAccount . This class will be the base class and for data members contain the common all Bank Accounts (ie accountNumber accHolder, balance). Here, accHolder is an object of class Person. Methods o toString):Stringreturns the data of the account formatted to display o addBankAccount): boolean prompts user to enter data for this object from keyboard edits data, and doesn't allow user to continue with bad data o updateBalance (double) - updates the balance in the object by the parameter amount monthlyAccountUpdate() processes the object with monthly update (make it abstract for base class.)Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Person{
private String firstName;
private String lastName;
private int phoneNumber;
private String emailAddress;
public Person(String fName, String lName, int phNumber, String email){
this.firstName = fName;
this.lastName = lName;
this.phoneNumber = phNumber;
this.emailAddress = email;
}
public String getFirstName() { return this.firstName;}
public String getLastName() { return this.lastName;}
public int getPhoneNumber() { return this.phoneNumber;}
public String getEmailAddress() {return this.emailAddress; }
}
class Bank{
protected int accountNumber;
protected Person accHolder;
protected int balance;
public void updateBalance(int balance){
this.balance = balance;
System.out.println("Balance updated to::"+balance);
}
public void addBankAccount(){
try{
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Account number:");
int accountNumber;
boolean isValid = true;
do{
accountNumber = Integer.parseInt(reader.readLine());
if(accountNumber < 0) {
System.out.println("Bad Account Number, Enter Again:");
isValid = false;
}
}while(!isValid);
this.accountNumber = accountNumber;
System.out.println("Enter firstName:");
String fName = reader.readLine();
System.out.println("Enter lastName:");
String lName = reader.readLine();
System.out.println("Enter PhoneNumber:");
int phone = Integer.parseInt(reader.readLine());
System.out.println("Enter EmailAddress:");
String email = reader.readLine();
this.accHolder = new Person(fName, lName, phone, email);
System.out.println("Enter Balance:");
int balance = Integer.parseInt(reader.readLine());
updateBalance(balance);
System.out.println(this.toString());
}catch(IOException e){e.printStackTrace();}
}
@Override
public String toString(){
return this.accountNumber +" "+ this.accHolder.getFirstName()+","+this.accHolder.getLastName()+","+
this.accHolder.getPhoneNumber()+","+this.accHolder.getEmailAddress()+", "+ this.balance;
}
}
public class MyClass {
public static void main(String args[]) {
Bank bn = new Bank();
bn.addBankAccount();
bn.updateBalance(200);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.