TASK 1 CODE
TASK 1 CODE
MACHINE
COMMENTS:
“After authentication, users can check their account balance. This feature
should display the current balance clearly and promptly, providing users
with up-to-date information on their finances.”
FUNDS WITHDRAWAL:
“Include error handling for cases where users attempt to withdraw more
than their balance or enter invalid amounts.”
FUNDS DEPOSIT:
“Users can deposit funds into their account. The system should accept
deposit amounts and update the account balance accordingly.”
“Ensure that deposit transactions are processed accurately and that users
are notified of successful deposits.”
PIN CHANGE PROCESS:
“The PIN change process involves verifying the current PIN, entering a
new PIN, and confirming the new PIN. This ensures that the user is
authorized to make the change and that the new PIN is correctly set.”
“// When the user requests their transaction history, retrieve the most
recent transactions from the stored data. Limit the number of transactions
displayed to a manageable number, such as the last 10 transactions.”
CODE:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ATM {
private static class Account {
private int pin;
private double balance;
private List<String> transactionHistory;
public Account(int pin, double initialBalance) {
this.pin = pin;
this.balance = initialBalance;
this.transactionHistory = new ArrayList<>();
this.transactionHistory.add("Initial deposit: $" + initialBalance);
}
public boolean validatePin(int pin) {
return this.pin == pin;
}
public void changePin(int newPin) {
this.pin = newPin;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
this.balance += amount;
this.transactionHistory.add("Deposited: $" + amount);
}
public boolean withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds.");
return false;
}
this.balance -= amount;
this.transactionHistory.add("Withdrew: $" + amount);
return true;
}
public void printTransactionHistory() {
System.out.println("Transaction History:");
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
}
}
private static Account account;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
account = new Account(1234, 1000.00); // Initial PIN is 1234 and initial balance is
$1000
while (true) {
System.out.println("\nATM Menu:");
System.out.println("1. Balance Inquiry");
System.out.println("2. Cash Withdrawal");
System.out.println("3. Cash Deposit");
System.out.println("4. Change PIN");
System.out.println("5. Transaction History");
System.out.println("6. Exit");
System.out.print("Please select an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
balanceInquiry();
break;
case 2:
cashWithdrawal();
break;
case 3:
cashDeposit();
break;
case 4:
changePin();
break;
case 5:
transactionHistory();
break;
case 6:
System.out.println("Thank you for using our ATM.");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void balanceInquiry() {
System.out.println("Your balance is: $" + account.getBalance());
}
private static void cashWithdrawal() {
System.out.print("Enter the amount to withdraw: $");
double amount = scanner.nextDouble();
if (account.withdraw(amount)) {
System.out.println("Withdrawal successful.");
}
}
private static void cashDeposit() {
System.out.print("Enter the amount to deposit: $");
double amount = scanner.nextDouble();
account.deposit(amount);
System.out.println("Deposit successful.");
}
private static void changePin() {
System.out.print("Enter your current PIN: ");
int currentPin = scanner.nextInt();
if (account.validatePin(currentPin)) {
System.out.print("Enter your new PIN: ");
int newPin = scanner.nextInt();
account.changePin(newPin);
System.out.println("PIN changed successfully.");
} else {
System.out.println("Invalid PIN.");
}
}
private static void transactionHistory() {
account.printTransactionHistory();
}
}
SAMPLE OUTPUT:
ATM Menu:
1. Balance Inquiry
2. Cash Withdrawal
3. Cash Deposit
4. Change PIN
5. Transaction History
6. Exit