QA's approach 2 Java - Bank Interest Rate Calculator Application exhibiting RunTimePolymorphism

Learn Runtime Polymorphism through Practical Example Application


Bank Interest Rate Calculator Application exhibiting Runtime Polymorphism

used to retrieve the Interest Rate of Bank by providing the Bank details at Runtime.

Create the package structure with class names as follows


Create class name with Bank

package runTimePolymorphsimSimpleExmp;

public class Bank {

// Default interest Rate
public Float getInterest() {
return 6.5f;
}
}

Create class name with Sbi extending Bank

package runTimePolymorphsimSimpleExmp;

public class Sbi extends Bank{

// SBI specific interest Rate

public Float getInterest() {

return 6.25f;

}
}

Create class name with Hdfc extending Bank

package runTimePolymorphsimSimpleExmp;

public class Hdfc extends Bank{

// HDFC specific interest Rate

public Float getInterest() {

return 6.75f;

}
}

Create class name with Icici extending Bank

package runTimePolymorphsimSimpleExmp;

public class Icici extends Bank {

// ICICI specific interest Rate
public Float getInterest() {
return 6.5f;
}
}

Create helper class with name RateCalculator

package runTimePolymorphsimSimpleExmp;

import java.util.Scanner;

public class RateCalculator {

static void rate() {
Bank b = null;
Scanner sc = null;
System.out.println("Please select the Bank: Sbi/Icici/Hdfc \n");
sc = new Scanner(System.in);
String input2 = sc.next();
String input1 = "runTimePolymorphsimSimpleExmp";
String input = input1 + "." + input2;
if (input2.equals("Sbi") || input2.equals("Icici") || input2.equalsIgnoreCase("Hdfc")) {

try {
b = (Bank) Class.forName(input).newInstance();
Float rT = b.getInterest();
System.out.println("Rate of Interest of the selected Bank is:" + rT);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Entered Bank name is nnot valid, please try again");
rate();
}

}

}

Create Execution logic class with name BankROICalculatorApp

package runTimePolymorphsimSimpleExmp;

import java.util.Scanner;

public class BankROICalculatorApp {

public static void main(String[] args) throws Exception {
int i = 0;
while (i == 0) {
RateCalculator.rate();
System.out.println(
"Do you want to check other banks interest rate, to Continue: Please type YES else NO to terminate");
if ((new Scanner(System.in)).next().equalsIgnoreCase("YES"))
continue;
else {
System.out.println("Thank for using the BankROICalculatorApp");
i = 1;
}
}
}

}


Debugging the output

1. Provide Input other than Icici or Sbi or Hdfc, the output on the console will be as follows:


2. Provide Input from any of the three banks Icici or Sbi or Hdfc


3. Provide input as yes


4.Provide Input from any of the three banks Icici or Sbi or Hdfc


5. Provide Input as no




Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?