CSS 321: Advanced DBMS and Implementation
Java GUI-Based Development
Dr. Nyamtiga, BW
Step 1: Set Up the Project
Create a new Java project in Visual Studio Code or any text editor of your choice.
Step 2: Design the GUI
1. Import Swing Components
Use [Link] for GUI elements.
2. Create the Main Frame
Use JFrame to serve as the main application window.
3. Add Components
Include components like JLabel, JButton, JTextField, and JPasswordField.
Example of GUI Components:
A JLabel for displaying account balance.
JTextField for inputting the amount.
JButton for actions like "Deposit," "Withdraw," and "Exit."
Step 3: Create Classes and Methods
1. Create a ATMProgram Class:
o Extend JFrame.
o Define fields for account balance and GUI components.
2. Define Functional Methods:
o deposit(double amount
Adds funds to the account balance.
o withdraw(double amount)
Subtracts funds from the account if sufficient balance exists.
o updateBalance()
Updates the balance display on the GUI.
3. Handle Events:
o Use ActionListener to respond to button clicks.
Step 4: Writing the Code
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class ATMProgram extends JFrame {
private double balance = 0.0;
private JLabel balanceLabel;
private JTextField amountField;
public ATMProgram() {
// Set up the frame
setTitle("ATM System");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 1));
// Balance display
balanceLabel = new JLabel("Balance: $0.00");
add(balanceLabel);
// Input field
amountField = new JTextField();
add(amountField);
// Buttons
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton exitButton = new JButton("Exit");
add(depositButton);
add(withdrawButton);
add(exitButton);
// Add button functionality
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double amount = [Link]([Link]());
deposit(amount);
}
});
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double amount = [Link]([Link]());
withdraw(amount);
}
});
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
[Link](0);
}
});
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
updateBalance();
} else {
[Link](this, "Enter a valid amount.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
updateBalance();
} else {
[Link](this, "Insufficient balance or
invalid amount.");
}
}
public void updateBalance() {
[Link]("Balance: $" + [Link]("%.2f", balance));
}
public static void main(String[] args) {
[Link](() -> {
ATMProgram atm = new ATMProgram();
[Link](true);
});
}
}
Step 5: Compile and Run the Program
1. Save the file as [Link].
2. Compile it using javac [Link].
3. Run the program using java ATMProgram.
Step 6: Test the Program
1. Start the application and check the balance.
2. Enter amounts to deposit or withdraw.
3. Verify that the balance updates correctly.
Step 7: Adding Enhancements
1. Add Login Functionality: Use a JPasswordField to simulate PIN-based authentication.
2. Implement Database Integration: Store account details in a database for persistence.