This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2018/11/4

what is wrong with my if and else statement

divinity divinity

2018/11/4

#
hi all I have been practicing on building an atm machine using gui interface. i have added a login form where the user have to punched in their pin and from there it will open to a dashboard where the user will choose what transaction they would like to perform. The thing I have encountered some errors, when I mean error not the kid of error where u are seeing the red squiggly line underneath, the errors where u cant see it but know there is an error, logical errors, I am talking about. here how the if and else statement supposed to work or at least i would like it to work. I have set a cash limit to the withdraw account(what I mean is, there is a certain amount the user can withdraw, here is where the if and else statement come in, if the user withdraw more than the required cash limit, the if statement should say you have exceeded your amount and here is where the else statement come in, if the user withdraw below the cash limit, it will say you can collect your cash but it is not working that way, the compiler hit only the if statement whenever the user withdraw more than the cash limit and within the required limit. is there something that I am missing or not seeing, need some help here. here is the codes that I have used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package atm;
 
import javax.swing.JOptionPane;
 
/**
 *
 
 */
public class Widthdrawal extends javax.swing.JFrame {
 
    /**
     * Creates new form Widthdrawal
     */
    public Widthdrawal() {
        initComponents();
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {
 
        jPanel1 = new javax.swing.JPanel();
        jlab_widthdrawal = new javax.swing.JLabel();
        jlab_cashlimit = new javax.swing.JLabel();
        jlab_cash = new javax.swing.JLabel();
        txt_cash = new javax.swing.JTextField();
        btn_width = new javax.swing.JButton();
        btn_dashboard = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jlab_widthdrawal.setFont(new java.awt.Font("Cambria", 3, 11)); // NOI18N
        jlab_widthdrawal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlab_widthdrawal.setText("Widthdrawal");
 
        jlab_cashlimit.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jlab_cashlimit.setText("Cash Limit is: $3000.00");
 
        jlab_cash.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jlab_cash.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlab_cash.setText("Please enter the amount");
 
        txt_cash.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
 
        btn_width.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        btn_width.setText("Widthdraw");
        btn_width.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_widthActionPerformed(evt);
            }
        });
 
        btn_dashboard.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        btn_dashboard.setText("Dashboard");
        btn_dashboard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_dashboardActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jlab_cash, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jlab_cashlimit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jlab_widthdrawal, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_cash)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btn_width, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btn_dashboard, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(131, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jlab_widthdrawal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlab_cashlimit)
                .addGap(47, 47, 47)
                .addComponent(jlab_cash)
                .addGap(18, 18, 18)
                .addComponent(txt_cash, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_width)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_dashboard)
                .addContainerGap(40, Short.MAX_VALUE))
        );
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
 
        pack();
    }// </editor-fold>                       
 
    private void btn_dashboardActionPerformed(java.awt.event.ActionEvent evt) {                                             
        ATM_Dashboard dash = new ATM_Dashboard();
        dash.setVisible(true);
        this.dispose();
    }                                            
 
    private void btn_widthActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
         
        double balance=0;
         double cash_limit=0;
        
       if(balance >= cash_limit)
       {
            balance = balance - cash_limit;
             
            JOptionPane.showMessageDialog(null, "You have exceeded your amount");
       }
       else if(balance > cash_limit)
       {
           JOptionPane.showMessageDialog(null, "Please collect your money");
       }
       
         
    }                                        
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Widthdrawal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Widthdrawal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Widthdrawal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Widthdrawal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Widthdrawal().setVisible(true);
            }
        });
    }
danpost danpost

2018/11/4

#
divinity wrote...
if the user withdraw more than the required cash limit
I do not see anywhere where the user decides on a specific amount to withdraw. You do not want the response to "I would like to make a withdraw." to be "Sorry, you have exceeded your amount." without knowing what amount is requested.
divinity divinity

2018/11/4

#
hi danpost where do you think i should add it. the question i mean. I put it above the if statement in a dialog box and when i run it to see ah getting the question but it not allowing me to enter the amount because the exceeded amount dialog box came up right after i click the okay button
danpost danpost

2018/11/4

#
divinity wrote...
where do you think i should add it. the question i mean. I put it above the if statement in a dialog box and when i run it to see ah getting the question but it not allowing me to enter the amount because the exceeded amount dialog box came up right after i click the okay button
You will need to show what you added and where (with surrounding codes).
danpost danpost

2018/11/4

#
With a fresh look at the code, I see you have a text field called txt_cash. If that is for the requested withdraw amount, then it would need to be used somewhere in your btn_widthActionPerformed method.
You need to login to post a reply.