How to create simple ATM machine using java programming

Many are the times that you go to ATMs and probably you start wondering, how the hell do they come up with this? ATM machines are created in normal programming languages that we use every day. Today we will focus much of our eyes on how to create a simple ATM machine in java. Our ATM machine includes 3 options only, but you can extend this to fit your specifications.

What is an ATM machine in programming view

Atm machine is a menu driven application which runs on windows. Some of these machines may be classified under class of systems called embedded systems. We might not have enough resources to embed ours, but trust me, the same concept you find out there, is the same concept is going to be achieved by this tutorial

Also Read: How to create a simple ATM Machine in C Programming

Coding for ATM machine

Now it’s time to share my code, feel free to modify and distribute it in your own ways. Let’s first see the code and then try to explain it.




import java.util.Scanner;

public class AtmMachine{

	private static Scanner in; 
	private static float balance = 0; // initial balance to 0 for everyone
	private static int anotherTransaction;

	public static void main(String args[]){
		in = new Scanner(System.in);

		// call our transaction method here 
		transaction();
	}

	private static void transaction(){
		// here is where most of the work is

		int choice; 

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

		choice = in.nextInt();

		switch(choice){
			case 1:
				float amount; 
				System.out.println("Please enter amount to withdraw: "); 
				amount = in.nextFloat();
				if(amount > balance || amount == 0){
					System.out.println("You have insufficient funds\n\n"); 
					anotherTransaction(); // ask if they want another transaction
				} else {
					// they have some cash
					// update balance 
					balance = balance - amount; 
					System.out.println("You have withdrawn "+amount+" and your new balance is "+balance+"\n");
					anotherTransaction(); 
				}
			break; 

			case 2:
				// option number 2 is depositing 
				float deposit; 
				System.out.println("Please enter amount you would wish to deposit: "); 
				deposit = in.nextFloat();
				// update balance 
				balance = deposit + balance;
				System.out.println("You have deposited "+deposit+" new balance is "+balance+"\n");
				anotherTransaction();
			break; 

			case 3:
				// this option is to check balance 
				System.out.println("Your balance is "+balance+"\n");
				anotherTransaction(); 
			break;

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

	}

	private static void anotherTransaction(){
		System.out.println("Do you want another transaction?\n\nPress 1 for another transaction\n2 To exit");
		anotherTransaction = in.nextInt();
		if(anotherTransaction == 1){
			transaction(); // call transaction method
		} else if(anotherTransaction == 2){
			System.out.println("Thanks for choosing us. Good Bye!");
		} else {
			System.out.println("Invalid choice\n\n");
			anotherTransaction();
		}
	}
}



Above code is self-explanatory, but if you need any kind of clarification, please feel free to ask in the commenting system below. I have all the time to attend to all your comments.