ATM Control Structures
Explanation & Learnings
In order to practice Java control structures such as if-else and switch, we were tasked to create an ATM Withdrawal Limit system. This activity has a combination of looping and conditionals. At first, I was contemplating on what logic I can create using do-while, if-else, and switch on one program. The brainstorming was hard. It is, especially when I forgot the syntax and the proper logic that I intended to use while typing. I was able to create the program by breaking it into blocks like "do(switch(cases & default)) while(condition)".
I realized that ATMs can be as simple as this — a balance variable, a loop to keep session alive, and conditions. Writing the withdrawal logic was particularly insightful: I had to account for edge cases like zero or negative amounts within an if-else chain. I memorized the switch-case-break now, it wasn't that different from C after all. Real-world systems are really just layered conditions built on top of each other.
To wrap up, this program made me understand how control flow works in Java and how combining loops, conditionals, and switch statements can produce a functional program. Breaking a problem into smaller, manageable blocks makes the coding process a lot less overwhelming.
import java.util.Scanner;
public class ATMWithdrawalLimit
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double balance = 9000.00;
int choice;
do {
System.out.println("===== ATM MENU =====");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.println("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("\n===== ATM BALANCE =====");
System.out.printf("Current Balance: PHP %.2f%n", balance);
System.out.println("=======================\n");
break;
case 2:
System.out.println("\n===== ATM DEPOSIT =====");
System.out.println("Enter deposit amount:");
double depositAmt = sc.nextDouble();
balance += depositAmt;
System.out.printf("Deposit successful. Current Balance: PHP %.2f%n", balance);
System.out.println("=======================\n");
break;
case 3:
System.out.println("\n===== ATM WITHDRAWAL =====");
System.out.println("Enter withdrawal amount:");
double withdrawAmt = sc.nextDouble();
if (withdrawAmt <= 0) {
System.out.println("Error! Amount must be greater than zero.");
System.out.println("=======================\n");
} else if (withdrawAmt > balance) {
System.out.println("Error! Insufficient account balance.");
System.out.println("=======================\n");
} else if (withdrawAmt > 3000) {
System.out.println("Error! Withdrawal limit exceeded. Maximum amount is PHP3000.00.");
System.out.println("=======================\n");
} else {
balance -= withdrawAmt;
System.out.printf("Withdrawal successful. Current Balance: PHP %.2f%n", balance);
System.out.println("=======================\n");
}
break;
case 4:
System.out.println("Transaction ended. Thank you for using our ATM services.");
System.out.println("=======================\n");
break;
default:
System.out.println("Invalid choice. Please try again.");
System.out.println("=======================\n");
}
} while (choice !=4);
}
}
===== ATM MENU =====
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice:
1
===== ATM BALANCE =====
Current Balance: PHP 9000.00
=======================