0% found this document useful (0 votes)
68 views

Assignment No 2 Java

The document describes a banking program with the following classes: - Account class to represent bank accounts with methods to deposit, withdraw, and check balance - Bank class with checking and savings account objects and methods to deposit, withdraw, transfer between accounts, and print balances - Tester class with a menu to perform transactions on the account objects and demonstrate the banking functionality

Uploaded by

Mahnoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Assignment No 2 Java

The document describes a banking program with the following classes: - Account class to represent bank accounts with methods to deposit, withdraw, and check balance - Bank class with checking and savings account objects and methods to deposit, withdraw, transfer between accounts, and print balances - Tester class with a menu to perform transactions on the account objects and demonstrate the banking functionality

Uploaded by

Mahnoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a complete program

 Implement a class Account. An account has

 a balance,  functions to add  and withdraw money,  and a function to


inquire the current balance.

 Pass a value into a constructor to set an initial balance.  If no value is passed


the initial balance should be set to Rs. 0.  Charge a Rs.5 penalty if an attempt is
made to withdraw more money than available in the account.

 Implement a class Bank. This bank has two objects

 checking  and savings

of the type Account

Implement four instance methods:

 deposit(double amount, String account)

 withdraw(double amount, String account)

 transfer(double amount, String account)


 printBalances()

Here the account string is "S" or "C". For the deposit or withdrawal, it indicates
which account is affected. For a transfer it indicates the account from which the
money is taken; the money is automatically transferred to the other account.

 Implement a class Tester Having menu  Current Account o Deposit o


Withdraw o Transfer o Show balance  Saving Account o Deposit o Withdraw o
Transfer o Show balance  Exit

Show menu again and again until user press Exit. Create object of bank class in
tester class and perform all functionalities.

Account
public class account {
protected double balance;
public account()
{
balance=0;
}
public double add(double x)
{
balance=x+balance;
return x;
}
public double withdraw(double x)
{

if(x>balance)
{
System.out.println("the amount u want to withdraw is more than
your current balance");
System.out.println("so you have plenty of RS 5");
balance=balance-5;
return -5;
}
else
{
balance=balance-x;
return balance;
}
}
double inquirebalance()
{
System.out.println("your current balance is ");
return balance;

BANK
public class bank extends account {
account checking=new account();
account savings=new account();
public void deposit(double amount,String account)
{
if (account.equals("c"))

checking.add(amount);
else
savings.add(amount);
}
public void withdraw(double amount,String account)
{
if (account.equals("c"))
checking.withdraw(amount);
else
savings.withdraw(amount);
}
public void transfer(double amount, String account)
{
if (account.equals("c"))
if (checking.withdraw(amount)>=0)
savings.add(amount);
else checking.add(5);
else
if (account.equals("s"))
if (savings.withdraw(amount)>=0)
checking.add(amount);
else savings.add(5);

}
public void print()
{
System.out.println("checking: "+checking.inquirebalance());
System.out.println("savings: "+savings.inquirebalance());
}

Tester
import java. util.Scanner;
public class tester {
public void menu()
{
System.out.println("*********menu********");
System.out.println("CURRENT ACCOUNT");
System.out.println("press 1 for DEposit");
System.out.println("press 2 for withdraw");
System.out.println("press 3 for transfer");
System.out.println("press 4 for show balance");
System.out.println("SAVING ACCOUNT");
System.out.println("press 1 for DEposit");
System.out.println("press 2 for withdraw");
System.out.println("press 3 for transfer");
System.out.println("press 4 for show balance");
}

public static void main(String[] args)


{
int n;
Scanner in =new Scanner(System.in);
bank b=new bank();
do{
tester t=new tester();
t.menu();
int choice;
choice=in.nextInt();

if (choice==1)
{System.out.println("enter ammount ");
double am;
am=in.nextInt();
System.out.println("enter account ");
String ac;
ac=in.next();
b.deposit(am,ac);
}
if (choice==2)
{
System.out.println("enter amount ");
double am;
am=in.nextInt();
System.out.println("enter account ");
String ac;
ac=in.next();
b.withdraw(am,ac);
}
if (choice==3)
{
System.out.println("enter amount ");
double am;
am=in.nextInt();
System.out.println("enter account from which u want to
transfer money ");
String ac;
ac=in.next();
b.transfer(am,ac);
}
if (choice==4)
{
b.print();
}
System.out.println("do you want to continue press 1");

n=in.nextInt();
}
while(n==1);

Output:

You might also like