Creating ATM in Java: A Step-by-Step Guide | Java Project ATM Machine | Java Mini Project
In This Article We Discuss How to Create ATM Machine in Java Step by Step With These Methods. This Project is Divided into Two Parts
Part 1
1. Balance
2. Cash Withdraw
3. Cash Deposit
4. Exit
Program Source Code
import java.util.Scanner;
public class ATM {
private int balance;
public ATM(int initialBalance) {
this.balance = initialBalance;
}
public static void waitForEnter() {
Scanner scanner = new Scanner(System.in);
scanner.nextLine(); // This will wait for the user to press enter
}
public int getBalance() {
return this.balance;
}
public boolean withdraw(int amount) {
if (amount > this.balance) {
return false;
}
this.balance -= amount;
return true;
}
public void deposit(int amount) {
this.balance += amount;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create an ATM object with an initial balance of $1000
ATM atm = new ATM(1000);
while (true) {
System.out.println("1. View Balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Your balance is: $" + atm.getBalance());
waitForEnter();
break;
case 2:
System.out.print("Enter the amount to withdraw: ");
int withdrawAmount = scanner.nextInt();
boolean success = atm.withdraw(withdrawAmount);
if (success) {
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance.");
}
waitForEnter();
break;
case 3:
System.out.print("Enter the amount to deposit: ");
int depositAmount = scanner.nextInt();
atm.deposit(depositAmount);
System.out.println("Deposit successful.");
waitForEnter();
break;
case 4:
System.out.println("Thank you for using the ATM!");
System.exit(0);
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
Related Video
Part 2
1. Balance
2. Cash Withdraw
3. Cash Deposit
4. Cash Transfer
5. Exit
Program Source Code
import java.util.Scanner;
public class ATM {
private int balance;
private int pincode;
public ATM(int initialBalance, int pincode) {
this.balance = initialBalance;
this.pincode = pincode;
}
public int getBalance() {
return this.balance;
}
public boolean withdraw(int amount, int pin) {
if(pin != this.pincode) {
return false;
}
if(amount > this.balance) {
return false;
}
this.balance -= amount;
return true;
}
public void deposit(int amount) {
this.balance += amount;
}
public boolean transfer(int amount, ATM recevier, int pin){
if(pin != this.pincode) {
return false;
}
boolean success = this.withdraw(amount,pin);
if(success) {
recevier.deposit(amount);
return true;
}
else {
return false;
}
}
public static void waitForEnter() {
Scanner ins = new Scanner(System.in);
ins.nextLine();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner ins = new Scanner(System.in);
ATM atm = new ATM(1000,1234);
while(true) {
System.out.println("1. View Balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Transfer");
System.out.println("5. Exit");
System.out.print("Enter Your Choice :");
int choice = ins.nextInt();
switch(choice) {
case 1:
System.out.println("Your Balance is :"+atm.getBalance());
waitForEnter();
break;
case 2:
System.out.println("Enter the Amoutn to Withdraw :");
int wdamount = ins.nextInt();
boolean success = atm.withdraw(wdamount,1234);
if(success) {
System.out.println("Withdraw Successfull");
}else {
System.out.println("Insufficient Balance...");
}
waitForEnter();
break;
case 3:
System.out.println("Enter the amout to deposite :");
int dpamount = ins.nextInt();
atm.deposit(dpamount);
System.out.println("Deposit Successfull..");
waitForEnter();
break;
case 4:
System.out.print("Enter The amout to Transfer:");
int tsamount = ins.nextInt();
System.out.print("Enter the receiver Pin Code:");
int rpin = ins.nextInt();
ATM receiver = new ATM(0,rpin);
boolean transfersucceess = atm.transfer(tsamount, receiver, rpin);
if(transfersucceess) {
System.out.println("Transfer Successfull...");
}
else {
System.out.println("Invalid pin or insuffecient ballance");
}
waitForEnter();
break;
case 5:
System.out.println("Thanks of using ATM..");
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
}
}
}
}
Post a Comment