-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithDrawBalance.java
More file actions
58 lines (53 loc) · 2.34 KB
/
Copy pathwithDrawBalance.java
File metadata and controls
58 lines (53 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* This Java program simulates a simple banking transaction where a user can withdraw an amount from their account balance.
* It demonstrates the use of exception handling for managing insufficient balance scenarios.
*
* Class: withDrawBalance
* - The main class contains the logic for the banking transaction.
*
* Functionality:
* - Prompts the user to enter a withdrawable amount.
* - Checks if the entered amount is greater than the available balance.
* - If the amount is greater, it throws an ArithmeticException indicating "Insufficient Balance".
* - If the amount is within the available balance, it deducts the amount from the balance and confirms the transaction.
*
* Components:
* - Scanner: Used to take input from the user.
* - try-catch block: Used for handling the ArithmeticException in case of insufficient balance.
*
* Note:
* - This is a simplified example for educational purposes and does not include advanced banking features like account verification,
* logging, or database integration.
*
* Author: [Your Name]
* Date: [Current Date]
*/
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
// Initialize Scanner for user input
Scanner input = new Scanner(System.in);
// Set initial available balance
int availBalance = 3500;
// Prompt user for the withdrawable amount
System.out.print("Enter withdrawable amount -> ");
int withDrawBalance = input.nextInt();
try {
// Check if available balance is less than the requested withdrawal amount
if (availBalance < withDrawBalance) {
// Throw exception if balance is insufficient
throw new ArithmeticException("Insufficient Balance");
} else {
// Deduct the withdrawal amount from available balance
availBalance -= withDrawBalance;
// Confirm transaction success
System.out.println("Transaction Successful!");
System.out.println("Remaining Balance : " + availBalance);
System.out.println("Thank you for choosing Bank!");
}
} catch (ArithmeticException e) {
// Handle the exception and print the error message
System.out.println(e.getMessage());
}
}
}