Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.4 kB
3
Indexable
Never
import 'dart:io';

class ATM {
String pin = "1234"; // Default PIN
double balance = 1000.0;

void runATM() {
if (!enterPin()) {
print("Too many failed attempts. Exiting...");
return;
        }

while (true) {
print("\nATM Menu:");
print("1. Balance Inquiry");
print("2. Withdraw Cash");
print("3. Deposit Money");
print("4. Transfer Money");
print("5. Change PIN");
print("6. Pay Bills");
print("7. Exit");

stdout.write("Select an option: ");
String? choice = stdin.readLineSync();

switch (choice) {
case '1':
balanceInquiry();
break;
case '2':
withdrawCash();
break;
case '3':
depositMoney();
break;
case '4':
transferMoney();
break;
case '5':
changePin();
break;
case '6':
payBills();
break;
case '7':
print("Exiting...");
return;
default:
print("Invalid option. Please try again.");
            }
        }
    }

bool enterPin() {
int attempts = 0;
while (attempts < 3) {
stdout.write("Enter your PIN: ");
String? enteredPin = stdin.readLineSync();
if (enteredPin == pin) {
print("PIN accepted.");
return true;
            } else {
attempts++;
print("Incorrect PIN. Attempts remaining: ${3 - attempts}");
            }
        }
return false;
    }

void balanceInquiry() {
print("Your current balance is: \$${balance.toStringAsFixed(2)}");
    }

void withdrawCash() {
stdout.write("Enter amount to withdraw: ");
double? amount = double.tryParse(stdin.readLineSync() ?? "");
if (amount != null && amount > 0 && amount <= balance) {
balance -= amount;
print("Withdrawal successful. New balance: \$${balance.toStringAsFixed(2)}");
        } else {
print("Invalid amount or insufficient funds.");
        }
    }

void depositMoney() {
stdout.write("Enter amount to deposit: ");
double? amount = double.tryParse(stdin.readLineSync() ?? "");
if (amount != null && amount > 0) {
balance += amount;
print("Deposit successful. New balance: \$${balance.toStringAsFixed(2)}");
        } else {
print("Invalid amount.");
        }
    }

void transferMoney() {
stdout.write("Enter recipient account number: ");
String? accountNumber = stdin.readLineSync();

stdout.write("Enter amount to transfer: ");
double? amount = double.tryParse(stdin.readLineSync() ?? "");

if (accountNumber != null && accountNumber.isNotEmpty && amount != null && amount > 0 && amount <= balance) {
balance -= amount;
print("Transfer successful. New balance: \$${balance.toStringAsFixed(2)}");
        } else {
print("Invalid account number or amount.");
        }
    }

void changePin() {
stdout.write("Enter your current PIN: ");
String? currentPin = stdin.readLineSync();

if (currentPin == pin) {
stdout.write("Enter new PIN: ");
String? newPin = stdin.readLineSync();

if (newPin != null && newPin.length == 4) {
pin = newPin;
print("PIN successfully changed.");
            } else {
print("Invalid PIN format. PIN must be 4 digits.");
            }
        } else {
print("Incorrect current PIN.");
        }
    }

void payBills() {
stdout.write("Enter biller name: ");
String? biller = stdin.readLineSync();

stdout.write("Enter amount to pay: ");
double? amount = double.tryParse(stdin.readLineSync() ?? "");

if (biller != null && biller.isNotEmpty && amount != null && amount > 0 && amount <= balance) {
balance -= amount;
print("Payment successful to $biller. New balance: \$${balance.toStringAsFixed(2)}");
        } else {
print("Invalid biller or amount.");
        }
    }
}

void main() {
ATM atm = ATM();
atm.runATM();
}
Leave a Comment