-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatm.php
More file actions
71 lines (63 loc) · 2.64 KB
/
atm.php
File metadata and controls
71 lines (63 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
// -----------------------------------------------------------------------------------------------------------------------------
// Step 1 - Intial setup
$balance = 2000;
// while (true): This creates an infinite loop, meaning the menu will keep displaying until the user chooses to exit the program.
while(true) {
// Display ATM menu to user
echo "\n----- WELCOME TO ATM CIMB -----\n";
echo "\n";
echo "What are you looking for?\n";
echo "\n";
echo "1. Check balance.\n";
echo "2. Check Deposit Money.\n";
echo "3. Withdraw Money.\n";
echo "4. Exit";
echo "\n";
echo "\n-------------------------------\n";
echo "Choose an option: ";
$choice = trim(fgets(STDIN));
// fgets(STDIN): Reads user input from the terminal (CLI).
// The trim() function is used to remove any extra spaces or newline characters from the input.
// -----------------------------------------------------------------------------------------------------------------------------
// Step 2: ATM Functionalities
switch ($choice) {
case 1:
echo "\nYour current balance is: RM" . $balance . "\n";
break;
case 2:
echo "\nEnter the amount to deposit: RM";
$deposit = trim(fgets(STDIN));
if (is_numeric($deposit) && $deposit > 0) {
$balance += $deposit;
echo "Successfully deposited RM" . $deposit . ". Your new balance is RM" . $balance . "\n";
} else {
echo "Invalid amount. Please enter a positive number.\n";
}
break;
case 3:
echo "\nEnter the amount to withdraw: RM";
$withdraw = trim(fgets(STDIN));
if (is_numeric($withdraw) && $withdraw > 0) {
if ($withdraw <= $balance) {
$balance -= $withdraw;
echo "Successfully withdrew RM" . $withdraw . ". Your new balance is RM" . $balance . "\n";
} else {
echo "Insufficient balance! You have only RM" . $balance . "\n";
}
} else {
echo "Invalid amount. Please enter a positive number.\n";
}
break;
case 4:
echo "Thank you for using our ATM! Goodbye.\n";
exit();
default:
echo "Invalid choice. Please choose a valid option (1-4).\n";
}
// -----------------------------------------------------------------------------------------------------------------------------
// Step 3: Loop and Exit
// The while (true) loop ensures the ATM menu keeps displaying after each transaction until the user selects 4 to exit.
// When case 4 is selected, the loop breaks with the exit() function, ending the program.
}
?>