-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.java
More file actions
245 lines (225 loc) · 8.02 KB
/
methods.java
File metadata and controls
245 lines (225 loc) · 8.02 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
package com.bkbank;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class methods {
// These will clear the screen
public static void clear_screen(){
for(int i = 0; i <= 200; i++){
System.out.println("\n");
}
}
// These will stop code or stop jvm on any error
public static void stop_code(String message){
if(message.length() > 2) {
System.out.println(message);
} else {
System.out.println("Hey My Mistake ! Please Wait Our Server is Busy");
}
System.exit(0);
}
// These will we stop / hang code for some time
public static void delay(int time){
try{
Thread.sleep(time);
} catch (Exception e){
stop_code("0");
}
}
// These will we help to take input from user and just as a choicebox / checkbox
public static boolean Selection_Input(String error,String print,char True_vale, char false_value){
Scanner input = new Scanner(System.in);
System.out.print(print);
try {
char selection = input.next().charAt(0);
int check_selection_y = Character.compare(selection, True_vale);
int check_selection_n = Character.compare(selection, false_value);
if(check_selection_y == 0){
return true;
} else if (check_selection_n == 0){
return false;
} else {
System.out.println(error);
}
} catch (Exception e){
System.out.println(error);
}
return false;
}
// These will return true if string length verified byt give argument
public static boolean length_check_string(String value,int min, int max,String error){
boolean min_flag_s = false, max_flag_s = false;
int string_value_length = 0;
try {
string_value_length = value.length();
} catch (Exception e){
System.out.println("PLEASE USE WORDS AND INTEGER IN USER NAME EX:- Rahul,sonam45,kitelover54,bkbanku e.t.c");
}
if(string_value_length > 1){
if(string_value_length >= min){
min_flag_s = true;
}else {
System.out.println(error);
}
if (string_value_length <= max){
max_flag_s = true;
if(min_flag_s){
return true;
}
} else {
System.out.println(error);
}
} else {
System.out.println(error);
}
return false;
}
// These will return true if long length verified byt give argument
public static boolean length_check_long(long value,int min, int max,String error){
boolean min_flag_s = false, max_flag_s = false;
int long_value_length;
long_value_length = String.valueOf(value).length();
if(long_value_length > 1){
if(long_value_length >= min){
min_flag_s = true;
}else {
System.out.println(error);
}
if (long_value_length <= max){
max_flag_s = true;
if(min_flag_s){
return true;
}
} else {
System.out.println(error);
}
} else {
System.out.println(error);
}
return false;
}
// These will take input on string and return it
public static String input(String error,String print){
Scanner input = new Scanner(System.in);
System.out.print(print);
try {
String u_input = input.next();
return u_input;
} catch (Exception e){
System.out.println(error);
}
return null;
}
// it gen random string key --> (sequence of char)
public static String string_key_gen(int lengths){
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrsuvwxyz9876543210";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int i = 0; i < lengths; i++) {
int index = random.nextInt(alphabet.length());
char randomChar = alphabet.charAt(index);
sb.append(randomChar);
}
return sb.toString();
}
// These will create a file and write something
public static boolean file_create_and_write(String path, String data, String fileName){
File file = new File(path + "\\" + fileName);
FileWriter fr = null;
try {
// Below constructor argument decides whether to append or override
fr = new FileWriter(file, true);
fr.write(data);
return true;
} catch (IOException e) {
stop_code("Error occurred" + e);
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
stop_code("Error occurred" + e);
e.printStackTrace();
}
}
return false;
}
public static String open_file_return_string(String path_with_file_name){
try{
DataInputStream dis = new DataInputStream (new FileInputStream (path_with_file_name));
byte[] datainBytes = new byte[dis.available()];
dis.readFully(datainBytes);
dis.close();
return new String(datainBytes, 0, datainBytes.length);
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
public static String string_password_mask(String value, int length, int start, int end, String symbol){
String mask_data = "";
try{
String data = value.substring(start, end);
for(int i = 0; i <= length; i++){
mask_data += symbol;
}
return data + mask_data;
} catch (Exception e){
stop_code("ERROR TRY AGAIN !!!");
}
return "0";
}
public static char[] generate_random_number(int length) {
String numbers = "1234567890";
Random random = new Random();
char[] otp = new char[length];
for(int i = 0; i< length ; i++) {
otp[i] = numbers.charAt(random.nextInt(numbers.length()));
}
return otp;
}
public static long generateRandom(int length) {
Random random = new Random();
char[] digits = new char[length];
digits[0] = (char) (random.nextInt(9) + '1');
for (int i = 1; i < length; i++) {
digits[i] = (char) (random.nextInt(10) + '0');
}
return Long.parseLong(new String(digits));
}
public static boolean delete_folder(File file){
try {
// store all the paths of files and folders present
// inside directory
for (File subfile : file.listFiles()) {
// if it is a subfolder,e.g Rohan and Ritik,
// recursiley call function to empty subfolder
if (subfile.isDirectory()) {
delete_folder(subfile);
}
// delete files and empty subfolders
subfile.delete();
}
return true;
} catch (Exception e){
System.out.println("Error !!");
}
return false;
}
public static String current_time(){
Date date=new Date();
SimpleDateFormat df2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return df2.format(date);
}
public static boolean delete_File_content(String path){
try{
PrintWriter writer = new PrintWriter(path);
writer.print("");
writer.close();
return true;
} catch (Exception e){
return false;
}
}
}