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

Program 6

The document discusses an exception handling program in Java. It defines a Demonetization Exception class and an Account class with deposit, withdraw, and balance methods. A Customer class is used to test the Account class methods based on user input.

Uploaded by

PRIYANSHU KUMARI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Program 6

The document discusses an exception handling program in Java. It defines a Demonetization Exception class and an Account class with deposit, withdraw, and balance methods. A Customer class is used to test the Account class methods based on user input.

Uploaded by

PRIYANSHU KUMARI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception handling

6. Design and implement a Java program for the following requirements:


a) An Exception class called Demonetization Exception which returns the statement that
says “Deposit of Old currency of (Rs_____) crosses Rs. 5,000 and cannot be Deposited”.
b) A class called ‘Account’ that creates account with 500 Rs minimum balance with
following methods.
i. deposit(amount, currencyType) method to deposit amount. This class should
handle “Demonetization Exception” and print the message defined in this Exception
class. If a currency type is “OLD” and the amount is greater than 5,000 then throw
the Demonetization Exception, otherwise update the balance.
ii. currBalance() method that displays balance amount in the account.
iii. withdraw(amount) method to withdraw amount and update the balance. Use proper
control structure to check Balance should not go less than 500.
c) A ‘Customer’ class that creates Account object and call the methods deposit(),
withdraw() and currBalance() based on the user choice.

import java.util.Scanner;

class demonetizationException extends Exception


{
float amount;
demonetizationException(float amount)
{
this.amount=amount;
}

public String toString()


{
return "Deposit of old currency of Rs "+amount+" cross Rs5000 and
cannot be deposited";
}
}

class account
{
float balance;
account()
{
balance = 500;
}

void deposit(float amount,String currencytype)


{
String currency = currencytype.toUpperCase();
try {
if (currency.equals("OLD") && amount > 5000)
throw new demonetizationException(amount);
else
balance = balance + amount;
System.out.println("Updated Balance is "+balance);
}
catch (demonetizationException e)
{
System.out.println(e);
}
finally {
System.out.println("Your money has been deposited
successfully!!");
}
}

void withdraw(float amount)


{
if (amount>balance)
{
System.out.println("Error: low balance");
}
balance = balance - amount;
System.out.println("Updated Balance is "+balance);
}

void getBalance()
{
System.out.println("Balance: "+balance);
}
}

public class Program6{


public static void main(String[] args)
{
int choice;
float amount;
String currencytype;
Scanner sc = new Scanner(System.in);
account obj = new account();

for(;;)
{
System.out.println("1:Deposit");
System.out.println("2:Withdraw");
System.out.println("3:Balance");
System.out.println("Enter your choice");
choice = sc.nextInt();

switch (choice) {
case 1 -> {
System.out.println("Enter the amount to deposit");
amount = sc.nextFloat();
System.out.println("Currency Type");
currencytype = sc.next();
obj.deposit(amount, currencytype);
}
case 2 -> {
System.out.println("Enter the amount to withdraw");
amount = sc.nextFloat();
obj.withdraw(amount);
}
case 3 -> obj.getBalance();
}
}
}
}
Output

You might also like