└── Task 2.java /Task 2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class BankAccount { 4 | private double balance; 5 | 6 | public BankAccount(double initialBalance) { 7 | this.balance = initialBalance; 8 | } 9 | 10 | public double getBalance() { 11 | return balance; 12 | } 13 | 14 | public void deposit(double amount) { 15 | balance += amount; 16 | } 17 | 18 | public boolean withdraw(double amount) { 19 | if (amount <= balance) { 20 | balance -= amount; 21 | return true; 22 | } else { 23 | return false; 24 | } 25 | } 26 | } 27 | 28 | class ATM { 29 | private BankAccount userAccount; 30 | 31 | public ATM(BankAccount account) { 32 | this.userAccount = account; 33 | } 34 | 35 | public void displayOptions() { 36 | System.out.println("1. Withdraw"); 37 | System.out.println("2. Deposit"); 38 | System.out.println("3. Check Balance"); 39 | System.out.println("4. Exit"); 40 | } 41 | 42 | public void processOption(int option, Scanner scanner) { 43 | switch (option) { 44 | case 1: 45 | System.out.print("Enter withdrawal amount: "); 46 | double withdrawAmount = scanner.nextDouble(); 47 | if (userAccount.withdraw(withdrawAmount)) { 48 | System.out.println("Withdrawal successful. Remaining balance: " + userAccount.getBalance()); 49 | } else { 50 | System.out.println("Insufficient funds. Withdrawal failed."); 51 | } 52 | break; 53 | 54 | case 2: 55 | System.out.print("Enter deposit amount: "); 56 | double depositAmount = scanner.nextDouble(); 57 | userAccount.deposit(depositAmount); 58 | System.out.println("Deposit successful. New balance: " + userAccount.getBalance()); 59 | break; 60 | 61 | case 3: 62 | System.out.println("Current balance: " + userAccount.getBalance()); 63 | break; 64 | 65 | case 4: 66 | System.out.println("Exiting. Thank you!"); 67 | System.exit(0); 68 | break; 69 | 70 | default: 71 | System.out.println("Invalid option. Please try again."); 72 | } 73 | } 74 | } 75 | 76 | public class Main { 77 | public static void main(String[] args) { 78 | Scanner scanner = new Scanner(System.in); 79 | 80 | System.out.print("Enter initial account balance: "); 81 | double initialBalance = scanner.nextDouble(); 82 | 83 | BankAccount userAccount = new BankAccount(initialBalance); 84 | ATM atm = new ATM(userAccount); 85 | 86 | while (true) { 87 | atm.displayOptions(); 88 | System.out.print("Choose an option (1-4): "); 89 | int option = scanner.nextInt(); 90 | 91 | atm.processOption(option, scanner); 92 | } 93 | } 94 | } --------------------------------------------------------------------------------