Professional Documents
Culture Documents
Session 1
Lets create the Form
🠶 Create three label for Barclays ATM, 1. Deposit, 2. Withdraw and Select Option in the
Design view
Now lets put code to control the Form
🠶 Lets start with the Exit Button, double click the Exit button in the Design View
🠶 This will take you to the Exit code button in the Source code
🠶 Now lets add this code
🠶 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
🠶 System.exit(0);
Clear button
🠶 jTextField1.setText(" ");
Execute Transaction button
choice = Integer.parseInt(jTextField1.getText());
switch(choice){
case 1: String ammt = JOptionPane.showInputDialog(this, "Enter amount to deposit");
int am = Integer.parseInt(ammt);
balance = balance + am;
JOptionPane.showMessageDialog(this, "Your new balance is "+balance);
break;
Execute Transaction button
case 2: String amount = JOptionPane.showInputDialog(this, "Enter amount to withdraw");
int m = Integer.parseInt(amount);
if(m > balance){
JOptionPane.showMessageDialog(this, " Insufficient Balance");
}
else {
balance = balance - m;
JOptionPane.showMessageDialog(this, "Your new balance is "+balance);
}
break;
default: JOptionPane.showMessageDialog(this, " Invalid Choice please try again");
}}
catch (Exception ex){
System.out.println(" Exception caught " + ex);
}
TASK 2
Create a Form that will add two numbers and show their sum
Solution
else {
JOptionPane.showMessageDialog(rootPane, "please put a valid number");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "Invalid input please put a number ");
}
Task 3
Solution
if ((text1.trim().isEmpty())||(text2.trim().isEmpty())){
JOptionPane.showMessageDialog(this, " Please input a number in the text box");
}else{}
try{
int number1 = Integer.parseInt(jTextField1.getText());
int number2 = Integer.parseInt(jTextField2.getText());
if ( number1 > number2) {
JOptionPane.showMessageDialog(this, number1 +" is greater than " + number2);
}
Solution
else if (number1 == number2){
JOptionPane.showMessageDialog(this, number1 +" is equal to " + number2);
}
else {
JOptionPane.showMessageDialog(this, number1 +" is less than " + number2);
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(this, "Numbers Only Allowed ");
}
}
Homework
Re do the activities once again and include other shapes like calculating area of a Triangle and
Square