You are on page 1of 11

atm

import java.util.Scanner;

public class ATMSystem {


public void start() {
displayWelcomeScreen();
Customer customer = authenticateCustomer();
if (customer != null) {
displayMainMenu();
int option = getSelectedOption();
while (option != 4) {
performSelectedOption(option, customer);
displayMainMenu();
option = getSelectedOption();
}
displayGoodbyeMessage();
} else {
displayErrorMessage();
}
}

private void displayWelcomeScreen() {


System.out.println("Welcome to the ATM");
System.out.println("Please enter your account number and PIN");
}

private Customer authenticateCustomer() {


int accountNumber = getAccountNumberFromUser();
int pin = getPINFromUser();
// Check account number and PIN validity from the database
if (isValidAccountNumberAndPIN(accountNumber, pin)) {
return new Customer(accountNumber);
} else {
return null;
}
}

private void displayMainMenu() {


System.out.println("Main Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Deposit Money");
System.out.println("4. Exit");
System.out.println("Please select an option: ");
}

private int getSelectedOption() {


// Read the selected option from the user
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

private void performSelectedOption(int option, Customer customer) {


switch (option) {
case 1:
displayBalance(customer);
break;
case 2:
withdrawMoney(customer);
break;
case 3:
depositMoney(customer);
break;
default:
System.out.println("Invalid option. Please try again.");
}
}

private void displayBalance(Customer customer) {


// Retrieve and display the customer's account balance
double balance = getAccountBalance(customer.getAccountNumber());
System.out.println("Your account balance is: " + balance);
}

private void withdrawMoney(Customer customer) {


double amount = getWithdrawalAmountFromUser();
// Process the withdrawal and update the account balance
boolean success = processWithdrawal(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Withdrawal successful. Please take your money.");
} else {
System.out.println("Insufficient funds. Withdrawal failed.");
}
}

private void depositMoney(Customer customer) {


double amount = getDepositAmountFromUser();
// Process the deposit and update the account balance
boolean success = processDeposit(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Deposit successful. Thank you for banking with
us.");
} else {
System.out.println("Deposit failed. Please try again later.");
}
}

private void displayGoodbyeMessage() {


System.out.println("Thank you for using our ATM. Goodbye!");
}

private void displayErrorMessage() {


System.out.println("Invalid account number or PIN. Please try again.");
}

// Other methods for getting user input and interacting with the database

private int getAccountNumberFromUser() {


// Read the account number from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
return scanner.nextInt();
}

private int getPINFromUser() {


// Read the PIN from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your PIN: ");
return scanner.nextInt();
}

private boolean isValidAccountNumberAndPIN(int accountNumber, int pin) {


// Check the account number and PIN against the database
// Return true if valid, false otherwise
// You can implement the database check here or call an external service
// For simplicity, let's assume account number 12345 and PIN 1234 are valid
return accountNumber == 12345 && pin == 1234;
}

private double getAccountBalance(int accountNumber) {


// Retrieve the account balance from the database
// You can implement the database retrieval here or call an external
service
// For simplicity, let's assume a fixed account balance of 1000.00 for all
accounts
return 1000.00;
}

private boolean processWithdrawal(int accountNumber, double amount) {


// Process the withdrawal transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the withdrawal is successful if there are
sufficient funds
double balance = getAccountBalance(accountNumber);
if (balance >= amount) {
// Update the account balance in the database
// Deduct the withdrawal amount from the current balance
// You should handle the database update based on your specific
implementation
return true;
} else {
return false;
}
}

private boolean processDeposit(int accountNumber, double amount) {


// Process the deposit transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the deposit is always successful
// Update the account balance in the database
// Add the deposit amount to the current balance
// You should handle the database update based on your specific
implementation
return true;
}

public static void main(String[] args) {


ATMSystem atmSystem = new ATMSystem();
atmSystem.start();
}
}

public class Customer {


private int accountNumber;
public Customer(int accountNumber) {
this.accountNumber = accountNumber;
}

public int getAccountNumber() {


return accountNumber;
}
}

Certainly! Here's an example implementation in Java of an ATM system based on the


Interaction Overview diagram, along with explanations for each part of the code:

java

import java.util.Scanner;

public class ATMSystem {


public void start() {
displayWelcomeScreen();
Customer customer = authenticateCustomer();
if (customer != null) {
displayMainMenu();
int option = getSelectedOption();
while (option != 4) {
performSelectedOption(option, customer);
displayMainMenu();
option = getSelectedOption();
}
displayGoodbyeMessage();
} else {
displayErrorMessage();
}
}

private void displayWelcomeScreen() {


System.out.println("Welcome to the ATM");
System.out.println("Please enter your account number and PIN");
}

private Customer authenticateCustomer() {


int accountNumber = getAccountNumberFromUser();
int pin = getPINFromUser();
// Check account number and PIN validity from the database
if (isValidAccountNumberAndPIN(accountNumber, pin)) {
return new Customer(accountNumber);
} else {
return null;
}
}

private void displayMainMenu() {


System.out.println("Main Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Deposit Money");
System.out.println("4. Exit");
System.out.println("Please select an option: ");
}
private int getSelectedOption() {
// Read the selected option from the user
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

private void performSelectedOption(int option, Customer customer) {


switch (option) {
case 1:
displayBalance(customer);
break;
case 2:
withdrawMoney(customer);
break;
case 3:
depositMoney(customer);
break;
default:
System.out.println("Invalid option. Please try again.");
}
}

private void displayBalance(Customer customer) {


// Retrieve and display the customer's account balance
double balance = getAccountBalance(customer.getAccountNumber());
System.out.println("Your account balance is: " + balance);
}

private void withdrawMoney(Customer customer) {


double amount = getWithdrawalAmountFromUser();
// Process the withdrawal and update the account balance
boolean success = processWithdrawal(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Withdrawal successful. Please take your money.");
} else {
System.out.println("Insufficient funds. Withdrawal failed.");
}
}

private void depositMoney(Customer customer) {


double amount = getDepositAmountFromUser();
// Process the deposit and update the account balance
boolean success = processDeposit(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Deposit successful. Thank you for banking with
us.");
} else {
System.out.println("Deposit failed. Please try again later.");
}
}

private void displayGoodbyeMessage() {


System.out.println("Thank you for using our ATM. Goodbye!");
}

private void displayErrorMessage() {


System.out.println("Invalid account number or PIN. Please try again.");
}
// Other methods for getting user input and interacting with the database

private int getAccountNumberFromUser() {


// Read the account number from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
return scanner.nextInt();
}

private int getPINFromUser() {


// Read the PIN from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your PIN: ");
return scanner.nextInt();
}

private boolean isValidAccountNumberAndPIN(int accountNumber, int pin) {


// Check the account number and PIN against the database
// Return true if valid, false otherwise
// You can implement the database check here or call an external service
// For simplicity, let's assume account number 12345 and PIN 1234 are valid
return accountNumber == 12345 && pin == 1234;
}

private double getAccountBalance(int accountNumber) {


// Retrieve the account balance from the database
// You can implement the database retrieval here or call an external
service
// For simplicity, let's assume a fixed account balance of 1000.00 for all
accounts
return 1000.00;
}

private boolean processWithdrawal(int accountNumber, double amount) {


// Process the withdrawal transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the withdrawal is successful if there are
sufficient funds
double balance = getAccountBalance(accountNumber);
if (balance >= amount) {
// Update the account balance in the database
// Deduct the withdrawal amount from the current balance
// You should handle the database update based on your specific
implementation
return true;
} else {
return false;
}
}

private boolean processDeposit(int accountNumber, double amount) {


// Process the deposit transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the deposit is always successful
// Update the account balance in the database
// Add the deposit amount to the current balance
// You should handle the database update based on your specific
implementation
return true;
}

public static void main(String[] args) {


ATMSystem atmSystem = new ATMSystem();
atmSystem.start();
}
}

public class Customer {


private int accountNumber;

public Customer(int accountNumber) {


this.accountNumber = accountNumber;
}

public int getAccountNumber() {


return accountNumber;
}
}

Explanation:

The ATMSystem class represents the ATM system and contains methods for various
operations.
The start() method is the entry point of the ATM system. It displays the
welcome screen, authenticates the customer, and presents the main menu options.
The displayWelcomeScreen() method displays a welcome message to the user.
The authenticateCustomer() method prompts the user for an account number and
PIN, checks the validity of the credentials, and returns a Customer object if the
authentication is successful.
The displayMainMenu() method displays the main menu options.
The getSelectedOption() method reads the selected option from the user.
The performSelectedOption() method executes the selected option by calling the
corresponding method.
The displayBalance() method retrieves and displays the customer's account
balance.
The withdrawMoney() method prompts the user for the withdrawal amount,
processes the withdrawal, and updates the account balance.
The depositMoney() method prompts the user for the deposit amount, processes
the deposit, and updates the account balance.
The displayGoodbyeMessage() method displays a farewell message to the user.
The displayErrorMessage() method displays an error message when the
authentication fails.
The getAccountNumberFromUser() method reads the account number from the user.
The getPINFromUser() method reads the PIN from the user.
The isValidAccountNumberAndPIN() method checks the account number and PIN
against the database (in this case, hardcoded values are used for simplicity).
The getAccountBalance() method retrieves the account balance from the database
(hardcoded value for simplicity).
The processWithdrawal() method processes the withdrawal transaction, updating
the account balance (simplified implementation based on balance comparison).
The processDeposit() method processes the deposit transaction, updating the
account balance (simplified implementation).
The main() method creates an instance of ATMSystem and starts the ATM system.
////with comments
import java.util.Scanner;

public class ATMSystem {


public void start() {
displayWelcomeScreen(); // Display the welcome screen
Customer customer = authenticateCustomer(); // Authenticate the customer
if (customer != null) {
displayMainMenu(); // Display the main menu
int option = getSelectedOption(); // Get the selected option from the
user
while (option != 4) {
performSelectedOption(option, customer); // Perform the selected
option
displayMainMenu(); // Display the main menu again
option = getSelectedOption(); // Get the next selected option
}
displayGoodbyeMessage(); // Display the goodbye message
} else {
displayErrorMessage(); // Display an error message if authentication
fails
}
}

private void displayWelcomeScreen() {


System.out.println("Welcome to the ATM");
System.out.println("Please enter your account number and PIN");
}

private Customer authenticateCustomer() {


int accountNumber = getAccountNumberFromUser(); // Get the account number
from the user
int pin = getPINFromUser(); // Get the PIN from the user
// Check account number and PIN validity from the database
if (isValidAccountNumberAndPIN(accountNumber, pin)) {
return new Customer(accountNumber); // Return a new Customer object if
authentication succeeds
} else {
return null; // Return null if authentication fails
}
}

private void displayMainMenu() {


System.out.println("Main Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Deposit Money");
System.out.println("4. Exit");
System.out.println("Please select an option: ");
}

private int getSelectedOption() {


// Read the selected option from the user
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
private void performSelectedOption(int option, Customer customer) {
switch (option) {
case 1:
displayBalance(customer); // Display the account balance
break;
case 2:
withdrawMoney(customer); // Withdraw money from the account
break;
case 3:
depositMoney(customer); // Deposit money into the account
break;
default:
System.out.println("Invalid option. Please try again.");
}
}

private void displayBalance(Customer customer) {


// Retrieve and display the customer's account balance
double balance = getAccountBalance(customer.getAccountNumber());
System.out.println("Your account balance is: " + balance);
}

private void withdrawMoney(Customer customer) {


double amount = getWithdrawalAmountFromUser(); // Get the withdrawal amount
from the user
// Process the withdrawal and update the account balance
boolean success = processWithdrawal(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Withdrawal successful. Please take your money.");
} else {
System.out.println("Insufficient funds. Withdrawal failed.");
}
}

private void depositMoney(Customer customer) {


double amount = getDepositAmountFromUser(); // Get the deposit amount from
the user
// Process the deposit and update the account balance
boolean success = processDeposit(customer.getAccountNumber(), amount);
if (success) {
System.out.println("Deposit successful. Thank you for banking with
us.");
} else {
System.out.println("Deposit failed. Please try again later.");
}
}

private void displayGoodbyeMessage() {


System.out.println("Thank you for using our ATM. Goodbye!");
}

private void displayErrorMessage() {


System.out.println("Invalid account number or PIN. Please try again.");
}

// Other methods for getting user input and interacting with the database

private int getAccountNumberFromUser() {


// Read the account number from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
return scanner.nextInt();
}

private int getPINFromUser() {


// Read the PIN from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your PIN: ");
return scanner.nextInt();
}

private boolean isValidAccountNumberAndPIN(int accountNumber, int pin) {


// Check the account number and PIN against the database
// Return true if valid, false otherwise
// You can implement the database check here or call an external service
// For simplicity, let's assume account number 12345 and PIN 1234 are valid
return accountNumber == 12345 && pin == 1234;
}

private double getAccountBalance(int accountNumber) {


// Retrieve the account balance from the database
// You can implement the database retrieval here or call an external
service
// For simplicity, let's assume a fixed account balance of 1000.00 for all
accounts
return 1000.00;
}

private boolean processWithdrawal(int accountNumber, double amount) {


// Process the withdrawal transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the withdrawal is successful if there are
sufficient funds
double balance = getAccountBalance(accountNumber);
if (balance >= amount) {
// Update the account balance in the database
// Deduct the withdrawal amount from the current balance
// You should handle the database update based on your specific
implementation
return true;
} else {
return false;
}
}

private boolean processDeposit(int accountNumber, double amount) {


// Process the deposit transaction in the database
// Return true if successful, false otherwise
// You can implement the database update here or call an external service
// For simplicity, let's assume the deposit is always successful
// Update the account balance in the database
// Add the deposit amount to the current balance
// You should handle the database update based on your specific
implementation
return true;
}
public static void main(String[] args) {
ATMSystem atmSystem = new ATMSystem();
atmSystem.start(); // Start the ATM system
}
}

public class Customer {


private int accountNumber;

public Customer(int accountNumber) {


this.accountNumber = accountNumber;
}

public int getAccountNumber() {


return accountNumber;
}
}

You might also like