atm-machine-project

Use the Account class created to simulate an ATM machine. Create 10 accounts with id AC001…..AC010 with initial balance 300₹. The system prompts the users to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, display menu with multiple choices.

1. Balance inquiry
2. Withdraw money [Maintain minimum balance 300₹]
3. Deposit money
4. Money Transfer
5. Create Account
6. Deactivate Account
In this program array list and iterator class have been used.
The Arraylist  class is a re sizable array, which can be found in the java.util package.

‘Iterator’ is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection.(In this program collection is the array list)
java.util package has public interface Iterator and contains three methods:

  1. boolean hasNext(): It returns true if Iterator has more element to iterate.
  2. Object next(): It returns the next element in the collection until the hasNext()method return true.
  3. void remove(): It removes the current element in the collection.
import java.lang.*;
import java.util.*;

class Account
{	
	String id;
	int balance;

	Account(String i)
	{
		id = i;
		balance = 300;
	}

	void BalanceInq()
	{
		System.out.println("ID :- " + id);
		System.out.println("Balance :- " + balance);
	}

	void withdraw()
	{
		int temp;
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter amount to withdraw :- ");
		temp = sc.nextInt();

		if(balance - temp < 300)
			System.out.println("Minimum 300 balance is needed");
		else
		{
			balance = balance - temp;
			System.out.println("Current balance :- " + balance);
		}
	}

	void deposit()
	{
		int temp;
		Scanner sc = new Scanner(System.in);
	
		System.out.print("Enter amount to deposite :- ");
		temp = sc.nextInt();

		balance = balance + temp;

		System.out.println("Current balance :- " + balance);
	}

	void moneyTransfer(ArrayList ar)
	{
		int temp;
		String id;
		Scanner sc = new Scanner(System.in);
		Account ac = null;

		System.out.print("Enter ID to transfer :- ");
		id = sc.next();

		Iterator itr = ar.iterator();
		while(itr.hasNext())
		{
			Account a = (Account) itr.next();

			if(id.equals(a.id))
				ac = a;
				
		}
		
		if(ac == null)
			System.out.println("Account not found");
		else
		{ 
			System.out.print("Enter amount to transfer :- ");
			temp = sc.nextInt();

			if(temp > balance)
			System.out.println("Not enough balane to transfer");
			else
			{
				ac.balance = ac.balance + temp;
				balance = balance - temp;
			}
		}
	}

	void Create(ArrayList arr)
	{
		String str;
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter ID :- ");
		str = sc.next();

		Account ac = new Account(str);
		arr.add(ac);
	}
	
	void delete(ArrayList arr)
	{
		String str;
		Scanner sc = new Scanner(System.in);
		Account ac = null;

		System.out.print("Enter ID :- ");
		str = sc.next();

		Iterator itr = arr.iterator();
		while(itr.hasNext())
		{
			Account a = (Account) itr.next();
			if(str.equals(a.id))
				ac = a;			
		}
	
		if(ac == null)
			System.out.println("Account not found");
		else
		{
			arr.remove(ac);
			System.out.println("Account removed successfully");
		}
	}
}

public class java13
{
	public static void main(String[] args)
	{
		ArrayList<Account>arr = new ArrayList<Account>();
		Scanner sc = new Scanner(System.in);
		String str;
		int op;
		Account ac = null;
		Account a = new Account(null);

		arr.add(new Account("AC001"));
		arr.add(new Account("AC002"));
		arr.add(new Account("AC003"));
		arr.add(new Account("AC004"));
		arr.add(new Account("AC005"));
		arr.add(new Account("AC006"));
		arr.add(new Account("AC007"));
		arr.add(new Account("AC008"));
		arr.add(new Account("AC009"));
		arr.add(new Account("AC010"));

		System.out.print("Enter your ID :- ");
		str = sc.next();

		Iterator itr = arr.iterator();
		while(itr.hasNext())
		{
			a = (Account) itr.next();
			if(str.equals(a.id))
				ac = a;
		}

		if(ac == null)
		{
			System.out.println("Invalid ID");
			System.exit(0);
		}
		else
		{

		do
		{
			System.out.println("Press 1 - for Balance Inquiry");
			System.out.println("Press 2 - for Withdraw");
			System.out.println("Press 3 - for Deposit");
			System.out.println("Press 4 - for Money Transfer");
			System.out.println("Press 5 - for Create Account");
			System.out.println("Press 6 - for Remove Account");
			System.out.println("Press 0 - for Exit");
		
			System.out.print("Enter your choice => ");
			op = sc.nextInt();
		
				switch(op)
				{
					case 1:
						ac.BalanceInq();
						break;
					case 2:
						ac.withdraw();
						break;
					case 3:
						ac.deposit();
						break;
					case 4:
						ac.moneyTransfer(arr);
						break;
					case 5:
						ac.Create(arr);
						break;
					case 6:
						ac.delete(arr);
						break;
					case 0:
					System.out.println("Thank you");
						break;
					default:
					System.out.println("Invalid input");
				}
			}while(op != 0);
		}
}	

LEAVE A REPLY

Please enter your comment!
Please enter your name here