Skip to content

Commit

Permalink
Merge pull request #259 from VaibhavS0710/Contribution
Browse files Browse the repository at this point in the history
ATM machine
  • Loading branch information
BlackIQ authored Oct 21, 2024
2 parents a79e2d6 + 604bbaf commit 7805ec9
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Java/examples/ATM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Scanner;

public class ATM {

private double balance;

public ATM(double initialBalance){
this.balance = initialBalance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
if(amount > 0){
balance += amount;
System.out.println("Deposit successful. Current balance: " + balance);
}
else{
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(double amount){
if(amount > 0 && amount <= balance){
balance -= amount;
System.out.println("Withdrawal successful. Current balance: " + balance);
}
else{
System.out.println("Invalid withdrawal amount or insufficient funds.");
}
}

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

while (true){
System.out.println("Choose an option:");
System.out.println("1. Check balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.println("Enter your choice: ");

int choice = sc.nextInt();

switch (choice){
case 1:
System.out.println("Your balance is: " + atm.getBalance());
break;
case 2:
System.out.println("Enter deposit amount: ");
double depositAmount = sc.nextDouble();
atm.deposit(depositAmount);
break;
case 3:
System.out.println("Enter withdrawal amount: ");
double withdrawalAmount = sc.nextDouble();
atm.withdraw(withdrawalAmount);
break;
case 4:
System.out.println("Thank you for using our ATM. Goodbye!");
sc.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}

}

0 comments on commit 7805ec9

Please sign in to comment.