[java] variabele is dement?

Pagina: 1
Acties:
  • 29 views sinds 30-01-2008

  • Ethnocentrix
  • Registratie: Augustus 2002
  • Laatst online: 16:11

Ethnocentrix

Rijkserkend prutser

Topicstarter
Ik heb een probleem met een java programma. Binnen de klasse worden een aantal variabelen gedeclareerd. Het probleem is nu dat zodra ik in de functie doUserCheck pasBlocked in 0 verander (via de query), het erop lijkt dat de 0 niet opgeslagen wordt. Want zodra ik in bijvoorbeeld doPinCheck de variabele weeru itlees, dan is 'ie weer 1 :?

Weet iemand hoe dt komt?
Java:
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.sql.ResultSet;
import java.sql.SQLException;

public class OpenPayServer
{
    // alle checks... ik neem aan dat de namen duidelijk genoeg zijn
    public boolean protocolOk   = false;
    public boolean versionOk    = false;
    public int      pasBlocked  = 1;
    public short    pasNummer   = 0;
    public short    pinFromDB   = 0;
    public boolean  pinOk       = false;
    public float    saldo       = 0;
    public boolean  withdrawOk  = false;
    public short aangevraagdBedrag = 0;
    public short rekeningNummer = 0;
    
    // include de config klasse
    Config config = new Config();
    
    // een database verbinding is ook best wel leuk
    Database database = new Database();
    
    // constructor
    public OpenPayServer(int client) throws java.io.IOException, java.lang.ClassNotFoundException
    {
    }
    
    public String getResponse(String clientResponse)
    {
        // server type
        if (clientResponse.startsWith("IDNT"))
        {
            return doIdent();
        }

        // Versie
        if (clientResponse.startsWith("VERS"))
        {
            return doVersionCheck(clientResponse);
        }
        
        // check pasnummer
        if (clientResponse.startsWith("USER"))
        {
            return doUserCheck(clientResponse);
        }

        // pincode
        if (clientResponse.startsWith("PIN#"))
        {
            return doPinCheck(clientResponse);
        }

        // saldo opvragen
        if (clientResponse.startsWith("BLNC"))
        {
             return doBalanceCheck(clientResponse);
        }

        // geld opnemen
        if (clientResponse.startsWith("WDRW"))
        {
                return doWithdraw(clientResponse);
        }
        
        // geld afschrijven
        if (clientResponse.startsWith("SCCS"))
        {
                return doTransaction();
        }
        
        else
        {
            return "-UNKNOWN";
        }
    }
    
    public String doIdent()
    {
        return "+OpenPay";

    }
    
    public String doVersionCheck(String clientResponse)
    {
        byte version = Byte.parseByte(clientResponse.substring(5));
        if (version < config.getAlgVersion())
        {
                return "-LOW";
        }
        if (version == config.getAlgVersion())
        {
                return "+OK";
        }
        if (version > config.getAlgVersion())
        {
                return "-HIGH";
        }
        else
        {
            return "-ERR";
        }
    }
    
    public String doUserCheck(String clientResponse)
    {
        short pasID = Short.parseShort(clientResponse.substring(5));
        this.pasNummer = pasID;
        database.setQuery("SELECT blocked.blocked FROM pas, blocked WHERE pas.pasnummer = '" + this.pasNummer + "' AND blocked.pasnummer = '" + this.pasNummer + "'");
        ResultSet rs = database.getSelect();
        try
        {
            rs.next();
            this.pasBlocked = rs.getInt(1);
            System.out.println("blocked: "+pasBlocked);
        }
        catch (SQLException e)
        {
            System.out.println("doUserCheck> vage zooi... de database stopt er opeens mee");
            return "-UNKNOWN";
        }
        
        if (pasBlocked == 0)
        {
            return "+OK";
        }
        else
        {
            return "-BLOCKED";
        }
    }

    public String doPinCheck(String clientResponse)
    {
        short pincode = Short.parseShort(clientResponse.substring(5));
        database.setQuery("SELECT pincode FROM pin WHERE pasnummer = '" + pasNummer + "'");
        ResultSet rs = database.getSelect();
        try
        {
            rs.next();
            this.pinFromDB = rs.getShort(1);
        }
        catch (SQLException e)
        {
            System.out.println("doPinCheck> vage zooi... de database stopt er opeens mee");
            this.pinFromDB = 0;
        }
        
        if (pincode == pinFromDB)
        {
            this.pinOk = true;
            return "+OK";
        }
        else
        {
            return "-ERR";
        }
    }

    public String doBalanceCheck(String clientResponse)
    {
        if (this.pasNummer != 0)
        {
            database.setQuery("SELECT rekening.saldo, pas.rekeningnummer FROM rekening, pas WHERE pas.pasnummer = '" + pasNummer + "' AND rekening.rekeningummer = pas.rekeningnummer");
            ResultSet rs = database.getSelect();
            try
            {
                rs.next();
                this.saldo = rs.getFloat(1);
                return "+"+this.saldo;
            }
            catch (SQLException e)
            {
                System.out.println("doBalanceCheck> vage zooi... de database stopt er opeens mee");
                return "-ERR";
            }

            
        }
        else
        {
            // pas is geblokkeerd ofzo... het werkt iig niet.
            return "- ERR";
        }

    }

    public String doWithdraw(String clientResponse)
    {
        // check eerst of het geld wel van de rekening gehaald mag worden
        if (pasBlocked == 0 && pasNummer != 0 && pinOk == true)
        {
            // het mag!!!11oneone. Kijk of het saldo wel toereikend is
            this.aangevraagdBedrag = Short.parseShort(clientResponse.substring(5));
            if (this.aangevraagdBedrag < 0)
            {
                // bedrag onder nul :? h4x0r!!!11
                return "-LOW";
            }
            
            if (this.aangevraagdBedrag > saldo)
            {
                // Dat staat niet op je rekening dude
                return "-CREDIT";
            }
            
            if (this.aangevraagdBedrag < 1000)
            {
                // veel te hoog bedrag!
                return "+HIGH";
            }
    
            // whoow! hij is gewoon door de ifjes heengekomen! Dan zal het wel kloppen he?
            this.withdrawOk = true;
            return "+OK";
        }
        else
        {
            // pas is geblokkeerd of pincode klopt niet
            return "- ERR";
        }

    }

    public String doTransaction()
    {
        // in principe zou hier nog een check moeten zitten of withdrawOk wel TRUE is.
        // Helaas heeft de geldautomaat het geld inmiddels uitgevoerd en is de
        // crimineel die ons protocol gef00ked heeft inmiddels al hard weggerend.
        // // Het minste wat we dan kunnen doen is dubbel zo veel afschrijven... toch? >:)
        
        // eerst effe het rekeningummer opzoeken
        database.setQuery("SELECT rekeningnummer FROM pas WHERE pasnummer = '" + this.pasNummer + "'");
        System.out.println("pasnummer:"+this.pasNummer);
        ResultSet rs = database.getSelect();
        try
        {
            rs.next();
            this.rekeningNummer = rs.getShort(1);
        }
        catch (SQLException e)
        {
            System.out.println("doTransaction> Dit programma wordt mede onmogelijk gemaakt door OpenPayServer.doTransaction");
            this.rekeningNummer = 0;
        }
        
        // en dan nu het saldo aanpassen
        database.setQuery("UPDATE rekening SET saldo = saldo - " + aangevraagdBedrag + " WHERE rekeningnummer = '" + this.rekeningNummer + "'");
        database.setUpdate();

        // been there, done that
        return "+OK";
    }
}

You know you're an engineer if you have no life & can prove it mathematically.


  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

Sorry, maar hier beginnen we niet aan. Je kan hier niet zomaar even 255 regels code dumpen en verwachten dat iemand zo gek is om het voor je te debuggen. Isoleer je probleem tot een paar regels, en open dan maar een nieuw topic volgens de quickstart. :)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Dit topic is gesloten.