Task - Generics - Bank Accounts #67
Replies: 11 comments
-
Beans##Interface package com.learn;
public interface AccountType {
<U extends Number> void deposit(U amount);
<U extends Number> void withdraw(U amount);
void showBalance();
}Savings Accountpackage com.learn;
public class SavingsAccount implements AccountType {
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount Deposited :" + amount + " Rs");
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount Withdrawn :" + amount + " Rs");
}
@Override
public void showBalance( ) {
System.out.println("Available Savings Balance (1000+40) = 1040" );
}
}Current Accountpackage com.learn;
public class CurrentAccount implements AccountType {
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount Deposited " + amount);
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount Withdrawn " + amount);
}
@Override
public void showBalance() {
System.out.println("Available Current Balance = 1000");
}
}Bank Account - Genericpackage com.learn;
public class BankAccount<T extends AccountType> {
private T type;
public BankAccount(T type) {
this.type = type;
}
public void showAccountType()
{
System.out.println(type.getClass().getName());
}
}Main Applicationpackage com.learn;
public class Main {
public static void main(String[] args) {
SavingsAccount save = new SavingsAccount();
save.deposit(400);
BankAccount<SavingsAccount> accountTypeObj = new BankAccount<>(save);
accountTypeObj.showAccountType();
}
}Screenshot |
Beta Was this translation helpful? Give feedback.
-
BeansAccount Type interfacepackage com.bank.application.entity;
public interface AccountType {
<U extends Number> void withdraw(U amount);
<U extends Number> void deposit(U amount);
void showBalance();
}CurrentAccount.javapackage com.bank.application.entity;
import java.text.DecimalFormat;
public class CurrentAccount implements AccountType {
private static final DecimalFormat df = new DecimalFormat("0.00");
private double availableBalance;
public double getAvailableBalance() {
return availableBalance;
}
public void setAvailableBalance(double availableBalance) {
this.availableBalance = availableBalance;
}
public CurrentAccount(double availableBalance) {
this.availableBalance = availableBalance;
}
public CurrentAccount() {
}
@Override
public <U extends Number> void withdraw(U amount) {
if (amount.doubleValue() > availableBalance) {
System.out.println(" InSufficient Balance");
} else {
availableBalance = availableBalance - amount.doubleValue();
System.out.println("Your Account is succesfully debited with INR : " + df.format(amount.doubleValue()));
}
System.out.println("Total Available Balance : " + df.format(availableBalance));
}
@Override
public <U extends Number> void deposit(U amount) {
availableBalance = availableBalance + amount.doubleValue();
System.out.println("Your Account is successfully credited with INR : " + amount.doubleValue());
System.out.println("Total Available Balance : " + df.format(availableBalance));
}
@Override
public void showBalance() {
System.out.println("Total Available Balance : " + df.format(availableBalance));
}
}SavingsAccount.javapackage com.bank.application.entity;
import java.text.DecimalFormat;
public class SavingsAccount implements AccountType {
private static final double INTEREST_RATE = 0.4;
private static final DecimalFormat df = new DecimalFormat("0.00");
private double availableBalance;
public double getAvailableBalance() {
return availableBalance;
}
public void setAvailableBalance(double availableBalance) {
this.availableBalance = availableBalance;
}
public SavingsAccount(double availableBalance) {
this.availableBalance = availableBalance;
}
public SavingsAccount() {
}
@Override
public <U extends Number> void withdraw(U amount) {
if (amount.doubleValue() > availableBalance) {
System.out.println(" InSufficient Balance");
} else {
availableBalance = availableBalance - amount.doubleValue();
System.out.println("Your Account is succesfully debited with INR : " + df.format(amount.doubleValue()));
}
System.out.println("Total Available Balance : " + df.format(availableBalance));
}
@Override
public <U extends Number> void deposit(U amount) {
availableBalance = availableBalance + amount.doubleValue();
System.out.println("Your Account is successfully credited with INR : " + amount.doubleValue());
System.out.println("Total Available Balance : " + df.format(availableBalance));
}
@Override
public void showBalance() {
System.out.println("Total Available Balance : " + df.format(availableBalance));
System.out.println("Annual Interest calculated : " + df.format(availableBalance * INTEREST_RATE));
}
}Generic class - BankAccount.javapackage com.bank.application.entity;
public class BankAccount<T extends AccountType> {
private T accountType;
public T getAccountType() {
return accountType;
}
public void setAccountType(T accountType) {
this.accountType = accountType;
}
public BankAccount(T accountType) {
super();
this.accountType = accountType;
}
public void showAccountType() {
System.out.println(" Account Type : " + accountType.getClass().getSimpleName());
}
}Main Applicationpackage com.bank.application;
import java.util.Scanner;
import com.bank.application.entity.AccountType;
import com.bank.application.entity.BankAccount;
import com.bank.application.entity.CurrentAccount;
import com.bank.application.entity.SavingsAccount;
public class BankingApplication {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println(" Welcome to the Banking Application");
System.out.println("Select the account : \n");
System.out.println("\t 1. Savings Account");
System.out.println("\t 2. Current Account");
System.out.print("Choose your Option : ");
int option = sc.nextInt();
switch (option) {
case 1:
savingsAccountServices();
break;
case 2:
currentAccountServices();
break;
default:
System.out.println("Invalid Option selected ");
break;
}
sc.close();
}
private static void currentAccountServices() {
// TODO Auto-generated method stub
CurrentAccount account = new CurrentAccount();
BankAccount<AccountType> bankAccount = new BankAccount<>(account);
bankAccount.showAccountType();
boolean exit = false;
while (!exit) {
displayAccountOptions();
System.out.print("Select your option : ");
int selection = sc.nextInt();
System.out.println();
switch (selection) {
case 1:
depositAmount(account);
break;
case 2:
withdrawAmount(account);
break;
case 3:
account.showBalance();
break;
case 4:
exit = true;
break;
default:
System.out.println("Invalid option selected !!!");
break;
}
}
System.out.println("You have successfully logged out !!!");
}
private static void savingsAccountServices() {
// TODO Auto-generated method stub
SavingsAccount account = new SavingsAccount();
BankAccount<AccountType> bankAccount = new BankAccount<>(account);
bankAccount.showAccountType();
boolean exit = false;
while (!exit) {
displayAccountOptions();
System.out.print("Select your option : ");
int selection = sc.nextInt();
System.out.println();
switch (selection) {
case 1:
depositAmount(account);
break;
case 2:
withdrawAmount(account);
break;
case 3:
account.showBalance();
break;
case 4:
exit = true;
break;
default:
System.out.println("Invalid option selected !!!");
break;
}
}
System.out.println("You have successfully logged out !!!");
}
private static void displayAccountOptions() {
// TODO Auto-generated method stub
System.out.println("Available Options : ");
System.out.println("\t 1. Deposit Amount ");
System.out.println("\t 2. WithDraw Amount ");
System.out.println("\t 3. Show Balance ");
System.out.println("\t 4. Exit ");
}
private static void depositAmount(AccountType account) {
System.out.print("Enter amount to be Deposited : ");
// checking the generic functionality in java by giving float value
Float depositAmount = sc.nextFloat();
account.deposit(depositAmount);
}
private static void withdrawAmount(AccountType account) {
System.out.print("Enter Withdraw Amount : ");
Float withdrawAmount = sc.nextFloat();
account.withdraw(withdrawAmount);
}
}Screenshot |
Beta Was this translation helpful? Give feedback.
-
Account Interfacepublic interface AccountType<U> {
void deposit(U amount) ;
void withdraw(U amount);
void showBalance() ;
}
SavingsAccountpublic class SavingsAccount<T> implements AccountType {
T balance ;
public SavingsAccount(T balance) {
super();
this.balance = balance;
}
@Override
public void showBalance() {
// TODO Auto-generated method stub
System.out.println("Current Balance :" + balance );
}
@Override
public void deposit(Object amount) {
// TODO Auto-generated method stub
System.out.println("Amound Deposisted" + amount);
}
@Override
public void withdraw(Object amount) {
// TODO Auto-generated method stub
System.out.println("Amount withdrawn" + amount);
}
}
Current Accountpublic class CurrentAccount<T> implements AccountType {
T balance ;
public CurrentAccount(T balance) {
super();
this.balance = balance;
}
@Override
public void showBalance() {
// TODO Auto-generated method stub
System.out.println("Current Balance :" + balance );
}
@Override
public void deposit(Object amount) {
// TODO Auto-generated method stub
System.out.println("Amound Deposisted" + amount);
}
@Override
public void withdraw(Object amount) {
// TODO Auto-generated method stub
System.out.println("Amount withdrawn" + amount);
}
}Bank Accountpublic class BankAccunt<T extends AccountType> {
T account ;
public T getaccount() {
return account;
}
public BankAccunt(T account) {
super();
this.account = account;
}
public void showAccaccount() {
System.out.println(account.getClass().getName());
}
}
Main Classpublic class Driver {
public static void main(String[] args) {
SavingsAccount saving = new SavingsAccount(10000);
saving.showBalance();
saving.deposit(2000);
saving.showBalance();
BankAccunt<SavingsAccount> s = new BankAccunt<>(saving);
s.showAccaccount();
CurrentAccount current = new CurrentAccount(1000);
current.deposit(1000);
BankAccunt<CurrentAccount> c = new BankAccunt<>(current) ;
c.showAccaccount();
}
}
|
Beta Was this translation helpful? Give feedback.
-
Codeinterface AccountType {
<U> void deposit(U amount);
<U> void withdraw(U amount);
void showBalance();
}
class SavingsAccount implements AccountType{
static final double rateOfInterest=0.04;
private double availableBalance=0;
@Override
public <U> void deposit(U amount) {
this.availableBalance=(double)amount;
System.out.println("Amount Deposited :"+amount);
}
@Override
public <U> void withdraw(U amount) {
if (this.availableBalance>100) {
this.availableBalance=this.availableBalance-(double)amount;
System.out.println("Amount Withdrawn :"+amount);
}else {
System.out.println("Insuffient Funds");
}
}
@Override
public void showBalance() {
this.availableBalance=this.availableBalance*rateOfInterest+this.availableBalance;
System.out.println(this.availableBalance);
}
}
class CurrentAccount implements AccountType {
private double availableBalance=0;
@Override
public <U> void deposit(U amount) {
this.availableBalance=(double)amount;
System.out.println("Amount Deposited :"+amount);
}
@Override
public <U> void withdraw(U amount) {
if (this.availableBalance>100) {
this.availableBalance=this.availableBalance-(double)amount;
System.out.println("Amount Withdrawn :"+amount);
}else {
System.out.println("Insuffient Funds");
}
}
@Override
public void showBalance() {
System.out.println(this.availableBalance);
}
}
class BankAccount<T extends AccountType> {
T type;
public void showAccountType(T type) {
System.out.println(type.getClass().getName());
}
}
public class Main1 {
public static void main(String[] args) {
SavingsAccount s1 = new SavingsAccount();
s1.deposit(1000.00);
s1.withdraw(500.00);
s1.showBalance();
BankAccount<SavingsAccount> b1 = new BankAccount<>();
b1.showAccountType(s1);
CurrentAccount c1 = new CurrentAccount();
BankAccount<CurrentAccount> b2 = new BankAccount<>();
c1.deposit(2000.00);
c1.withdraw(500.00);
c1.showBalance();
b2.showAccountType(c1);
}
} |
Beta Was this translation helpful? Give feedback.
-
package abcde;
public interface AccountType {
<U extends Number> void deposit(U amount);
<U extends Number> void withdraw(U amount);
void showBalance();
}
class SavingsAccount<U extends Number> implements AccountType {
@Override
public <U extends Number> void deposit(U amount){
System.out.println("deposit amount of SavingsAccount");
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("withdraw amount of SavingsAccount");
}
@Override
public void showBalance() {
System.out.println("balance of SavingsAccount is");
}
}
class CurentAccount<U extends Number> implements AccountType {
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("deposit amount of CurentAccount");
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("withdraw amount of CurentAccount");
}
@Override
public void showBalance() {
System.out.println("CurentAccount balance is");
}
}
class BankAccount<T extends AccountType>{
T type;
BankAccount(T type)
{
this.type=type;
}
public void showAccountType() {
System.out.println(type.getClass().getName());
}
public static void main(String[] args) {
SavingsAccount s=new SavingsAccount();
CurentAccount curentAccount=new CurentAccount();
s.deposit(100);
BankAccount<SavingsAccount> b=new BankAccount<>(s);
b.showAccountType();
}
}
|
Beta Was this translation helpful? Give feedback.
-
discu 67import java.util.*;
interface AccountType<U>{
void deposit(U amount);
void withdraw(U amount);
void showBalance();
}
class SavingsAccount<T> implements AccountType{
double balance;
static final double rateOfInterest = 0.04;
public SavingsAccount(T balance){
this.balance = (double)balance;
}
@Override
public void deposit(Object amount){
System.out.println("amount deposited: " + amount);
this.balance += (double)amount;
}
@Override
public void withdraw(Object amt){
if(balance < 0){
System.out.println("insufficent funds");
}
else{
System.out.println("amt withdrawl is: " + amt);
this.balance -= (double)amt;
}
}
@Override
public void showBalance(){
this.balance = this.balance * rateOfInterest + this.balance;
System.out.println("balance: " +balance);
}
}
class CurrentAccount<T> implements AccountType{
double balance;
public CurrentAccount(T balance){
this.balance = (double)balance;
}
@Override
public void showBalance() {
System.out.println("Current Balance : " + balance );
}
@Override
public void deposit(Object amount) {
System.out.println("Amound Deposisted: " + amount);
this.balance += (double)amount;
}
@Override
public void withdraw(Object amount) {
this.balance -= (double)amount;
System.out.println("Amount withdrawn:: " + amount);
}
}
class BankAccount<T extends AccountType>{
T account;
public BankAccount(T account){
this.account = account;
}
public T getaccount() {
return account;
}
public void showAccaccount() {
System.out.println("type of account-> " + account.getClass().getName());
}
}
public class Discussion67{
public static void main(String[] args) {
System.out.println("saving account******");
SavingsAccount saving = new SavingsAccount(25000.00);
saving.showBalance();
saving.deposit(4999.00);
saving.withdraw(400.00);
saving.showBalance();
System.out.println("current account******");
CurrentAccount current = new CurrentAccount(1200.00);
current.showBalance();
current.deposit(1200.00);
current.withdraw(200.00);
current.showBalance();
System.out.println("bounded account *****");
BankAccount<SavingsAccount> obj = new BankAccount<>(saving);
obj.showAccaccount();
BankAccount<CurrentAccount> cu = new BankAccount<>(current);
cu.showAccaccount();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface AccountType{
<U extends Number> void deposit (U amount);
<U extends Number> void withdraw (U amount);
void showBalance();
}
class SavingAccount implements AccountType{
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount Deposited :" + amount );
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount withdraw :" + amount );
}
@Override
public void showBalance() {
System.out.println("Available Savings Balance");
}
}
class CurrentAccount implements AccountType{
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount deposit :" + amount );
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount withdraw :" + amount );
}
@Override
public void showBalance() {
System.out.println("Available Current Balance ");
}
}
class BankAccount<T extends AccountType>{
private T type;
public BankAccount(T type) {
this.type = type;
}
public void showAccountType(){
System.out.println(type.getClass().getName());
}
}
public class Account {
public static void main(String[] args) {
SavingAccount save = new SavingAccount();
save.deposit(4000);
BankAccount<SavingAccount> accountTypeObj = new BankAccount<>(save);
accountTypeObj.showAccountType();
CurrentAccount curr = new CurrentAccount();
curr.deposit(5000);
}
} |
Beta Was this translation helpful? Give feedback.
-
interface AccountType{
<U extends Number> void deposit (U amount);
<U extends Number> void withdraw (U amount);
void showBalance();
}
class SavingAccount implements AccountType{
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount Deposited :" + amount );
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount withdraw :" + amount );
}
@Override
public void showBalance() {
System.out.println("Available Savings Balance");
}
}
class CurrentAccount implements AccountType{
@Override
public <U extends Number> void deposit(U amount) {
System.out.println("Amount deposit :" + amount );
}
@Override
public <U extends Number> void withdraw(U amount) {
System.out.println("Amount withdraw :" + amount );
}
@Override
public void showBalance() {
System.out.println("Available Current Balance ");
}
}
class BankAccount<T extends AccountType>{
private T type;
public BankAccount(T type) {
this.type = type;
}
public void showAccountType(){
System.out.println(type.getClass().getName());
}
}
public class Account {
public static void main(String[] args) {
SavingAccount save = new SavingAccount();
save.deposit(4000);
BankAccount<SavingAccount> accountTypeObj = new BankAccount<>(save);
accountTypeObj.showAccountType();
CurrentAccount curr = new CurrentAccount();
curr.deposit(5000);
}
} |
Beta Was this translation helpful? Give feedback.
-
interface AccountType { } } } class Main { } |
Beta Was this translation helpful? Give feedback.
-
|
PRASETHA N package com.practice.problems.java;
public class BankAccountApp {
public static void main(String[] args) {
BankAccount<SavingsAccount> savings = new BankAccount<>(new SavingsAccount());
savings.performDeposit(10000.0);
savings.performWithdraw(2500.0);
savings.showAccountType();
System.out.println();
BankAccount<CurrentAccount> current = new BankAccount<>(new CurrentAccount());
current.performDeposit(5000.0);
current.performWithdraw(1500.0);
current.showAccountType();
}
}
interface AccountType {
void deposit(Double amount);
void withdraw(Double amount);
void showBalance();
}
class SavingsAccount implements AccountType {
private double balance;
private final double interestRate = 0.04;
@Override
public void deposit(Double amount) {
if (amount > 0) {
balance += amount;
}
}
@Override
public void withdraw(Double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance or invalid amount.");
}
}
@Override
public void showBalance() {
double interest = balance * interestRate;
System.out.printf("Savings Balance: %.2f (Annual Interest: %.2f)%n", balance, interest);
}
}
class CurrentAccount implements AccountType {
private double balance;
@Override
public void deposit(Double amount) {
if (amount > 0) {
balance += amount;
}
}
@Override
public void withdraw(Double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance or invalid amount.");
}
}
@Override
public void showBalance() {
System.out.printf("Current Balance: %.2f%n", balance);
}
}
class BankAccount<T extends AccountType> {
private T account;
public BankAccount(T account) {
this.account = account;
}
public void performDeposit(Double amount) {
account.deposit(amount);
}
public void performWithdraw(Double amount) {
account.withdraw(amount);
}
public void showAccountType() {
if (account instanceof SavingsAccount) {
System.out.println("Account Type: Savings Account");
} else if (account instanceof CurrentAccount) {
System.out.println("Account Type: Current Account");
}
account.showBalance();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Bhavana public interface AccountType { class SavingsAccount implements AccountType { } } class BankAccount { } } |
Beta Was this translation helpful? Give feedback.




Uh oh!
There was an error while loading. Please reload this page.
-
The objective is to implement a generic
BankAccountclass so that any customer can create any type of bank account.Define an interface AccountType:
Implement the interface in two classes -
SavingsAccountandCurrentAccount. TheSavingsAccountclass will show the balance along with annual interest @ 4%.Define a generic class
BankAccountwhere the generic type is bounded toAccountType. TheBankAccountclass will have a generic data memberTthat will take the form of eitherSavingsAccountorCurrentAccount. The class will have a method calledshowAccountType().Beta Was this translation helpful? Give feedback.
All reactions