-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from VaibhavS0710/Contribution
ATM machine
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} | ||
|
||
} |