-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSavingAccount.java
More file actions
253 lines (225 loc) · 8.63 KB
/
SavingAccount.java
File metadata and controls
253 lines (225 loc) · 8.63 KB
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
/******************************************************************************
* Program Author: Kavita Mishra for CSCI 6810 Java and the Internet *
* Date: September, 2018 *
*******************************************************************************/
package com.mishra;
import java.lang.*; //including Java packages used by this program
import java.sql.*;
import com.mishra.*;
public class SavingAccount
{ //Instance Variables
private String SavingAccountNumber, CustomerName, CustomerID;
private float Balance = -1, Amount = -1;
public SavingAccount(String SA_Num, String Cust_Name, String Cust_ID, String Amt) { //Constructor One with three parameters
SavingAccountNumber = SA_Num;
CustomerName = Cust_Name;
CustomerID = Cust_ID;
Amount = Float.parseFloat(Amt);
}
public SavingAccount(String SA_Num) { //Constructor Two with one parameter
SavingAccountNumber = SA_Num;
}
public SavingAccount(){
}
public boolean openAcct() {
boolean done = false;
try {
if (!done) {
DBConnection ToDB = new DBConnection(); //Have a connection to the DB
Connection DBConn = ToDB.openConn();
Statement Stmt = DBConn.createStatement();
String SQL_Command = "SELECT SavingAccountNumber FROM SavingAccount WHERE SavingAccountNumber ='"+SavingAccountNumber+"'"; //SQL query command
ResultSet Rslt = Stmt.executeQuery(SQL_Command); //Inquire if the username exsits.
done = !Rslt.next();
if (done) {
SQL_Command = "INSERT INTO SavingAccount(SavingAccountNumber, CustomerName, Balance, CustomerID)"+
" VALUES ('"+SavingAccountNumber+"','"+CustomerName+"','"+Amount+"', '"+CustomerID+"')"; //Save the username, password and Name
Stmt.executeUpdate(SQL_Command);
}
Stmt.close();
ToDB.closeConn();
}
}
catch(java.sql.SQLException e)
{ done = false;
System.out.println("SQLException: " + e);
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Vendor: " + e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
catch (java.lang.Exception e)
{ done = false;
System.out.println("Exception: " + e);
e.printStackTrace ();
}
return done;
}
public String getAccno(String C_ID) { //Method to return a SavingAccount No.
try {
DBConnection ToDB = new DBConnection(); //Have a connection to the DB
Connection DBConn = ToDB.openConn();
Statement Stmt = DBConn.createStatement();
String SQL_Command = "SELECT SavingAccountNumber FROM SavingAccount WHERE CustomerID ='"+C_ID+"'";
ResultSet Rslt = Stmt.executeQuery(SQL_Command);
while (Rslt.next()) {
SavingAccountNumber = Rslt.getString("SavingAccountNumber");
}
Stmt.close();
ToDB.closeConn();
}
catch(java.sql.SQLException e)
{
System.out.println("SQLException: " + e);
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Vendor: " + e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
return SavingAccountNumber;
}
public float getBalance() { //Method to return a SavingAccount balance
try {
DBConnection ToDB = new DBConnection(); //Have a connection to the DB
Connection DBConn = ToDB.openConn();
Statement Stmt = DBConn.createStatement();
String SQL_Command = "SELECT Balance FROM SavingAccount WHERE SavingAccountNumber ='"+SavingAccountNumber+"'"; //SQL query command for Balance
ResultSet Rslt = Stmt.executeQuery(SQL_Command);
while (Rslt.next()) {
Balance = Rslt.getFloat(1);
}
Stmt.close();
ToDB.closeConn();
}
catch(java.sql.SQLException e)
{
System.out.println("SQLException: " + e);
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Vendor: " + e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
catch (java.lang.Exception e)
{
System.out.println("Exception: " + e);
e.printStackTrace ();
}
return Balance;
}
public float getBalance(String SaveAcctNumber) { //Method to return a SavingAccount balance
try {
DBConnection ToDB = new DBConnection(); //Have a connection to the DB
Connection DBConn = ToDB.openConn();
Statement Stmt = DBConn.createStatement();
String SQL_Command = "SELECT Balance FROM SavingAccount WHERE SavingAccountNumber ='"+SaveAcctNumber+"'"; //SQL query command for Balance
ResultSet Rslt = Stmt.executeQuery(SQL_Command);
while (Rslt.next()) {
Balance = Rslt.getFloat(1);
}
Stmt.close();
ToDB.closeConn();
}
catch(java.sql.SQLException e)
{
System.out.println("SQLException: " + e);
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Vendor: " + e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
catch (java.lang.Exception e)
{
System.out.println("Exception: " + e);
e.printStackTrace ();
}
return Balance;
}
public boolean deposit(String C_ID){
boolean done= !SavingAccountNumber.equals("") && !CustomerID.equals("");
try{
if(done){
DBConnection DBconn = new DBConnection();
Connection conn = DBconn.openConn();
Statement stat = conn.createStatement();
String SQL_Command = "SELECT Balance FROM SavingAccount WHERE SavingAccountNumber = '"+SavingAccountNumber+"' AND CustomerID ='"+C_ID+"'";
System.out.println(SQL_Command);
ResultSet reslt = stat.executeQuery(SQL_Command);
while (reslt.next()) {
Balance = reslt.getFloat(1);
}
Balance = Balance + Amount;
SQL_Command = "UPDATE SavingAccount SET Balance = '" + Balance + "' WHERE SavingAccountNumber = '"+SavingAccountNumber+"'";
System.out.println(SQL_Command);
stat.executeUpdate(SQL_Command);
stat.close();
DBconn.closeConn();
}
}
catch (SQLException e){
System.out.println("SQLException" + e);
done= false;
System.out.println("SQLExceptionState" + e.getSQLState());
System.out.println("message" + e.getMessage());
System.out.println("vendor" + e.getErrorCode());
e.getNextException();
System.out.println("");
}
catch (java.lang.Exception e){
System.out.println("Exception" + e);
e.printStackTrace();
}
return done;
}
public boolean Withdraw(String C_ID){
boolean done = !SavingAccountNumber.equals("") && !CustomerID.equals("") && !(Balance == 0);
try{
if(done){
DBConnection DBconn = new DBConnection();
Connection Conn = DBconn.openConn();
Statement Stat = Conn.createStatement();
String SQL_Command = "SELECT Balance FROM SavingAccount WHERE SavingAccountNumber = '"+SavingAccountNumber+"' AND CustomerID= '"+C_ID+"' ";
System.out.println(SQL_Command);
ResultSet rslt = Stat.executeQuery(SQL_Command);
while (rslt.next())
{
Balance = rslt.getFloat(1);
}
if (Balance>=Amount) {
Balance = Balance - Amount;
SQL_Command = "UPDATE SavingAccount SET Balance = '"+Balance+"' WHERE SavingAccountNumber = '"+SavingAccountNumber+"'";
System.out.println(SQL_Command);
Stat.executeUpdate(SQL_Command);
Stat.close();
DBconn.closeConn();
}
}
}
catch (java.sql.SQLException e){
System.out.println("SQLException" + e);
while (e != null){
System.out.println("SqlExceptionState" + e.getSQLState());
System.out.println("Message"+ e.getMessage());
System.out.println("Vendor"+ e.getErrorCode());
e = e.getNextException();
System.out.println("");
}
}
catch (java.lang.Exception e){
System.out.println("Exception" + e);
e.printStackTrace();
}
return done;
}
}