java

 avatar
unknown
plain_text
5 months ago
2.7 kB
1
Indexable
package oops;

import java.util.Scanner;

public class FreedomBankingServices {
    private double balance;

    public FreedomBankingServices() {
        this.balance = 0.0;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Successfully deposited: $" + amount);
        } else {
            System.out.println("Deposit amount must be positive.");
        }
    }

    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            System.out.println("Successfully withdrew: $" + amount);
        } else {
            throw new IllegalArgumentException("Insufficient balance. Withdrawal failed.");
        }
    }

    public double checkBalance() {
        return balance;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        FreedomBankingServices bankingService = new FreedomBankingServices();
        int choice;

        do {
            System.out.println("\nFreedom Banking Services");
            System.out.println("1. Deposit");
            System.out.println("2. Withdraw");
            System.out.println("3. Check Balance");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter deposit amount: ");
                    double depositAmount = scanner.nextDouble();
                    bankingService.deposit(depositAmount);
                    break;
                case 2:
                    System.out.print("Enter withdrawal amount: ");
                    double withdrawalAmount = scanner.nextDouble();
                    try {
                        bankingService.withdraw(withdrawalAmount);
                    } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage());
                        System.out.println("Terminating program.");
                        return;
                    }
                    break;
                case 3:
                    System.out.println("Current balance: $" + bankingService.checkBalance());
                    break;
                case 4:
                    System.out.println("Exiting the program. Thank you!");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        } while (choice != 4);

        scanner.close();
    }
}
Editor is loading...
Leave a Comment