ATM machine project
Below is a simple project Java code for an ATM machine simulation. This code allows users to perform basic ATM functions such as checking balance, withdrawing money, and depositing money.
This code defines a simple ATM class with methods for checking balance, withdrawing money, depositing money, and a menu for user interaction. The main
method creates an instance of the ATM class and starts the ATM application, allowing users to interact with the menu until they choose to exit.
Here’s an example of how the output might look when you run the ATM program:
import java.util.Scanner;
public class ATM {
private double balance;
private Scanner scanner;
public ATM(double initialBalance) {
balance = initialBalance;
scanner = new Scanner(System.in);
}
public void checkBalance() {
System.out.println("Your balance is: $" + balance);
}
public void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds");
} else {
balance -= amount;
System.out.println("Withdrawal successful. Current balance is: $" + balance);
}
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposit successful. Current balance is: $" + balance);
}
public void menu() {
System.out.println("Welcome to the ATM!");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance();
break;
case 2:
System.out.print("Enter amount to withdraw: $");
double withdrawAmount = scanner.nextDouble();
withdraw(withdrawAmount);
break;
case 3:
System.out.print("Enter amount to deposit: $");
double depositAmount = scanner.nextDouble();
deposit(depositAmount);
break;
case 4:
System.out.println("Thank you for using the ATM. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice");
}
}
public static void main(String[] args) {
ATM atm = new ATM(1000); // Starting balance is $1000
while (true) {
atm.menu();
}
}
}
Suppose you choose option 1 to check balance:
Welcome to the ATM!
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Then, you choose option 2 to withdraw money and enter the amount to withdraw, for example, $200:
Your balance is: $1000.0
Welcome to the ATM!
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter amount to withdraw: $200
Withdrawal successful. Current balance is: $800.0
Welcome to the ATM!
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Next, you choose option 3 to deposit money and enter the amount to deposit, for example, $500:
Enter amount to deposit: $500
Deposit successful. Current balance is: $1300.0
Welcome to the ATM!
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
You can continue interacting with the menu until you choose option 4 to exit:
4. Exit
Thank you for using the ATM. Goodbye!
And the program terminates.