Create an interface named iBanking with the following function
deposit(int amount) : returns int
withdraw(int amount) : returns int
-----------
Implement the above interface using Customer Class
Attributes of Customer class
customerName : String
transactionType : String
baseAmount : int
accessor methods , Parameterized Constructor for initialization
Override the methods, deposit should add the amount with baseAmount and withdraw should subtract
--------------------
main method Class CustomerProcess
Use iBanking interface to access the deposit and withdraw for one customer with below attributes
customerName : Yourname
transactionType : read using scanner (withdraw or deposit)
baseAmount : read using scanner
based on transactionType you can call the methods
SOLUTION CODE FOR ABOVE PROGRAM
package com.company;
import java.util.*;
//This is our interface
interface iBanking
{
//Interface methods
public int deposit(int amount);
public int withdraw(int amount);
}
//Class customer for implementing iBanking interface
class Customer implements iBanking
{
private String customerName;
private String transactionType;
private int baseAmount;
Customer(String customerName, String transactionType, int baseAmount)
{
this.customerName=customerName;
this.transactionType = transactionType;
this.baseAmount = baseAmount;
}
public int deposit(int deposit_amount)
{
return (baseAmount+deposit_amount);
}
public int withdraw(int withdraw_amount)
{
return (baseAmount-withdraw_amount);
}
public static void main(String arg[])
{
//Prompt the user to Enter name, transaction type and baseamount
Scanner sc = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
String customer_name = sc.next();
System.out.print("Enter the base amount: ");
int base_amount = sc.nextInt();
System.out.println("Enter transaction type: ");
System.out.println("1.Deposit");
System.out.println("2.Withdraw");
System.out.print("Enter your option: ");
int option = sc.nextInt();
//if option is 1 perform deposit operations
if(option == 1)
{
iBanking my_transaction_object = new Customer(customer_name, "Deposit",base_amount);
//prompt the user to enter the amount to deposit
System.out.print("Enter the amount to deposit: ");
int amount_to_deposit = sc.nextInt();
int total_amount = my_transaction_object.deposit(amount_to_deposit);
System.out.println("\nDear "+customer_name+" you have successfully deposited "+amount_to_deposit
+"\nin your account, your balance is now "+total_amount);
}
//if option is 2, perform withdrawal operation
else if(option == 2)
{
iBanking my_transaction_object = new Customer(customer_name, "Withdraw",base_amount);
//prompt the user to enter the amount to withdraw
System.out.print("Enter the amount to withdraw: ");
int amount_to_withdraw = sc.nextInt();
if(amount_to_withdraw>base_amount)
{
System.out.println("\nYou do not have enough money to withdraw "+amount_to_withdraw
+"\nyour balance is "+base_amount);
}
else
{
int total_amount = my_transaction_object.withdraw(amount_to_withdraw);
System.out.println("\nDear "+customer_name+" you have successfully Withdrawn "+amount_to_withdraw
+"\nfrom your account, your balance is now "+total_amount);
}
}
//else the option entered is invalid
else
{
System.out.println("\nInvalid transaction type, please try again");
}
}
}
SAMPLE PROGRAM OUTPUTS
Comments
Leave a comment