Thursday, February 22, 2018

ATM Java Program

If you goto any ATM machine , which will display 3 major options on screen

-->Withdraw Money
-->Balance Check
-->Deposit Cash

After doing any of the operation ,it will ask us to continue another transaction.

Here I am providing a program to implemeny these 3 operation in a small Java program.
package com.wipro;

import java.util.Scanner;

public class MyATM {

 private static Scanner input;
 private static float Balance = 1000;
 private static int doNextTransaction;

 public static void main(String args[]) {
  input = new Scanner(System.in);
  // Transaction Initiation
  transaction();
 }

 private static void transaction() {
  // Major ATM Operations

  int Option;

  System.out.println("Please select an option");
  System.out.println("1. Withdraw");
  System.out.println("2. Deposit");
  System.out.println("3. Balance");

  Option = input.nextInt();

  switch (Option) {
  case 1:
   float Amount;
   System.out.println("Please enter amount to withdraw: ");
   Amount = input.nextFloat();
   if (Amount > Balance || Amount == 0) {
    System.out.println("You have Insufficient funds\n\n");
    doNextTransaction(); // Continue Transaction
   } else {
    // Balance Amount
    Balance = Balance - Amount;
    System.out.println("You have withdrawn " + Amount
      + " And your new balance is " + Balance + "\n");
    doNextTransaction();
   }
   break;

  case 2:
   // Depositing
   float Deposit;
   System.out.println("Please enter amount you would wish to deposit: ");
   Deposit = input.nextFloat();
   // Available Balance
   Balance = Deposit + Balance;
   System.out.println("You have deposited " + Deposit
     + " And New Balance is " + Balance + "\n");
   doNextTransaction();
   break;

  case 3:
   // Balance Check
   System.out.println("Your balance is " + Balance + "\n");
   doNextTransaction();
   break;

  default:
   System.out.println("Invalid option:\n\n");
   doNextTransaction();
   break;
  }

 }

 private static void doNextTransaction() {
  System.out.println("To continue another transaction \nPress 1 \n  To exit press 2");
  doNextTransaction = input.nextInt();
  if (doNextTransaction == 1) {
   transaction(); 
  } else if (doNextTransaction == 2) {
   System.out.println("Thanks for the transaction with us. Have a great day");
  } else {
   System.out.println("Invalid option\n\n");
   doNextTransaction();
  }
 }
}



Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: