Skip to content

Commit fa82dcf

Browse files
banking application
1 parent bd25a95 commit fa82dcf

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed
+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
package banking;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.SQLIntegrityConstraintViolationException;
10+
import java.sql.Statement;
11+
12+
public class bankManagement { // these class provides all
13+
// bank method
14+
15+
private static final int NULL = 0;
16+
17+
static Connection con = connection.getConnection();
18+
static String sql = "";
19+
public static boolean
20+
createAccount(String name,
21+
int passCode) // create account function
22+
{
23+
try {
24+
// validation
25+
if (name == "" || passCode == NULL) {
26+
System.out.println("All Field Required!");
27+
return false;
28+
}
29+
// query
30+
Statement st = con.createStatement();
31+
sql = "INSERT INTO customer(cname,balance,pass_code) values('"
32+
+ name + "',1000," + passCode + ")";
33+
34+
// Execution
35+
if (st.executeUpdate(sql) == 1) {
36+
System.out.println(name
37+
+ ", Now You Login!");
38+
return true;
39+
}
40+
// return
41+
}
42+
catch (SQLIntegrityConstraintViolationException e) {
43+
System.out.println("Username Not Available!");
44+
}
45+
catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
return false;
49+
}
50+
public static boolean
51+
loginAccount(String name, int passCode) // login method
52+
{
53+
try {
54+
// validation
55+
if (name == "" || passCode == NULL) {
56+
System.out.println("All Field Required!");
57+
return false;
58+
}
59+
// query
60+
sql = "select * from customer where cname='"
61+
+ name + "' and pass_code=" + passCode;
62+
PreparedStatement st
63+
= con.prepareStatement(sql);
64+
ResultSet rs = st.executeQuery();
65+
// Execution
66+
BufferedReader sc = new BufferedReader(
67+
new InputStreamReader(System.in));
68+
69+
if (rs.next()) {
70+
// after login menu driven interface method
71+
72+
int ch = 5;
73+
int amt = 0;
74+
int senderAc = rs.getInt("ac_no");
75+
;
76+
int receiveAc;
77+
while (true) {
78+
try {
79+
System.out.println(
80+
"Hallo, "
81+
+ rs.getString("cname"));
82+
System.out.println(
83+
"1)Transfer Money");
84+
System.out.println("2)View Balance");
85+
System.out.println("5)LogOut");
86+
87+
System.out.print("Enter Choice:");
88+
ch = Integer.parseInt(
89+
sc.readLine());
90+
if (ch == 1) {
91+
System.out.print(
92+
"Enter Receiver A/c No:");
93+
receiveAc = Integer.parseInt(
94+
sc.readLine());
95+
System.out.print(
96+
"Enter Amount:");
97+
amt = Integer.parseInt(
98+
sc.readLine());
99+
100+
if (bankManagement
101+
.transferMoney(
102+
senderAc, receiveAc,
103+
amt)) {
104+
System.out.println(
105+
"MSG : Money Sent Successfully!\n");
106+
}
107+
else {
108+
System.out.println(
109+
"ERR : Failed!\n");
110+
}
111+
}
112+
else if (ch == 2) {
113+
114+
bankManagement.getBalance(
115+
senderAc);
116+
}
117+
else if (ch == 5) {
118+
break;
119+
}
120+
else {
121+
System.out.println(
122+
"Err : Enter Valid input!\n");
123+
}
124+
}
125+
catch (Exception e) {
126+
e.printStackTrace();
127+
}
128+
}
129+
}
130+
else {
131+
return false;
132+
}
133+
// return
134+
return true;
135+
}
136+
catch (SQLIntegrityConstraintViolationException e) {
137+
System.out.println("Username Not Available!");
138+
}
139+
catch (Exception e) {
140+
e.printStackTrace();
141+
}
142+
return false;
143+
}
144+
public static void
145+
getBalance(int acNo) // fetch balance method
146+
{
147+
try {
148+
149+
// query
150+
sql = "select * from customer where ac_no="
151+
+ acNo;
152+
PreparedStatement st
153+
= con.prepareStatement(sql);
154+
155+
ResultSet rs = st.executeQuery(sql);
156+
System.out.println(
157+
"-----------------------------------------------------------");
158+
System.out.printf("%12s %10s %10s\n",
159+
"Account No", "Name",
160+
"Balance");
161+
162+
// Execution
163+
164+
while (rs.next()) {
165+
System.out.printf("%12d %10s %10d.00\n",
166+
rs.getInt("ac_no"),
167+
rs.getString("cname"),
168+
rs.getInt("balance"));
169+
}
170+
System.out.println(
171+
"-----------------------------------------------------------\n");
172+
}
173+
catch (Exception e) {
174+
e.printStackTrace();
175+
}
176+
}
177+
public static boolean transferMoney(int sender_ac,
178+
int reveiver_ac,
179+
int amount)
180+
throws SQLException // transfer money method
181+
{
182+
// validation
183+
if (reveiver_ac == NULL || amount == NULL) {
184+
System.out.println("All Field Required!");
185+
return false;
186+
}
187+
try {
188+
con.setAutoCommit(false);
189+
sql = "select * from customer where ac_no="
190+
+ sender_ac;
191+
PreparedStatement ps
192+
= con.prepareStatement(sql);
193+
ResultSet rs = ps.executeQuery();
194+
195+
if (rs.next()) {
196+
if (rs.getInt("balance") < amount) {
197+
System.out.println(
198+
"Insufficient Balance!");
199+
return false;
200+
}
201+
}
202+
203+
Statement st = con.createStatement();
204+
205+
// debit
206+
con.setSavepoint();
207+
208+
sql = "update customer set balance=balance-"
209+
+ amount + " where ac_no=" + sender_ac;
210+
if (st.executeUpdate(sql) == 1) {
211+
System.out.println("Amount Debited!");
212+
}
213+
214+
// credit
215+
sql = "update customer set balance=balance+"
216+
+ amount + " where ac_no=" + reveiver_ac;
217+
st.executeUpdate(sql);
218+
219+
con.commit();
220+
return true;
221+
}
222+
catch (Exception e) {
223+
e.printStackTrace();
224+
con.rollback();
225+
}
226+
// return
227+
return false;
228+
}
229+
}

0 commit comments

Comments
 (0)