-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
358 lines (337 loc) · 16 KB
/
Main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package com.TugasPraktikum7;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
while(true){
System.out.println("=========================================");
System.out.println("MENU: ");
System.out.println("1) Registrasi");
System.out.println("2) Login");
System.out.println("3) Tampilkan Data");
System.out.println("4) Ubah Data");
System.out.println("5) Hapus Data");
System.out.println("6) Exit");
System.out.print("Pilih Menu: ");
int menu = in.nextInt();
if(menu == 1){
System.out.println("--------------- REGISTRASI ---------------");
System.out.print("Masukkan username: ");
in.nextLine();
String username = in.nextLine();
System.out.print("Masukkan Password: ");
String pass = in.nextLine();
if(CheckRegistrasi(username, pass)){
System.out.println("=========================================");
System.out.println(" DATA BERHASIL DIDAFTARKAN");
System.out.println("=========================================");
Registrasi(username,pass);
FileWriter fw = new FileWriter("output.txt",true);
BufferedWriter writer = new BufferedWriter(fw);
String kode = String.format("%04d", userCode(username));
writer.write(username.substring(0, 3).toUpperCase()+"-"+kode+"\n");
writer.close();
System.out.println("Username : "+username);
System.out.println("Password : "+pass);
System.out.println("Kode User : "+username.substring(0, 3).toUpperCase()+"-"+kode);
System.out.println("=========================================");
}
else{
System.out.println("=========================================");
System.out.println(" REGISTRASI GAGAL !!!");
System.out.println("=========================================");
}
}else if(menu == 2){
System.out.println("--------------- LOGIN ---------------");
System.out.print("Masukkan username : ");
in.nextLine();
String username = in.nextLine();
System.out.print("Masukkan password : ");
String pass = in.nextLine();
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password="";
String userCode="";
int count=0;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
if(username.equals(uname)){
count++;
break;
}
}
if(count==0){
System.out.println("Login gagal, username tidak ditemukan");
}else if(!pass.equals(password)){
System.out.println("Login gagal, password yang anda masukkan salah");
}else{
System.out.println("=========================================");
System.out.println("SELAMAT DATANG "+userCode+" !!!");
System.out.println("Data Anda ");
System.out.println("Username : "+username);
System.out.println("Kode User : "+userCode);
System.out.println("Password : "+password);
}
reader.close();
}else if(menu == 3){
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String username ;
String pass;
String userCode;
int count = 1;
System.out.println(" [[[[[[[[ LIST DATA ]]]]]]]]");
while((username = reader.readLine())!= null){
pass = reader.readLine();
userCode = reader.readLine();
System.out.println("=========== Data "+count+" ===========");
System.out.println("Username : "+username);
System.out.println("Kode User : "+ userCode+"\n");
count++;
}
reader.close();
}else if(menu == 4){
System.out.println("=========================================");
System.out.println("Silahkan login ke akun yang ingin anda ubah");
System.out.print("Masukkan username : ");
in.nextLine();
String username = in.nextLine();
System.out.print("Masukkan password : ");
String pass = in.nextLine();
if(checkLogin(username, pass)){
System.out.println("=========================================");
System.out.println("1) Ubah Username");
System.out.println("2) Ubah Password");
System.out.print("Pilih nomor: ");
int option= in.nextInt();
String usernameUpdate=username;
String passUpdate=pass;
String usernameCheck = " ";
if(option==1){
System.out.println("=========================================");
System.out.print("Masukkan username baru: ");
in.nextLine();
usernameUpdate = in.nextLine();
usernameCheck = usernameUpdate;
}else if(option==2){
System.out.println("=========================================");
System.out.print("Masukkan password baru: ");
in.nextLine();
passUpdate = in.nextLine();
}
List<String> unameTemp = new LinkedList<String>();
List<String> passTemp = new LinkedList<String>();
List<String> userCodeTemp = new LinkedList<String>();
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password="";
String userCode="";
int count=0;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
if(username.equals(uname)){
unameTemp.add(usernameUpdate);
passTemp.add(passUpdate);
userCodeTemp.add(usernameUpdate.substring(0, 3).toUpperCase()+userCode.substring(3, 8));
}else{
unameTemp.add(uname);
passTemp.add(password);
userCodeTemp.add(userCode);
}
}
reader.close();
if(CheckRegistrasi(usernameCheck, passUpdate)){
FileWriter fw = new FileWriter("output.txt");
BufferedWriter writer = new BufferedWriter(fw);
for(String list : unameTemp){
writer.write(list+"\n");
writer.write(passTemp.get(unameTemp.indexOf(list))+"\n");
writer.write(userCodeTemp.get(unameTemp.indexOf(list))+"\n");
}
writer.close();
System.out.println("=========================================");
System.out.println("========== DATA BERHASIL DIUBAH =========");
System.out.println("=========================================");
}else{
System.out.println("=========================================");
System.out.println(" GAGAL MENGUBAH DATA !!!");
System.out.println("=========================================");
}
}
}else if(menu == 5){
System.out.println("=========================================");
System.out.println("Silahkan login ke akun yang ingin anda hapus");
System.out.print("Masukkan username : ");
in.nextLine();
String username = in.nextLine();
System.out.print("Masukkan password : ");
String pass = in.nextLine();
List<String> unameTemp = new LinkedList<String>();
List<String> passTemp = new LinkedList<String>();
List<String> userCodeTemp = new LinkedList<String>();
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password="";
String userCode="";
int count=0;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
if(username.equals(uname)){
count++;
}else{
unameTemp.add(uname);
passTemp.add(password);
userCodeTemp.add(userCode);
}
if(username.equals(uname) && pass.equals(password)){
count++;
}
}
reader.close();
if(count==0){
System.out.println("Login gagal, username tidak ditemukan");
}else if(count==1){
System.out.println("Login gagal, password yang anda masukkan salah");
}else{
FileWriter fw = new FileWriter("output.txt");
BufferedWriter writer = new BufferedWriter(fw);
int kode =1;
for(String list : unameTemp){
String kodeString = String.format("%04d", kode);
writer.write(list+"\n");
writer.write(passTemp.get(unameTemp.indexOf(list))+"\n");
writer.write(userCodeTemp.get(unameTemp.indexOf(list)).substring(0, 4)+kodeString+"\n");
kode++;
}
writer.close();
System.out.println("=========================================");
System.out.println(" DATA BERHASIL DIHAPUS");
System.out.println("=========================================");
}
}else if(menu == 6){
System.exit(0);
}else{
System.out.println("MENU TIDAK TERSEDIA, SILAHKAN MASUKKAN KEMBALI PILIHAN MENU");
}
}
}
public static void Registrasi(String username, String pass) throws IOException{
FileWriter fw = new FileWriter("output.txt",true);
BufferedWriter writer = new BufferedWriter(fw);
writer.write(username+"\n");
writer.write(pass+"\n");
writer.close();
}
public static int userCode(String username) throws IOException{
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password;
String userCode;
int kode = 0;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
kode++;
}
reader.close();
return kode;
}
public static boolean CheckRegistrasi(String username, String pass) throws IOException{
// Check syarat Username
if(username.length()<7){
System.out.println("Username harus lebih dari 6 karakter");
return false;
}
for(int i=0;i<username.length();i++){
if(username.charAt(i) == '!' | username.charAt(i) == '@' | username.charAt(i) == '#' | username.charAt(i) == '$' | username.charAt(i) == '%' | username.charAt(i) == '^' | username.charAt(i) == '&' | username.charAt(i) == '*' | username.charAt(i) == '(' | username.charAt(i) == ')' | username.charAt(i) == '.' | username.charAt(i) == ',' ){
System.out.println("Username tidak boleh mengandung spesial karakter");
return false;
}
}
// Check duplikasi Username
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password;
String userCode;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
if(uname.equals(username)){
System.out.println("Username sudah terpakai");
return false;
}
}
reader.close();
// Check syarat Password
if(pass.length()<9){
System.out.println("Password harus lebih dari 8 karakter");
return false;
}else if(!pass.matches(".*[A-Z]+.*") ){
System.out.println("Password tidak mengandung huruf kapital");
return false;
}else if(!pass.matches(".*[a-z]+.*")){
System.out.println("Password tidak mengandung huruf kecil");
return false;
}
int count =0;
for(int i=0;i<pass.length();i++){
if(pass.charAt(i) == '!' | pass.charAt(i) == '@' | pass.charAt(i) == '#' | pass.charAt(i) == '$' | pass.charAt(i) == '%' | pass.charAt(i) == '^' | pass.charAt(i) == '&' | pass.charAt(i) == '*' | pass.charAt(i) == '(' | pass.charAt(i) == ')' | pass.charAt(i) == '.' | pass.charAt(i) == ',' ){
count++;
}
}
if(count==0){
System.out.println("Password tidak mengandung spesial karakter (!@#$%^&*().,)");
return false;
}
// Check Palindrome
StringBuilder sb=new StringBuilder(pass);
sb.reverse();
String rev=sb.toString();
if(pass.equals(rev)){
System.out.println("Password tidak boleh berbentuk Palindrome");
return false;
}
return true;
}
public static boolean checkLogin(String username, String pass) throws IOException{
FileReader fr = new FileReader("output.txt");
BufferedReader reader = new BufferedReader(fr);
String uname ;
String password="";
String userCode="";
int count=0;
while((uname = reader.readLine())!= null){
password = reader.readLine();
userCode = reader.readLine();
if(username.equals(uname)){
count++;
break;
}
}
reader.close();
if(count==0){
System.out.println("Login gagal, username tidak ditemukan");
return false;
}else if(!pass.equals(password)){
System.out.println("Login gagal, password yang anda masukkan salah");
return false;
}else{
return true;
}
}
}