└── task2.java /task2.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 | System.out.println("Insufficient funds. Withdrawal failed."); 21 | return false; 22 | } else { 23 | balance -= amount; 24 | return true; 25 | } 26 | } 27 | } 28 | 29 | class ATM { 30 | private BankAccount userAccount; 31 | 32 | public ATM(BankAccount account) { 33 | this.userAccount = account; 34 | } 35 | 36 | public void withdraw(double amount) { 37 | if (amount > 0) { 38 | if (userAccount.withdraw(amount)) { 39 | System.out.println("Withdrawal successful. Remaining balance: $" + userAccount.getBalance()); 40 | } 41 | } else { 42 | System.out.println("Invalid withdrawal amount."); 43 | } 44 | } 45 | 46 | public void deposit(double amount) { 47 | if (amount > 0) { 48 | userAccount.deposit(amount); 49 | System.out.println("Deposit successful. New balance: $" + userAccount.getBalance()); 50 | } else { 51 | System.out.println("Invalid deposit amount."); 52 | } 53 | } 54 | 55 | public void checkBalance() { 56 | System.out.println("Current balance: $" + userAccount.getBalance()); 57 | } 58 | } 59 | 60 | public class ATMProgram { 61 | public static void main(String[] args) { 62 | Scanner scanner = new Scanner(System.in); 63 | 64 | System.out.print("Enter initial balance: $"); 65 | double initialBalance = scanner.nextDouble(); 66 | 67 | BankAccount userAccount = new BankAccount(initialBalance); 68 | ATM atm = new ATM(userAccount); 69 | 70 | int choice; 71 | do { 72 | System.out.println("\nATM Menu:"); 73 | System.out.println("1. Withdraw"); 74 | System.out.println("2. Deposit"); 75 | System.out.println("3. Check Balance"); 76 | System.out.println("0. Exit"); 77 | System.out.print("Enter your choice: "); 78 | choice = scanner.nextInt(); 79 | 80 | switch (choice) { 81 | case 1: 82 | System.out.print("Enter withdrawal amount: $"); 83 | double withdrawalAmount = scanner.nextDouble(); 84 | atm.withdraw(withdrawalAmount); 85 | break; 86 | case 2: 87 | System.out.print("Enter deposit amount: $"); 88 | double depositAmount = scanner.nextDouble(); 89 | atm.deposit(depositAmount); 90 | break; 91 | case 3: 92 | atm.checkBalance(); 93 | break; 94 | case 0: 95 | System.out.println("Exiting ATM. Thank you!"); 96 | break; 97 | default: 98 | System.out.println("Invalid choice. Please enter a valid option."); 99 | } 100 | 101 | } while (choice != 0); 102 | 103 | scanner.close(); 104 | } 105 | } 106 | --------------------------------------------------------------------------------