-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.java
313 lines (272 loc) · 11.9 KB
/
UI.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package banking;
import org.sqlite.SQLiteDataSource;
import java.sql.*;
import java.util.*;
public class UI {
private boolean isWorking = true;
CreditCard currentCard = null;
private ArrayList<CreditCard> creditCards = new ArrayList<CreditCard>();
Scanner scanner = new Scanner(System.in);
SQLiteDataSource dataSource;
public UI() {
dataSource = new SQLiteDataSource();
dataSource.setUrl(Main.URL);
try (Connection con = dataSource.getConnection()) {
con.isValid(5);
try (Statement statement = con.createStatement()) {
// Statement execution
statement.executeUpdate("CREATE TABLE IF NOT EXISTS card(" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"number TEXT NOT NULL," +
"pin TEXT NOT NULL," +
"balance INTEGER DEFAULT 0);");
} catch (SQLException e) {
e.printStackTrace();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
public boolean showMenu () {
int chosen;
System.out.println();
System.out.println("1. Create an account");
System.out.println("2. Log into account");
System.out.println("0. Exit");
chosen = scanner.nextInt();
if (chosen == 1) {
createAccount();
} else if (chosen == 2) {
logAccount();
} else if (chosen == 0) {
System.out.println("\nBye!");
isWorking = false;
}
return isWorking;
}
void createAccount() {
byte[] IIN = new byte[]{4,0,0,0,0,0};
byte[] creatorNumb = new byte[9];
byte checkSum = 0;
byte [] creatorPinCode = new byte[4];
Random random = new Random();
for (int i = 0; i < creatorNumb.length; i++) {
creatorNumb[i] = (byte) random.nextInt(10);
}
//implementation Luhn algorithm
byte[] temporaryArr = new byte[15];
byte temporarySum = 0;
for (int i = 0; i < IIN.length; i++) {
temporaryArr[i] = IIN[i];
}
for (int i = 0; i < creatorNumb.length; i++) {
temporaryArr[i + IIN.length] = creatorNumb[i];
}
for (int i = 0; i < temporaryArr.length; i++) {
if ((i + 1) % 2 == 1) {
temporaryArr[i] *= 2;
}
}
for (int i = 0; i < temporaryArr.length; i++) {
if (temporaryArr[i] > 9) {
temporaryArr[i] -= 9;
}
temporarySum += temporaryArr[i];
}
for (byte i = 0 ; i < 10; i++) {
if ((temporarySum + i) % 10 == 0) {
checkSum = i;
}
}
//create Pin Code
for (int i = 0; i < creatorPinCode.length; i++) {
creatorPinCode[i] = (byte) random.nextInt(10);
}
String numberCard = "";
for (byte numb: IIN) {
numberCard += numb;
}
for (byte numb : creatorNumb) {
numberCard = numberCard + numb;
}
numberCard += checkSum;
String pinCode = "";
for (byte numb : creatorPinCode) {
pinCode += numb;
}
// record to DATABASE
boolean isExisting = false;
try (Connection con = dataSource.getConnection()) {
try (Statement statement = con.createStatement()) {
String command = "SELECT * FROM Card WHERE number = " + numberCard + ";" ;
try (ResultSet validateCards = statement.executeQuery(command)) {
if (validateCards.next()) {
isExisting = true;
}
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
if (!isExisting) {
command = "INSERT INTO Card (number, pin, balance) VALUES (" +
numberCard + ", " + pinCode + ", 0);";
statement.executeUpdate(command);
System.out.println("\nYour card has been created");
System.out.println("Your card number");
System.out.println(numberCard);
System.out.println("Your card PIN");
System.out.println(pinCode);
}
} catch (SQLException e) {
e.printStackTrace();
isExisting = true;
}
}
catch (SQLException e) {
e.printStackTrace();
}
if (isExisting) {
createAccount();
}
}
void logAccount() {
System.out.println("\nEnter your card number:");
String currentNumber = scanner.next();
System.out.println("Enter your PIN:");
String currentPin = scanner.next();
// work with DATABASE
boolean isExisting = false;
try (Connection con = dataSource.getConnection()) {
try (Statement statement = con.createStatement()) {
// Statement execution
String command = "SELECT * FROM Card WHERE number = '" +
currentNumber + "' AND pin ='" + currentPin + "';";
try (ResultSet validateCards = statement.executeQuery(command)) {
if (validateCards.next()) {
// Retrieve column values
String pin = validateCards.getString("pin");
String name = validateCards.getString("number");
int balance = validateCards.getInt("balance");
currentCard = new CreditCard(name, pin, balance);
} else {
System.out.println("\nWrong card number or PIN");
return;
}
}
System.out.println("\nYou have successfully logged in!");
workWithAccount();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
void workWithAccount() {
System.out.println("\n1. Balance");
System.out.println("2. Add income");
System.out.println("3. Do transfer");
System.out.println("4. Close account");
System.out.println("5. Log out");
System.out.println("0. Exit");
int answer = scanner.nextInt();
if (answer == 1) {
System.out.printf("\nBalance : %d\n", currentCard.getBalance());
workWithAccount();
} else if (answer == 2) {
System.out.println("\nEnter income:");
int moneyIn = scanner.nextInt();
currentCard.setBalance(currentCard.getBalance() + moneyIn);
try (Connection con = dataSource.getConnection()) {
try (Statement statement = con.createStatement()) {
String command = "UPDATE Card SET balance = " + currentCard.getBalance() + " WHERE number = '" +
currentCard.getNumberCard() + "';" ;
if ( statement.executeUpdate(command) == 1) {
System.out.println("Income was added!");
workWithAccount();
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
} else if (answer == 3) {
System.out.println("\nTransfer");
System.out.println("Enter card number:");
String cardTo = scanner.next();
if (cardTo.equals(currentCard.getNumberCard())) {
System.out.println("You can't transfer money to the same account!");
workWithAccount();
} else {
if (!CreditCard.isValid(cardTo)) {
System.out.println("You made mistake in the card number.");
System.out.println("Please try again!");
workWithAccount();
} else {
try (Connection con = dataSource.getConnection()) {
try (Statement statement = con.createStatement()) {
String command = "SELECT * FROM Card WHERE number = " + cardTo + ";";
try (ResultSet validateCards = statement.executeQuery(command)) {
if (validateCards.next()) {
int balanceFrom = validateCards.getInt("balance");
System.out.println("Enter how much money you want to transfer:");
int moneyTo = scanner.nextInt();
if (moneyTo > currentCard.getBalance()) {
System.out.println("Not enough money!");
workWithAccount();
} else {
currentCard.setBalance(currentCard.getBalance() - moneyTo);
moneyTo += balanceFrom;
command = "UPDATE Card SET balance = " + currentCard.getBalance() + " WHERE number = '" +
currentCard.getNumberCard() + "';";
statement.executeUpdate(command);
command = "UPDATE Card SET balance = " + moneyTo + " WHERE number = '" +
cardTo + "';";
statement.executeUpdate(command);
System.out.println("Success!");
workWithAccount();
}
} else {
System.out.println("Such a card does not exit");
workWithAccount();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
} else if (answer ==4) {
try (Connection con = dataSource.getConnection()) {
try (Statement statement = con.createStatement()) {
String command = "DELETE FROM Card WHERE number = '" + currentCard.getNumberCard() + "';";
if (statement.executeUpdate(command) == 1) {
currentCard = null;
System.out.println("\nThe account has been closed!");
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
} else if (answer == 5) {
currentCard = null;
System.out.println("\nYou successfully logged out!");
} else if (answer == 0) {
System.out.println("\nBye!");
isWorking = false;
}
}
}