0% found this document useful (0 votes)
14 views3 pages

Atm Functions

Uploaded by

Christian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Atm Functions

Uploaded by

Christian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Create a Java Application Project in Netbeans.

ProjectName: ATM
2. Inside the ATM project, create a class for logic called ATMFunctions.java. This will contain
reusable methods like login, deposit, withdraw, changePIN, etc.

public class ATMFunctions {


private double balance;
private String pin;

public ATMFunctions(double initialBalance, String initialPin){


this.balance = initialBalance;
this.pin = initialPin;
}

public boolean login(String enteredPin){


return this.pin.equals(enteredPin);
}

public double getBalance(){


return balance;
}

public boolean deposit(double amount) {


if (amount > 0) {
balance += amount;
return true;
}
return false;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}

public boolean changePin(String oldPin, String newPin) {


if (this.pin.equals(oldPin)) {
this.pin = newPin;
return true;
}
return false;
}
}

3. Create JFrames for LoginScreen, CheckBalanceScreen, DepositScreen, WithdrawScreen and


ChangePINScreen.
4. Implement the ATMFunctions on each JFrame.
For example, in your DepositScreen JFrame, this is how we implement functions by
creating object and calling methods:

public class DepositScreen extends javax.swing.JFrame {


private ATMFunctions atm; // reference to shared functions

public DepositWindow(ATMFunctions atm) {


this.atm = atm;
initComponents();
}

private void depositButtonActionPerformed(java.awt.event.ActionEvent evt) {


try {
double amount = Double.parseDouble(amountTextField.getText());
if (atm.deposit(amount)) {
JOptionPane.showMessageDialog(this, "Deposit successful! New
balance: " + atm.getBalance());
} else {
JOptionPane.showMessageDialog(this, "Invalid deposit amount.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number.");
}
}
}

5. Share the same ATMFunctions instance across all windows

Goodluck!

You might also like