Problem First American Bank provides both normal bank services like checking acc
ID: 3920406 • Letter: P
Question
Problem First American Bank provides both normal bank services like checking accounts and mortgages to its customers. Each customer who uses checking services has a checking account. Each customer who gets a mortgage loan from the bank has a mortgage account. The bank executives ask a team of software analysts to develop a system to manage its customers' bank accounts. Section A: Define an abstract Java class named “BankAccount” in a package named “BANKACCOUNTS”. This class has three attributes: • (1) Acount number: an integer of 10 digits, defined as protected • (2) Account type: String (e.g.: "CHECKING" versus "MORTGAGE"), defined as protected • (3) Customer's full name: String, defined as protected Class BankAccount definition provides • a default constructor • get-methods and set-methods for the variable attributes, • a public method, named "displayAccountData()", that prints out the following pieces of data: account number, account type, and customer's full name in only one line. Section B: Define a Java class named “CheckingAccount” in the same package, BANKACCOUNTS, that extends the class BankAccount. Class CheckingAccount has only one attribute: • checking balance, that is defined as private. Class CheckingAccount definition provides: • a default constructor, • another constructor that accepts account number and customer's full name as two different parameters • get-method and set-method for the attribute checking balance, • a public method (displayAccountData()) that overrides the method of the same name of the superclass BankAccount and prints out the following pieces of data in only one line: o account number, o account type, o customer's full name, o checking balance (only 2 digits after decimal point) Section C: Define a Java class named “MortgageAccount” in the same package, BANKACCOUNTS, that extends the above class BankAccount. Class MortgageAccount has two attributes: • principal balance that is defined as private. • interest rate that is defined as private. Class MortgageAccount definition provides: • a default constructor, • another constructor that accepts account number and customer's full name as two different parameters • get-method and set-method for eachvariable attribute • a public method (displayAccountData()) that overrides the method of the same name of the superclass BankAccount and prints out the following pieces of data in only one line: o account number, o account type, o customer's full name, o the principal balance (only 2 digits after decimal point), o the interest rate (only 2 digits after decimal point) Section D: Write a Java program that can read inputs of bank accounts and print out the account data. The account can be either a checking account or a mortgage account. First, the program reads an input of a numeric account code that identifies which type of account that the user wants to work with. The values of account code are: • 1 for checking account • 2 for mortgage account The program should check to be sure that the user enters correct account code. Otherwise, the program asks the user to re-enter the code until it is correctly done. Then, for the checking account, the user enters the following pieces of data from the console: • account number, • customer's full name, • checking balance (only 2 digits after decimal point) For the mortgage account, the user enters the following pieces of data from the console: • account number, • customer's full name, • principal balance • interest rate The program should check to be sure that checking balance, principal balance, and interest rate, are not negative. The Java program is another Java class named “AccountDisplayer” in the same package, i.e. BANKACCOUNTS. To provide a solution to the problem, it is expected that inheritance and polymorphism are used in the coding.
Explanation / Answer
abstract class BankAccount{
protected long accountNumber;
protected String type;
protected String customerName;
public BankAccount(){
}
public void setAccountNumber(long ac){
accountNumber = ac;
}
public void setAccountType(String t){
if(t.equals("CHECKING") || t.equals("MORTGAGE")){
type = t;
}
}
public void setCustomerName(String name){
customerName = name;
}
public String getAccountType(){
return type;
}
public String getCustomerName(){
return customerName;
}
public long getAccountNumber(){
return accountNumber;
}
abstract public void displayAccountData();
}
CheckingAccount.java : --------------->>>>>>>..
class CheckingAccount extends BankAccount{
private double balance;
public CheckingAccount(){
}
public void setBalance(double b){
balance = b;
}
public double getBalance(){
return balance;
}
public void displayAccountData(){
System.out.println("Account Number : "+accountNumber);
System.out.println("Account Type : "+type);
System.out.println("Customer Name : "+customerName);
System.out.println("Checking Balance : "+balance);
}
}
--------------------------------------------------------------------------------------------------------------
class MortgageAccount extends BankAccount{
private double balance;
private int rate;
public MortgageAccount(){
}
public void setPrincipalBalance(double p){
balance = p;
}
public void setInterestRate(int r){
rate = r;
}
public int getInterestRate(){
return rate;
}
public double getPrincipalBalance(){
return balance;
}
public void displayAccountData(){
System.out.println("Account Number : "+accountNumber);
System.out.println("Account Type : "+type);
System.out.println("Customer Name : "+customerName);
System.out.println("Principal Balance : "+balance);
System.out.println("Interest Rate : "+rate+"%");
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class AccountDisplayer{
public static void main(String[] args) {
int type;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Enter the account code : ");
type = sc.nextInt();
if(type == 1 || type == 2){
break;
}else{
System.out.println("Enter code 1 for checking or 2 for mortgage ");
}
}
if(type == 1){
CheckingAccount b = new CheckingAccount();
System.out.println("Enter Customer First Name : ");
String n1 = sc.next();
System.out.println("Enter Customer Last Name : ");
String n2 = sc.next();
b.setCustomerName(n1+" "+n2);
System.out.println("Enter Account Number ");
b.setAccountNumber(sc.nextLong());
while(true){
System.out.println("Enter The Checking Balance : ");
double bal = sc.nextDouble();
if(bal >= 0){
b.setBalance(bal);
break;
}
}
b.setAccountType("CHECKING");
b.displayAccountData();
}
else{
MortgageAccount b = new MortgageAccount();
System.out.println("Enter Customer First Name : ");
String n1 = sc.next();
System.out.println("Enter Customer Last Name : ");
String n2 = sc.next();
b.setCustomerName(n1+" "+n2);
System.out.println("Enter Account Number ");
b.setAccountNumber(sc.nextLong());
while(true){
System.out.println("Enter The Principal Balance : ");
double bal = sc.nextDouble();
if(bal >= 0){
b.setPrincipalBalance(bal);
break;
}
}
while(true){
System.out.println("Enter The Interest Rate : ");
int rate = sc.nextInt();
if(rate >= 0){
b.setInterestRate(rate);
break;
}
}
b.setAccountType("MORTGAGE");
b.displayAccountData();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.