Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- // Custom exception for insufficient funds
- class InsufficientFundsException extends Exception {
- public InsufficientFundsException(String message) {
- super(message);
- }
- }
- // Class to represent a bank account
- class BankAccount1 {
- private String accountHolder;
- private double balance;
- // Constructor to initialize account holder's name and initial balance
- public BankAccount1(String accountHolder) {
- this.accountHolder = accountHolder;
- this.balance = 0.0; // Initial balance set to zero
- }
- // Method to deposit money
- public void deposit(double amount) {
- if (amount <= 0) {
- System.out.println("Deposit amount must be positive.");
- return;
- }
- balance += amount;
- System.out.println("Deposited: $" + amount);
- }
- // Method to withdraw money
- public void withdraw(double amount) throws InsufficientFundsException {
- if (amount <= 0) {
- System.out.println("Withdrawal amount must be positive.");
- return;
- 33
- }
- if (amount > balance) {
- throw new InsufficientFundsException("Insufficient funds for withdrawal.");
- }
- balance -= amount;
- System.out.println("Withdrawn: $" + amount);
- }
- // Method to check balance
- public double checkBalance() {
- return balance;
- }
- // Method to get account holder's name
- public String getAccountHolder() {
- return accountHolder;
- }
- }
- // Main class to manage the bank account transactions
- public class BAM {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Get account holder's name and create the account
- System.out.print("Enter account holder's name: ");
- String name = scanner.nextLine();
- BankAccount1 account = new BankAccount1(name);
- int choice = -1;
- do {
- // Display menu
- System.out.println("\n--- Bank Account Management ---");
- System.out.println("1. Check Balance");
- System.out.println("2. Deposit Money");
- System.out.println("3. Withdraw Money");
- System.out.println("4. Exit");
- System.out.print("Enter your choice: ");
- 34
- try {
- choice = scanner.nextInt(); // Get user's menu choice
- switch (choice) {
- case 1:
- System.out.println("Current Balance: $" + account.checkBalance());
- break;
- case 2:
- System.out.print("Enter amount to deposit: $");
- double depositAmount = scanner.nextDouble();
- account.deposit(depositAmount);
- break;
- case 3:
- System.out.print("Enter amount to withdraw: $");
- double withdrawAmount = scanner.nextDouble();
- account.withdraw(withdrawAmount);
- break;
- case 4:
- System.out.println("Exiting...");
- break;
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- } catch (InsufficientFundsException e) {
- // Handle insufficient funds exception
- System.out.println("Error: " + e.getMessage());
- } catch (InputMismatchException e) {
- // Handle invalid input (non-integer or non-double values)
- System.out.println("Invalid input. Please enter a number.");
- scanner.next(); // Clear the invalid input
- }
- } while (choice != 4);
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement